I have been struggling with this for the last two days. I have no idea at what point in my code I am making the mistake. Please help!
import java.util.Scanner;
class Program3
{
private static Scanner inp;
public static void main(String args[])
{
double number1;
double number2;
inp = new Scanner(System.in);
double repeat = 0;
do
{
System.out.println("Please enter a number.");
number1 = inp.nextDouble();
System.out.println("Please enter a second number.");
number2 = inp.nextDouble();
System.out.println(" You have chosen numbers, " +number1+ ", and " +number2++);
double add;
double subtract;
double multiply;
double divide;
double choice;
add = number1+number2;
subtract = number1-number2;
multiply = number1*number2;
divide = number1/number2;
System.out.println("What type of operation would you like to run? ");
System.out.println("Please enter the number that corresponds with the operation.");
System.out.println("Please note, that if your dividing number is equal to 0, the operation will return and error message.");
System.out.println("1. add");
System.out.println("2. subtract");
System.out.println("3. multiply");
System.out.println("4. divide");
choice = inp.nextInt();
if (choice == 1)
{choice = add;
System.out.println("By adding " +number1+ " to " +number2+ " the answer is " +choice++);
}
else if (choice == 2)
{choice = subtract;
System.out.println("By subtracting " +number1+ " and " +number2+ " the answer is " +choice++);
}
else if (choice == 3)
{choice = multiply;
System.out.println("By multiplying " +number1+ " by " +number2+ " the answer is " +choice++);
}
else if (choice == 4)
{choice = divide;
System.out.println("By dividing " +number1+ " from " +number2+ " the answer is " +choice++);
}
if (number2 == 0)
System.out.println("Error: Number cannot be divisible by 0. Please choose another number");
System.out.println("Would you like to try other numbers?");
System.out.println("Enter 1 to continue");
repeat = inp.nextDouble();
} while (repeat == 1);
inp.close();
}
}
Here is the result
Please enter a number.
2
Please enter a second number.
3
You have chosen numbers, 2.0, and 3.0
What type of operation would you like to run?
Please enter the number that corresponds with the operation.
Please note, that if your dividing number is equal to 0, the operation will return and error message.
1. add
2. subtract
3. multiply
4. divide
1
By adding 2.0 to 4.0 the answer is 6.0
Would you like to try other numbers?
Enter 1 to continue
As you can see. the number input by the user and the number in the operation have a difference of 1 and I cannot find the mistake in the code. Thanks for your help!