繁体   English   中英

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

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

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

我想要用户名,密码,就是这样! 但是我注意到,即使我按Enter键询问姓名,它也会立即询问我密码。 所以我想确保我得到一个名字。 另外,当我询问密码时,我希望能够少给他机会说再见。

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

这就是我在控制台上得到的。

谢谢!

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

第二件事是您需要删除while (password2==password);末尾放置的分号while (password2==password); 因为放入semi-colon将执行while循环块,而不管while循环的条件是否为false。

现在要做您想做的,尝试下面的代码。

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

}

您不需要两个Scanner,因为它适用于userName和Password。 做在这里工作正常。 您一直在要求输入有效的密码。 更新计数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!");
            }
        }
    }
}

尝试这个:

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