do-while循环中变量的范围

时间:2015-10-24 23:59:31

标签: 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 {
    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");


}

4 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:2)

@ergonaut的回答是解决您确切问题的最佳解决方案:

  

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

     

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

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:-1)

  

只需声明"响应"在do-while循环之外的varialbe,如下所示:

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


}