Scope of variable inside do-while loop

大兔子大兔子 提交于 2019-12-13 04:38:08

问题


I'm trying to loop through an entire program if the user inputs "y" or "Y" when they are prompted at the end of the program, but I'm getting a "cannot find symbol - variable response" error since the variable I declared is inside the do-while loop, therefore I suppose I can't use it in the condition of the do-while loop. Is there any way to get around this problem and still be able to loop through the program if the user inputs a "y" or "Y"? This is all in the main method and getFileRunStats is the method that contains all other methods from the program.

Here's the relevant code:

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


}

回答1:


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

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



回答2:


Answer by @ergonaut is the best solution for your exact problem:

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

do {

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

Notice that the code also fixed the error you had by using an equals method, not the == operator.


However, a more general solution for how to control a do-while loop using valued defined inside the loop, you have two choices:

// 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;
}



回答3:


just providing a more extensible approach to your problem because it allows you to dynamically add more options to your program in future.

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

}




回答4:


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


}


来源:https://stackoverflow.com/questions/33324653/scope-of-variable-inside-do-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!