如何在Java中使用Scanner和条件?

时间:2016-12-11 01:01:20

标签: java input

我对java很新,现在我正在测试Scanner!这是我的代码:

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!!!!");
            }   
        }
    }   
}        

我想要用户的名字,密码就是这样!但是我注意到,即使我按下回车键,当我问这个名字时,它会马上问我密码。所以我想确保得到一个名字。此外,当我问密码时,我想在告别之前给他几点机会。

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

这是我在控制台上获得的。

谢谢!

3 个答案:

答案 0 :(得分:2)

首先,您已声明了不必要的scanner个对象。您只需要生成scanner个对象一次。您可以根据需要多次使用该对象进行输入。

第二件事是你需要删除while (password2==password);末尾的分号,因为无论while循环的条件是否为真,放置semi-colon都会执行while循环块是的。

现在要做你想做的事,试试下面的代码。

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 !!");
}

}

答案 1 :(得分:0)

您不需要两台扫描仪,因为它适用于userName和Pass字。 Do-While在这里运作正常。你一直在询问有效的密码。 更新了第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);
    }   
}        

另外,你可以这样做。如果您一次又一次不想要"您的密码是什么。

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

计数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!");
        }
    }
}

有效进入后没有再见:

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!");
            }
        }
    }
}

答案 2 :(得分:-1)

试试这个:

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");
}