简体   繁体   English

循环永远持续下去

[英]Loop keeps going on forever

I'm having a bit of trouble with my code. 我的代码有点麻烦。 I'm trying to get the user name and password from the user and read a file that contains multiple user names and passwords. 我正在尝试从用户那里获取用户名和密码,并读取包含多个用户名和密码的文件。 If the username and password are not on the same line, the user has 2 more attempts to get it right, otherwise, the program will exit. 如果用户名和密码不在同一行,则用户将再尝试2次以使其正确无误,否则,程序将退出。 At the moment, the code is saying the username and password are wrong even when the user enters the right information, also, it is prompting the user more than 3 times. 目前,该代码表明即使用户输入正确的信息,用户名和密码也是错误的,并且提示用户3次以上。 Could someone please help? 有人可以帮忙吗? I have the code below. 我有下面的代码。 I didn't post the headings because I think it might be a little confusing. 我没有发布标题,因为我认为这可能会有些混乱。 Thank you! 谢谢!

//*****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();
            }
          }
        }

You while (userInfoScanner.hasNext()) loop depends on user input, but user can write something always, so this while loop will be forever. while (userInfoScanner.hasNext())循环取决于用户输入,但用户可以始终编写某些内容,因此,此while循环将永远存在。

You can write more briefly. 您可以写得更简短一些。

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM