简体   繁体   English

如何在Java中使用扫描仪和条件?

[英]How to use Scanner and conditions in Java?

I'm fairly new to java and now I'm testing the Scanner ! 我对Java很陌生,现在正在测试Scanner Here is my code: 这是我的代码:

package Examples;

import java.util.Scanner;

public class UserLogin
{
    public static void main(String[] args)
    {
        Scanner in= new Scanner (System.in);
        System.out.println("Please enter your name!");
        String keyboard= in.nextLine();    

        System.out.println("Welcome: " + keyboard);
        System.out.println("What is your password?");

        Scanner x= new Scanner (System.in);
        int password= x.nextInt();


        if(password ==14567) 
        {
            System.out.println("Password accepted ! you are welcome!");
        }
        else
        {
            System.out.println("Sorry wrong password! Try again");
            Scanner input = new Scanner(System.in);
            int password2=input.nextInt();
            while (password2==password);
            {
                System.out.println("Welcome!!!!");
            }   
        }
    }   
}        

I want the user's name, password and that is it! 我想要用户名,密码,就是这样! But I noticed that when I ask the name even if I press the enter key, it will ask me the password right away. 但是我注意到,即使我按Enter键询问姓名,它也会立即询问我密码。 So I want to make sure I get a name. 所以我想确保我得到一个名字。 Also when I ask the password I want to be able to give him few chances before saying goodbye. 另外,当我询问密码时,我希望能够少给他机会说再见。

Please enter your name!
michel
Welcome: michel
What is your password?
12345
Sorry wrong password! Try again
12345

This is what I get on the console. 这就是我在控制台上得到的。

Thanks! 谢谢!

First things first, You have declared unnecessary scanner objects. 首先,您已经声明了不必要的scanner对象。 You only need to make scanner object once. 您只需要使scanner对象一次。 You can take input using that object as many times as you want. 您可以根据需要使用该对象进行多次输入。

Second thing is you need to remove semi-colon you have put at the end of while (password2==password); 第二件事是您需要删除while (password2==password);末尾放置的分号while (password2==password); because putting a semi-colon there will execute the block of while loop regardless of whether condition of while loop is true of false. 因为放入semi-colon将执行while循环块,而不管while循环的条件是否为false。

Now to do what you want, try following code. 现在要做您想做的,尝试下面的代码。

public static void main(String[] args) {
 Scanner in= new Scanner (System.in);
 int default_password = 14567;
 int counter = 3;

System.out.println("Please enter your name!");
String keyboard= in.nextLine();    

System.out.println("Welcome: " + keyboard);
System.out.println("What is your password?");

int password= in.nextInt();

if(password == default_password) 
{
    System.out.println("Password accepted ! you are welcome!");

}
else
{
    while(counter > 0)
    {
      System.out.println("Sorry wrong password! Try again");

       password = in.nextInt();

       if (password == default_password)
       {
         System.out.println("Welcome!!!!");
         break;
       }
       else
       {
        counter--;
       }  


    }
    if(counter== 0)
           System.out.println("Good Bye !!");
}

}

You don't need two Scanner since it would work for userName and Pass word. 您不需要两个Scanner,因为它适用于userName和Password。 Do-While works fine here. 做在这里工作正常。 You keep asking for valid pass word. 您一直在要求输入有效的密码。 Updated with count 3. 更新计数3。

package ab;
import java.util.Scanner;

public class UserLogin{
    public static void main(String[] args) {

        Scanner stdIn= new Scanner (System.in);

        String userName; 
        int userPassWord;

        boolean invaildPassWord = false;

        System.out.println("Please enter your name!");
        userName = stdIn.nextLine();    

        System.out.println("Welcome: " + userName);

        do { 
              System.out.println("\nWhat is your password?");
              userPassWord = stdIn.nextInt();

              if (userPassWord ==14567){
                  System.out.println("Password accepted ! you are welcome!");
                  invaildPassWord = false;
              }

              else {
                  System.out.println("Sorry wrong password! Try again");
                  invaildPassWord = true;

        }

        } while(invaildPassWord);
    }   
}        

Also, you can do it like this. 另外,您可以这样做。 If you don't want "what's your password again and again. 如果您不希望“密码一遍又一遍。

package ab;
import java.util.Scanner;

public class UserLogin{
    public static void main(String[] args) {

        Scanner stdIn= new Scanner (System.in);

        String userName; 
        int userPassWord;

        boolean invaildPassWord = false;

        System.out.println("Please enter your name!");
        userName = stdIn.nextLine();    

        System.out.println("Welcome: " + userName);

        System.out.println("\nWhat is your password?");
        userPassWord = stdIn.nextInt();

        do { 
              if (userPassWord ==14567){
                  System.out.println("Password accepted ! you are welcome!");
                  invaildPassWord = false;
              }

              else {
                  System.out.println("Sorry wrong password! Try again");
                  userPassWord = stdIn.nextInt();
                  invaildPassWord = true;

        }

        } while(invaildPassWord);
    }   
} 

With count 3 times: 计数3次:

package ab;

import java.util.Scanner;

public class UserLogin {
    public static void main(String[] args) {

        Scanner stdIn = new Scanner(System.in);

        String userName;
        int userPassWord;
        int count = 3;

        boolean invaildPassWord = false;

        System.out.println("Please enter your name!");
        userName = stdIn.nextLine();

        System.out.println("Welcome: " + userName);

        System.out.println("\nWhat is your password?");
        userPassWord = stdIn.nextInt();

        if (count > 0) {

            do {
                if (userPassWord == 14567) {
                    System.out.println("Password accepted ! you are welcome!");
                    invaildPassWord = false;
                }

                else {
                    --count;
                    System.out.println("Sorry wrong password! Try again");
                    userPassWord = stdIn.nextInt();
                    invaildPassWord = true;

                }

            } while (invaildPassWord && count > 0);

            System.out.println("GoodBye!");
        }
    }
}

Without goodbye after valid entry: 有效进入后无需再见:

import java.util.Scanner;

public class UserLogin {
    public static void main(String[] args) {

        Scanner stdIn = new Scanner(System.in);

        String userName;
        int userPassWord;
        int count = 3;

        boolean invaildPassWord = false;

        System.out.println("Please enter your name!");
        userName = stdIn.nextLine();

        System.out.println("Welcome: " + userName);

        System.out.println("\nWhat is your password?");
        userPassWord = stdIn.nextInt();

        if (count > 0) {

            do {
                if (userPassWord == 14567) {
                    System.out.println("Password accepted ! you are welcome!");
                    invaildPassWord = false;
                }

                else {
                    --count;
                    System.out.println("Sorry wrong password! Try again");
                    userPassWord = stdIn.nextInt();
                    invaildPassWord = true;

                }

            } while (invaildPassWord && count > 0);

            if (count <= 0){
                System.out.println("GoodBye!");
            }
        }
    }
}

try this: 尝试这个:

while true{
    Scanner x= new Scanner (System.in);
    int password= x.nextInt();
    if(password ==14567)
    {
        System.out.println("Password accepted ! you are welcome")
        break;

    }
    else
    {
        System.out.println("Sorry wrong password! Try again");
}

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

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