-1

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!

solarissmoke
  • 30,039
  • 14
  • 71
  • 73
jth14
  • 9
  • 1
    Why are you specifically telling Java to add 1 to it with `number2++`? Why do you have so many instances of `++` randomly stuck to the end of things? – user2357112 Jul 10 '16 at 03:03

3 Answers3

3

In the following line

      System.out.println(" You have chosen numbers, " +number1+ ", and " +number2++);

You are incrementing number2 by one, by doing this

number2++

This is known as an increment, and what this does is to increment the value of a numeric variable by adding 1.

Remove those ++ at the end, and it should work.

Cristian Meneses
  • 4,013
  • 17
  • 32
0

When you do "choice++", that means to increment the value of "choice" by one.

Here's a short description.

D Hydar
  • 502
  • 2
  • 8
0

The answer is already posted here.

but instead of using if-else condition on choice, you must use switch statements and make cases according to user choice.

take a look at Why the switch statement and not if-else?

Community
  • 1
  • 1
Dheeraj Arya
  • 71
  • 1
  • 6
  • Thank you for the suggestion and the link! This is a homework assignment to work on the "if else statements". Next assignment uses the switch statement. Thanks again! – jth14 Jul 10 '16 at 17:48