Java do while循环内变量的范围
如果用户在程序末尾收到提示时输入“y”或“y”,我会尝试循环整个程序,但是我得到了一个“找不到符号-变量响应”错误,因为我声明的变量在do while循环中,因此我想我不能在do while循环的条件下使用它。如果用户输入“y”或“y”,有没有办法绕过这个问题,仍然能够循环程序?这都在main方法中,getFileRunStats是包含程序中所有其他方法的方法 以下是相关代码:Java do while循环内变量的范围,java,if-statement,while-loop,scope,do-while,Java,If Statement,While Loop,Scope,Do While,如果用户在程序末尾收到提示时输入“y”或“y”,我会尝试循环整个程序,但是我得到了一个“找不到符号-变量响应”错误,因为我声明的变量在do while循环中,因此我想我不能在do while循环的条件下使用它。如果用户输入“y”或“y”,有没有办法绕过这个问题,仍然能够循环程序?这都在main方法中,getFileRunStats是包含程序中所有其他方法的方法 以下是相关代码: public static void main(String[] args) throws IOException {
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"));
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
@ergonaut的答案是解决您确切问题的最佳解决方案:
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
do{
}while(keyboard.nextLine().equalsIgnoreCase(“y”)代码>
请注意,代码还通过使用equals
方法(而不是=
操作符)修复了错误
但是,对于如何使用循环内定义的值控制do而循环,有两种更通用的解决方案:
// 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;
}
@ergonaut的答案是解决您确切问题的最佳解决方案:
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
do{
}while(keyboard.nextLine().equalsIgnoreCase(“y”)代码>
请注意,代码还通过使用equals
方法(而不是=
操作符)修复了错误
但是,对于如何使用循环内定义的值控制do而循环,有两种更通用的解决方案:
// 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;
}
@ergonaut的答案是解决您确切问题的最佳解决方案:
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
do{
}while(keyboard.nextLine().equalsIgnoreCase(“y”)代码>
请注意,代码还通过使用equals
方法(而不是=
操作符)修复了错误
但是,对于如何使用循环内定义的值控制do而循环,有两种更通用的解决方案:
// 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;
}
@ergonaut的答案是解决您确切问题的最佳解决方案:
您可以删除该语句,然后将其添加到循环条件中
do {
} while (keyboard.nextLine().equalsIgnoreCase("y"));
do{
}while(keyboard.nextLine().equalsIgnoreCase(“y”)代码>
请注意,代码还通过使用equals
方法(而不是=
操作符)修复了错误
但是,对于如何使用循环内定义的值控制do而循环,有两种更通用的解决方案:
// 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");
}
}只是为您的问题提供了一种更具可扩展性的方法,因为它允许您在将来为程序动态添加更多选项
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");
}
}只是为您的问题提供了一种更具可扩展性的方法,因为它允许您在将来为程序动态添加更多选项
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");
}
}只是为您的问题提供了一种更具可扩展性的方法,因为它允许您在将来为程序动态添加更多选项
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");
}
}
只需在do while循环之外声明“response”变量,如下所示:
只需在do while循环之外声明“response”变量,如下所示:
只需在do while循环之外声明“response”变量,如下所示:
只需在do while循环之外声明“response”变量,如下所示:
您可以在do
之前声明字符串响应。在Java中,您无法将字符串与==
进行比较,我尝试过这样做,但即使用户出于某种原因键入“Y”或“Y”,程序也会停止。您是否删除了response=keyboard.nextLine()之前的“String”字代码>?如果否,那么您只需重新声明您的var,并在一个var中设置值,然后检查另一个。可能是条件接收了顶部声明的未分配变量“response”,而没有考虑用户类型?可能是==不能用于比较字符串您可以在do
之前声明String response
。在Java中,您无法将字符串与==
进行比较,我尝试过这样做,但即使用户出于某种原因键入“Y”或“Y”,程序也会停止。您是否删除了response=keyboard.nextLine()之前的“String”字代码>?如果否,那么您只需重新声明您的var,并在一个var中设置值,然后检查另一个。可能是条件接收了顶部声明的未分配变量“response”,而没有考虑用户类型?可能是==不能用于比较字符串您可以在do
之前声明String response
。在Java中,您无法将字符串与==
进行比较,我尝试过这样做,但即使用户出于某种原因键入“Y”或“Y”,程序也会停止。您是否删除了response=keyboard.nextLine()之前的“String”字代码>?如果否,那么您只需重新声明您的var,并在一个var中设置值,然后检查另一个。可能是条件接受了未赋值变量“response”d吗