简体   繁体   中英

How can I make a calculator ask the user if they want to perform another calculation in Java?

I have tried to do this using a do while loop however the variable isn't found. My goal is to basically perform the code all over again when the user wants to. Can anybody help?

import java.util.Scanner;
public class Calculator {
    public static void main(String args[]) {
        do{
            Scanner typed=new Scanner(System.in);
            System.out.println("Please type: 1 for add, 2 for subtract, 3 for multiply or 4 for divide");
            int userInput=typed.nextInt();
            System.out.println("Please enter the first number to calculate:");
            double number1=typed.nextDouble();
            System.out.println("Please enter the second number to calculate:");
            double number2=typed.nextDouble();
            if (userInput==1){
                System.out.println("The answer is");
                double result=number1+number2;
                System.out.println(result);
            }
            if (userInput==2){
                System.out.println("The answer is");
                double result=number1-number2;
                System.out.println(result);
            }
            if (userInput==3){
                System.out.println("The answer is");
                double result=number1*number2;
                System.out.println(result);
            }
            if (userInput==4){
                System.out.println("The answer is");
                double result=number1/number2;
                System.out.println(result);
            }
                System.out.println("Do you want to perform another calculation? Press 1 for yes or 2 for no.");
                int pressed=typed.nextInt();
            
        }while(pressed==1);
            
    }
}

You're trying to use a variable outside of its scope.

    do{
      // ...
      int pressed=typed.nextInt();
        
    }while(pressed==1);  // Outside of the scope of 'pressed'

You can address this by moving the declaration outside the loop, but leaving the assignment.

    int pressed = 1;

    do{
      // ...
      pressed=typed.nextInt();
        
    }while(pressed==1);  // Outside of the scope of 'pressed'

pressed leaves scope after the body of your loop. Move the declaration of pressed to before the do .

int pressed = 0;
do {
    // ...
    pressed=typed.nextInt();
} while(pressed==1);

Or , you could change your loop to an infinite loop while(true); and use

int pressed = typed.nextInt();
if (pressed != 1) {
    break; // End the infinite loop
}

Just declare 'pressed' variable outside the do while loop.

int pressed;
do 
{
 //code
}

and remove int from int pressed=typed.nextInt(); this line so that it becomes pressed=typed.nextInt();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM