I am trying to ask the user to enter two integers and have the message to say
"The GCD of "first integer" and "second integer" is "GCD"
I have all my calculations right but it is just printing out my num1 for all values.
import java.util.Scanner;
public class Assignment3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the first integer: ");
int num1 = scan.nextInt();
System.out.print("Enter the second integer: ");
int num2 = scan.nextInt();
while (num1 != num2)
{
if (num1> num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
}
System.out.println("The gcd of" + num1 + " and " + num2 + " is " + num1);
}
}
In your while loop you check if num1 != num2 so when the println gets executed num1 will have the same value as num2.
Related
I incorporated a couple different methods I've seen on here. Does anyone know how to fix this problem I am having? When you use this code, It asks for you to enter the mathematical operator, BUT when I do if I enter +9 or -%, it will still work and use the first symbol. I want it to give an error if someone inputs */ instead of just *. I even tried switching the case to numbers (ie case 1:) and it will do the same thing if I set addition to 1 and if I enter 15, it will read the one and do addition. Any ideas?
import java.util.Scanner;
public class javacalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double num1, num2, answer;
char userOperation;
System.out.print("Enter your full name: ");
String userName = input.nextLine();
System.out.println("+ Addition");
System.out.println("- Subtraction");
System.out.println("* Multiplication");
System.out.println("/ Division");
System.out.println("% Modulus");
System.out.println("\n");
System.out.print("Enter mathematical operator e.g. + for Addition: ");
userOperation = operation.next().charAt(0);
boolean invalidOperator = false;
switch (userOperation) {
case '+':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 + num2;
System.out.print(num1 + " + " + num2 + " = " + answer);
break;
case '-':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 - num2;
System.out.print(num1 + " - " + num2 + " = " + answer);
break;
case '*':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 * num2;
System.out.print(num1 + " * " + num2 + " = " + answer);
break;
case '/':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 / num2;
System.out.print(num1 + " / " + num2 + " = " + answer);
break;
case '%':
System.out.print("Enter first number: ");
num1 = input.nextInt();
System.out.print("Enter the second number: ");
num2 = input.nextInt();
answer = num1 % num2;
System.out.print(num1 + " % " + num2 + " = " + answer);
break;
default:
System.out.println("Invalid operator!");
break;
}
}
}
Let us break this down: operation.next().charAt(0);
operation.next() -> Gives you full input string
.charAt(0); -> returns the first character of the full input string
So anything we enter +9 or */ -> it returns the first element.
Now let's handle this case:
String o = operation.next(); // here is complete input
if(o.length()!=1){
userOperation = 0; //if length of input is not 1 we set custom value
}else{
userOperation = o.charAt(0); //get and set the first and only element
}
Hopefully it helps!
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args )
{
Scanner userInput = new Scanner(System.in);
String operator;
double num1,num2,answer = 0;
System.out.println("Enter first number: ");
num1 = userInput.nextDouble();
System.out.println("Enter operator: ");
operator = userInput.next();
System.out.println("Enter second number: ");
num2 = userInput.nextDouble();
if (operator.equals ("+")){
answer = num1 + num2;
}
else if (operator.equals ("-")){
answer = num1 - num2;
}
else if (operator.equals ("*")){
answer = num1 * num2;
}
else if (operator.equals ("/")){
answer = num1 / num2;
}
System.out.println("First number:" + num1);
System.out.println("Operator:" + operator);
System.out.println("Second number:" + num2);
System.out.println("Answer: " + answer);
}
}
Use this function:
Math.pow(x, y)
In this way: place the code
else if (operator.equals ("^")){
answer = Math.pow(num1, num2);
}
just after your present code
else if (operator.equals ("/")){
answer = num1 / num2;
}
So this part of code will then be
else if (operator.equals ("/")){
answer = num1 / num2;
}
else if (operator.equals ("^")){
answer = Math.pow(num1, num2);
}
Use a while loop to keep asking the user to enter the order of
numbers until the user does give two numbers in the right order (first
smaller than second
Hi! i'm a beginner in java and I have this code but I can't loop the "error" message. it just prints 2 times
import java.util.Scanner;
public class Q6 {
public static void main(String[] args) {
int num1, num2;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please type two numbers:");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
if (num1 < num2) {
int counter = num1;
while (counter <= num2) {
System.out.print(counter + " ");
counter = counter + 1;
}
}
else {
System.out.println("Error: the first number must be smaller than the second");
System.out.print("Please type two numbers: ");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
}
}
}
int num1,num2;
while (num1>=num2) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type two numbers");
System.out.printn("first number must be smaller than the second:)";
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
}
int num1,num2;
while (num1>=num2) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Please type two numbers:");
num1 = keyboard.nextInt();
num2 = keyboard.nextInt();
if(num1>=num2) {
System.out.println("Error: First number must be smaller than the second.");
}
}
I want to know exactly how these if statements are switching the numbers. I have never been asked to do non-descending order before so I took a little snip off the internet.
import java.util.Scanner;
/**
* Created by Nicholas on 10/26/2015.
*/
public class Main {
final static Scanner userInput = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Please enter three numbers.");
System.out.println();
System.out.print("Number 1: ");
int num1 = userInput.nextInt();
System.out.println();
System.out.print("Number 2: ");
int num2 = userInput.nextInt();
System.out.println();
System.out.print("Number 3: ");
int num3 = userInput.nextInt();
System.out.println();
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
if (num2 > num3) {
int temp = num2;
num2 = num3;
num3 = temp;
}
if (num1 > num2) {
int temp = num1;
num1 = num2;
num2 = temp;
}
System.out.print("The numbers in non-descending order are, " + num1 + " " + num2 + " " + num3);
}
}
The if statements switch the numbers by creating a temporary value to store the old number in before setting it to the new number, the other number is then set to the old, saved number
A more interesting way of switching numbers is with bitwise operators
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
This works from xoring the bits together, and because xor inverts itself when given the same value ((A xor B) xor B) == A
This is a program that is supposed to prompt the user to enter three numbers and
then display the largest of these numbers. However, there are logic errors in it. I'm stuck on trying to figure out where this little bugger is. Please use your expertise to lend me a hand. I am a student, so please don't rage on me \:p
import java.util.*;
public class HA8LargestErr {
private int num1;
private int num2;
private int num3;
public HA8LargestErr() {
num1 = 0;
num2 = 0;
num3 = 0;
}
public void getNumsFromUser() {
Scanner input = new Scanner (System.in);
System.out.println("Enter three numbers: ");
num1 = input.nextInt();
num2 = input.nextInt();
num3 = input.nextInt();
}
public int returnLargest() {
if (num1 > num2 && num1 > num3)
return num1;
if (num2 > num3 && num2 > num1)
return num2;
return num3;
}
public static void main(String[] args) {
HA8LargestErr data = new HA8LargestErr();
data.getNumsFromUser();
System.out.println ("The largest is : " + data.returnLargest());
}
}
Replace your implementation of returnLargest with
public int returnLargest() {
if (num1 >= num2 && num1 >= num3)
return num1;
if (num2 >= num3)
return num2;
return num3;
}
Or use Math.max as suggested above.
Edit:
You need to use >= instead of > because otherwise num3 will be returned when num1 and num2 are equal and larger than num3.