【问题标题】:How to use variable in a while loop outside of the loop in java?java - 如何在java循环之外的while循环中使用变量?
【发布时间】:2015-05-02 07:25:48
【问题描述】:

我在 while 循环中设置了一个变量,因为它正在从文件中读取。我需要从循环外部访问和使用代码,因为我在 if 语句中使用变量,并且 if 语句不能在 while 循环中,否则它将重复多次。这是我的代码。

 BufferedReader br = null;

            try {

                String sCurrentLine;

                br = new BufferedReader(new FileReader("C:\\Users\\Brandon\\Desktop\\" + Uname + ".txt"));

                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);

                }if(sCurrentLine.contains(pwd)){System.out.println("password accepted");}

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }

【问题讨论】:

  • 阅读 java 规范 - 第 6.4 节指出 here
  • 在需要访问的上下文中声明变量...
  • 您已经在 while 循环之外声明了 sCurrentLine。你想要达到的行为是什么?如目前所写,您只测试sCurrentLine 是否包含pwd,而它必须是null。您还希望它打印出文件的每一行吗?你只是想记住pwd是在循环结束后找到的吗?

标签: java loops variables while-loop


【解决方案1】:

将 if 语句放在 for 循环中,但使用 break:

while...
    if(sCurrentLine.contains(pwd)){
        System.out.println("password accepted");
        break;
    }

这打破了 for 循环,因此一旦找到密码,它就会停止循环。您不能真正将 if-check 移到循环之外,因为您想在找到密码之前检查每一行,对吗?

如果这样做,则无需将 sCurrentLine 变量移出循环。如果要使用 sCurrentLine.equals(pwd) 而不是使用 contains,您可能还需要仔细检查。

【讨论】:

    【解决方案2】:

    您已经在 while 循环之外声明了 sCurrentLine。问题是您在下一行中一次又一次地使用它。如果您仍然希望它打印文件并且您尝试做的是记住找到密码或找到密码的行,请尝试以下操作:

        BufferedReader br = null;
        boolean pwdFound = false;
        String pwdLine = "";
    
    
            try {
    
                String sCurrentLine;
    
                br = new BufferedReader(new FileReader("C:\\Users\\Brandon\\Desktop\\" + Uname + ".txt"));
    
                while ((sCurrentLine = br.readLine()) != null) {
                    System.out.println(sCurrentLine);
                    if(sCurrentLine.contains(pwd)){
                        System.out.println("password accepted");
                        pwdFound = true;
                        pwdLine = sCurrentLine;
                    }
                }
    
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null)br.close();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
    

    【讨论】:

      【解决方案3】:

      布尔标志 = 假; while ((sCurrentLine = br.readLine()) != null) {

         if(sCurrentLine.contains(pwd))
         {
            flag = true;
            break;
         }
      

      } if(flag){System.out.println("密码接受");}

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 2015-10-09
        • 2012-10-23
        • 1970-01-01
        • 2014-03-29
        • 2010-11-26
        • 1970-01-01
        相关资源
        最近更新 更多