【问题标题】:Loop keeps going on forever循环永远持续下去
【发布时间】:2017-11-05 20:27:06
【问题描述】:

我的代码有点问题。我试图从用户那里获取用户名和密码,并读取一个包含多个用户名和密码的文件。如果用户名和密码不在同一行,用户还有 2 次尝试才能正确,否则程序将退出。目前,即使用户输入正确的信息,代码仍然显示用户名和密码错误,并且提示用户3次以上。有人可以帮忙吗?我有下面的代码。我没有发布标题,因为我认为它可能有点混乱。谢谢!

//*****Prompts user to enter in user name and password
    System.out.println("Please enter in your user name: ");
    userName = keyboard.nextLine();
    System.out.println("Please enter in your password: ");
    password = keyboard.nextLine();
    while (userName.equals("done"))
    {
      System.out.println("You entered done. Now exiting the test");
      System.exit(0);
    }

    while (userInfoScanner.hasNext())
        {
          String userInfoLine = userInfoScanner.nextLine();
          String [] userInfoArr = userInfoLine.split ("\t");
          // for (int i=0; i<3; i++)
          //{ 
          if(userInfoArr[0].equalsIgnoreCase(userName) && userInfoArr[1].equals(password))
          {
            System.out.println("Correct");
          }
          while (!userInfoArr[0].equalsIgnoreCase(userName) && !userInfoArr[1].equals(password))
          {
            for (int i=0; i<3; i++)
            {
              System.out.println();
              System.out.println("Incorrect. Please enter in the correct user name: ");
              userName = keyboard.nextLine();
              System.out.println("Please enter in the correct password: ");
              password = keyboard.nextLine();
            }
          }
        }

【问题讨论】:

  • 你有三个嵌套循环。我很确定这太过分了。此外,您在最里面的循环中提示用户输入用户名和密码。我本来希望这是在最外层循环中。
  • 不,我在循环外提示了用户。我应该把它放在代码中。对不起。我刚刚编辑了代码。 @图灵85
  • 仍然:如果你进入最里面的for-loop,你将无条件地执行3次,无需进一步检查。我很确定这不是你想要的。尝试重新安排程序,以便在最外层循环和仅最外层循环内提示用户输入用户名和密码(do ... while(...) 循环可能有用)。
  • 所以我应该把我在 do-while 循环中第一次提示用户的地方放在哪里?
  • 等等,你能再解释一下吗?以不同的方式?我有点困惑@Turing85

标签: java loops for-loop file-io while-loop


【解决方案1】:

while (userInfoScanner.hasNext())循环依赖于用户输入,但用户总是可以写东西,所以这个while循环将永远存在。

你可以写得更简洁些。

Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter in your user name: ");
        String userName = keyboard.nextLine();
        System.out.println("Please enter in your password: ");
        String password = keyboard.nextLine();

        while (userName.equals("done"))
        {
            System.out.println("You entered done. Now exiting the test");
            System.exit(0);
        }

        System.out.println("Enter login and password");
        String userInfoLine = keyboard.nextLine();
        String [] userInfoArr = userInfoLine.split (" "); // i changed separation from \t to simple space

        if(userInfoArr[0].equalsIgnoreCase(userName) && userInfoArr[1].equals(password))
        {
            System.out.println("Correct");
        }
        while (!userInfoArr[0].equalsIgnoreCase(userName) && !userInfoArr[1].equals(password))
        {

            System.out.println();
            System.out.println("Incorrect. Please enter in the correct user name: ");
            userName = keyboard.nextLine();
            System.out.println("Please enter in the correct password: ");
            password = keyboard.nextLine();

        }

【讨论】:

  • 对不起,我不太明白你想说什么?
猜你喜欢
  • 1970-01-01
  • 2019-03-23
  • 2018-10-09
  • 1970-01-01
  • 2022-06-15
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多