Hello, I have this calculator program that compiles and runs just fine, but my loop will not break as intendend too, when I hit 5 it it still runs the options to enter a number, here is my code:

import java.util.Scanner;

public class calculator
{
    public static void main(String[] args) 
    {

      int i = 0;

      do
      {   
            Scanner s = new Scanner(System.in);

            System.out.println("-Easy Calculator-");

            System.out.println("1) Add");

            System.out.println("2) Subtract");

            System.out.println("3) Multiply");

            System.out.println("4) Divide");

            System.out.println("5) Exit");

        System.out.println(" ");


        System.out.println("Enter Option: ");

            i = s.nextInt();


            System.out.println("First Number: ");
            int a = s.nextInt();
        System.out.println(" ");
            System.out.println("Your first number number is " +a);
        System.out.println(" ");



            System.out.println("Second Number: ");
            int b = s.nextInt();
        System.out.println(" ");
            System.out.println("Your second number number is " +b);
        System.out.println(" ");



            double result = 0;


       switch (i)
            {
                case 1:
                result = a + b;
          System.out.println("Answer = " +result);
          System.out.println(" ");
          break;


                case 2:
                result = a - b;
          System.out.println("Answer = " +result);
          System.out.println(" ");
          break;

                case 3:
                result = a * b;
          System.out.println("Answer = " +result);
          System.out.println(" ");
                break;


                case 4:
                if (b == 0)
          {
                  System.out.println("Cannot Divide By Zero");
            System.out.println(" ");
            break;
          }

            else
                   result = a / b;
             System.out.println("Answer = " +result);
             System.out.println(" ");
             break;

          case 5:
          System.out.println("Thank you for using easy calculator. Goodbye");
          break;

                default:
                System.out.println("You have entered a wrong choice");
          System.out.println(" ");
          break;
            }


    }while(i != 5);

 }


}

I do not know exactly what I need to do, i tried changing my do while statements, but nothing

Recommended Answers

All 3 Replies

that is because you wrote your code like that.
you have to check for '5' the moment you input it, unless you want to re-write more code.

but also: why do you initialize your Scanner object each iteration? why don't you just create one and re-use that?

On lines 34-46 you prompt for first and second number, even if the user's menu choice was not a calculation (1,2,3 or 4)
If the user's choice was 5 your program exits properly after prompting for those two values.
You may want to make prompting for 1st and 2nd number dependent on the menu choice being 1-4 ?

I'm an idiot, I got it, thanks guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.