简体   繁体   中英

How to correct the scope of a variable that is created within a do-while loop?

I am struggling to get the correct scope for my variable "input".

I am making a calculator for a university task, and I've got everything working apart from when I tried to make it loop by wrapping my main code in a do-while loop. Because the variable "input" is declared in the "do" part of the loop, it didn't know what it was when I was trying to use it in the "while" condition. To fix this I then declared "input" as a string before my do-while loop to make it a global. However, now the scanner that takes the value of input will not work. AM I doing something stupid or am I missing something?

import java.util.Scanner;

public class Calculator {

    public static void main(String [] args) {

        String input;

        do {

            System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

            Scanner myScanner = new Scanner(System.in);
            String oper = myScanner.nextLine();

            System.out.println("Now please enter two numbers:");
            double a = myScanner.nextDouble();
            double b = myScanner.nextDouble();

            switch (oper) {
            case "+" :
                System.out.println(CalculatorUtils.add(a, b));
                break;
            case "-" :
                System.out.println(CalculatorUtils.subtract(a, b));
                break;
            case "/" :
                System.out.println(CalculatorUtils.divide(a, b));
                break;
            case "*" :
                System.out.println(CalculatorUtils.multiply(a, b));
                break;
            }

        System.out.println("Do you want to complete another calculation? (y/n)");
        input = myScanner.nextLine();


        }
        while (input.contentEquals("y"));

    }


}

I expect this to be the output: Welcome to the calculator. Please enter an operator (+, -, /, *) below: + Now please enter two numbers: 32.5 12.5 45.0 Do you want to complete another calculation? (y/n) y (This is where the code would start again)

However I'm not being able to enter my input when being asked if I would like to do another calculation.

Here is the fix.

import java.util.Scanner;

public class Calculator {

    public static void main(String[] args) {

        String input;

        do {

            System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

            Scanner myScanner = new Scanner(System.in);
            String oper = myScanner.nextLine();

            System.out.println("Now please enter two numbers:");
            double a = myScanner.nextDouble();
            double b = myScanner.nextDouble();

            switch (oper) {
                case "+":
                    System.out.println(CalculatorUtils.add(a, b));
                    break;
                case "-":
                    System.out.println(CalculatorUtils.subtract(a, b));
                    break;
                case "/":
                    System.out.println(CalculatorUtils.divide(a, b));
                    break;
                case "*":
                    System.out.println(CalculatorUtils.multiply(a, b));
                    break;
            }
            myScanner.nextLine();
            System.out.println("Do you want to complete another calculation? (y/n)");
            input = myScanner.nextLine();
myScanner.nextLine();


        }
        while (input.contentEquals("y"));

    }


}

It happens because second time you call myScanner.nextLine() it just scans enter from before. It will happen after myScanner.nextDouble() but not after myScanner.nextLine() because myScanner.nextLine() reads/scans until including next newLine character ( \n ) whereas myScanner.nextDouble() will just scan a double and leave.

Here is similar thread

What you do not want to do is create a Scanner on every trip around the loop. Move the definition and initialization of your Scanner variable outside the loop:

    String input;
    Scanner myScanner = new Scanner(System.in);
    do {
        System.out.println("Welcome to the calculator. Please enter an operator (+, -, /, *) below:");

        String oper = myScanner.nextLine();

        // rest of loop...

    } while (input.contentEquals("y"));

This may or may not solve you're immediate problem, but it's still the right thing to do in general.

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