【问题标题】:Scope of variable inside do-while loopdo-while循环内的变量范围
【发布时间】:2016-01-24 07:22:18
【问题描述】:

如果用户在程序结束时提示输入“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");


}

【问题讨论】:

  • 您可以在do 之前声明String response。而且你不能用Java中的==比较字符串,它不起作用
  • 我试过了,但即使用户出于某种原因输入“Y”或“y”,程序也会停止
  • 您是否删除了response = keyboard.nextLine(); 之前的“字符串”字样?如果不是,那么您只需重新声明您的 var,然后在一个 var 中设置值,然后检查另一个。
  • 是的,我从 do-while 循环内的响应中删除了字符串。会不会是条件接受了顶部声明的未赋值变量“response”,甚至没有考虑用户输入的内容?
  • 可能是==不能用来比较字符串

标签: java if-statement while-loop scope do-while


【解决方案1】:

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

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

【讨论】:

    【解决方案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;
    }
    

    【讨论】:

      【解决方案3】:

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

      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】:

        只需在 do-while 循环之外声明“响应”变量,如下所示:

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

        【讨论】:

          猜你喜欢
          • 2011-12-25
          • 2013-02-25
          • 1970-01-01
          • 1970-01-01
          • 2013-08-14
          • 1970-01-01
          • 1970-01-01
          • 2023-03-31
          • 1970-01-01
          相关资源
          最近更新 更多