do-while 循环中的变量范围

Scope of variable inside do-while loop

如果用户在程序结束时收到提示时输入 "y" 或 "Y",我将尝试循环整个程序,但我得到的是 "cannot find symbol - variable response" 错误,因为我声明的变量在 do-while 循环内,因此我想我不能在 do-while 循环的条件下使用它。如果用户输入 "y" 或 "Y",是否有任何方法可以解决此问题并仍然能够循环执行程序?这些都在 main 方法中,getFileRunStats 是包含程序中所有其他方法的方法。

相关代码如下:

public static void main(String[] args) throws IOException {
    System.out.println("A program to analyze home field advantage in sports.");
    System.out.println();        
    Scanner keyboard = new Scanner(System.in);

do{   
    getFileRunStats(keyboard);

    System.out.println("Do you want to check another data set?");
    System.out.print("Enter Y or y for to analyze another file, anything else to quit: ");
    String response = keyboard.nextLine();
}while(response == "Y" || response == "y");


}

您可以只删除该语句,并将其添加到循环条件中。

do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));

@ergonaut 的回答是针对您具体问题的最佳解决方案:

You can just remove the statement, and add it to the loop condition.

do {

} while (keyboard.nextLine().equalsIgnoreCase("y"));

请注意,该代码还修复了您使用 equals 方法而不是 == 运算符时遇到的错误


但是,对于如何使用在 循环中定义的值 来控制 do-while 循环的更通用的解决方案,您有两个选择:

// Define variable outside, no need to initialize it
String response;
do {
    // code here
    response = keyboard.nextLine();
} while (response.equals("Y") || response.equals("y"));

// Use break inside an infinite loop
for (;;) { // or   while (true) { ... }   or   do { ... } while (true)
    // code here
    String response = keyboard.nextLine();
    if (! response.equals("Y") && ! response.equals("y"))
        break;
}

只是为您的问题提供了一种更可扩展的方法,因为它允许您在将来动态地向您的程序添加更多选项。

public static void main(String[] args) {
    System.out.println("A program to analyze home field advantage in sports.");
    System.out.println();
    Scanner keyboard = new Scanner(System.in);

    boolean quit = false;
    do{
        System.out.println("Do you want to check another data set?");
        System.out.print("Enter Y or y for to analyze another file ");
        System.out.print("Enter X to do X");
        System.out.print("Enter Z to do Z");
        String response = getUserInput(keyboard);

        if (response.equalsIgnoreCase("X"))
            doX();
        else if (response.equalsIgnoreCase("Y"))
            doY();
        else if (response.equalsIgnoreCase("Z"))
            doZ();
        else
            quit = true;

    }while(!quit);
}

private static String getUserInput(Scanner scanner) {
    return scanner.nextLine();
}

private static void doX() {
    System.out.println("X");
}

private static void doY() {
    System.out.println("Y");
}

private static void doZ() {
    System.out.println("Z");
}

}

Just declare the "response" varialbe outside of do-while loop, like this:

String reponse = null;
do{   
    getFileRunStats(keyboard);

    System.out.println("Do you want to check another data set?");
    System.out.print("Enter Y or y for to analyze another file, anything else to quit: ");
    response = keyboard.nextLine();
}while(response == "Y" || response == "y");


}