问题
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