【问题标题】:Java: While Loop for entering username and password 3 times - Issue with break statement?Java:用于输入用户名和密码 3 次的 While 循环 - 中断语句有问题吗?
【发布时间】:2018-02-22 07:08:47
【问题描述】:

我正在做一个课堂作业,想创建一个循环,让用户有 3 次登录机会,输入正确的用户名和密码。

  • 成功后,提示“您现在已登录”

  • 1-2次输入错误,提示“输入错误,重试(...)”,然后提示再次输入用户名和密码

  • 输入错误3次,提示“输入错误3次(...)”,然后退出系统

这是我的代码的一部分:

System.out.println("Enter password");
String inputPassword = input.next();

int count = 0;

//create while loop, set loop continuation condition to count < 3
while (count <= 2) {            

    if ((!inputUsername.equals(userName)) || (!inputPassword.equals(password))) {       
        System.out.println("Wrong entry. try again: Enter username");
        inputUsername = input.next();

        System.out.println("Enter password");
        inputPassword = input.next();
    }   
    else
        System.out.println("You are now logged in");    

    count++;

    break;  
}

if (count > 2)
 System.out.println("You have enterede wrong three times. Please try again in a few hours");
 System.exit(0);

知道为什么系统在 2 次尝试失败后退出而没有给我错误消息“您输入了 3 次错误 (...)”吗?

我认为问题在于增加计数后的“中断”,但不确定。

如果没有这个中断,如果成功进入,控制台会显示“你现在登录”3次,然后是错误消息“你已经输入了3次错误(...)”——这就是为什么我把它.

【问题讨论】:

    标签: while-loop break


    【解决方案1】:

    您的break 是无条件的。它将始终运行,这意味着您的循环将始终在第一次迭代时退出。

    前两次失败的尝试都发生在第一次迭代之前和期间,然后您点击break 并退出。由于您从未循环过,count 在中断之前只增加了一次。

    我相信您打算使用花括号将break 分组到else 下。现在,由于您没有使用花括号,因此只有println 语句是else 的一部分。 永远不要省略花括号

    【讨论】:

      【解决方案2】:

      感谢致癌!

      有道理!

      我同时找到了解决方案。我已经更改了延续条件并且它有效!

      int count = 0;
      
      //create while loop, set loop continuation condition to count <2
      while (count < 2 && ((!inputUsername.equals(userName)) || (!inputPassword.equals(password)))) {
          System.out.println("Wrong entry. try again: Enter username");
          inputUsername = input.next();
      
          System.out.println("Enter password");
          inputPassword = input.next();
          count++;    
      }   
      if ((inputUsername.equals(userName)) && (inputPassword.equals(password)))
          System.out.println("You are now logged in");
      else
          System.out.println("You have enterede wrong three times. Please try again in a few hours");
      
          System.exit(0);
      

      再次感谢!

      【讨论】:

        【解决方案3】:

        你的“休息”需要移动。

        System.out.println("Enter password");
        String inputPassword = input.next();
        
        int count = 0;
        
        //create while loop, set loop continuation condition to count < 3
        while (count <= 2) {            
        
            if ((!inputUsername.equals(userName)) || (!inputPassword.equals(password))) {       
                System.out.println("Wrong entry. try again: Enter username");
                inputUsername = input.next();
        
                System.out.println("Enter password");
                inputPassword = input.next();
            }   
            else {
                System.out.println("You are now logged in");
                break;
            }            
        
            count++;
        }
        
        if (count > 2)
            System.out.println("You have entered wrong three times. Please try again in a few hours");
        
        System.exit(0);
        

        【讨论】:

          【解决方案4】:
          System.out.println("Ghiane027");
          String inputPassword = input.next();
          
          int count = 0;
          
          //create while loop, set loop continuation condition to count < 3
          while (count <= 2) {            
          
              if ((!inputUsername.equals(Liezel Saleng)) || (!inputPassword.equals(Ghiane027))) {       
                  System.out.println("Wrong entry. try again: Enter username");
                  inputUsername = input.next();
          
                  System.out.println("Ghiane027");
                  inputPassword = input.next();
              }   
              else {
                  System.out.println("You are now logged in");
                  break;
              }            
          
              count++;
          }
          
          if (count > 2)
              System.out.println("You have entered wrong three times. Please try again in a few hours");
          
          System.exit(0);
          

          【讨论】:

          • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
          猜你喜欢
          • 1970-01-01
          • 2020-07-07
          • 2021-01-17
          • 1970-01-01
          • 2013-09-07
          • 1970-01-01
          • 1970-01-01
          • 2018-05-27
          • 2021-10-14
          相关资源
          最近更新 更多