Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何更正在do while循环中创建的变量的范围?_Java_While Loop_Scope_Do While - Fatal编程技术网

Java 如何更正在do while循环中创建的变量的范围?

Java 如何更正在do while循环中创建的变量的范围?,java,while-loop,scope,do-while,Java,While Loop,Scope,Do While,我正在努力为变量“input”获取正确的范围 我正在为一项大学任务制作一个计算器,除了我试图通过将我的主代码包装在do-while循环中使其循环之外,我的一切都正常工作。因为变量“input”是在循环的“do”部分声明的,所以当我试图在“while”条件下使用它时,它不知道它是什么。为了解决这个问题,我在do-while循环之前将“input”声明为一个字符串,使其成为一个全局变量。但是,现在获取输入值的扫描仪将无法工作。 我是在做傻事还是错过了什么 import java.util.Scann

我正在努力为变量“input”获取正确的范围

我正在为一项大学任务制作一个计算器,除了我试图通过将我的主代码包装在do-while循环中使其循环之外,我的一切都正常工作。因为变量“input”是在循环的“do”部分声明的,所以当我试图在“while”条件下使用它时,它不知道它是什么。为了解决这个问题,我在do-while循环之前将“input”声明为一个字符串,使其成为一个全局变量。但是,现在获取输入值的扫描仪将无法工作。 我是在做傻事还是错过了什么

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"));

    }


}
我希望这是输出: 欢迎使用计算器。请在下面输入运算符(+、-、/、*): + 现在请输入两个数字: 32.5 12.5 45 是否要完成另一个计算?(是/否) Y (这是代码重新开始的地方)

但是,当被问及是否要进行另一次计算时,我无法输入我的输入。

这里是解决方法

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"));

    }


}
这是因为第二次调用
myScanner.nextLine()
。它将发生在
myScanner.nextDouble()
之后,但不会发生在
myScanner.nextLine()
之后,因为
myScanner.nextLine()
读取/扫描直到包含下一个换行符(
\n
),而myScanner.nextDouble()将只扫描一个双精度字符并离开

您不想做的是在每次循环中创建一个
扫描仪。将
扫描仪
变量的定义和初始化移到循环外:

    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"));

这可能解决不了您当前的问题,但总的来说,这仍然是正确的做法。

您所说的“无法输入我的输入”是什么意思?您的按键是否未记录在终端上?您所说的“更正范围”是什么意思?这里没有需要纠正的范围问题。@AndrewMcDowell,这没有帮助。
输入
之前读取,而
,因此它应该能正常工作。现在还不清楚“无法输入”在这里的实际含义。@Kayaman我想他想说的是,在被要求“进行另一次计算”之前,程序正在退出。@Kayaman说得好,没有引起足够的注意。谢谢,我已经将它移到了do while循环之外。它并没有解决我的问题,但它改进了我的最佳实践。我认为在读入
a
b
的值后,您需要执行
myScanner.nextLine()
。对原因的解释有点技术性和乏味,但应该会有所帮助。