Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
密码检查器java程序_Java - Fatal编程技术网

密码检查器java程序

密码检查器java程序,java,Java,我试图创建一个程序,将检查用户的密码。我希望程序结束,一旦用户得到正确的,但如果没有,我希望它只要求4次 问题:即使你得到了正确的密码,程序也会不断地询问和猜测密码。如果你弄错了,它会问错。我怎样才能解决这个问题 import java.util.Scanner; public class HowToAdd { public static void main (String [] args){ Scanner input = new Scanner (System.in)

我试图创建一个程序,将检查用户的密码。我希望程序结束,一旦用户得到正确的,但如果没有,我希望它只要求4次

问题:即使你得到了正确的密码,程序也会不断地询问和猜测密码。如果你弄错了,它会问错。我怎样才能解决这个问题

import java.util.Scanner;
public class HowToAdd {
    public static void main (String [] args){
        Scanner input = new Scanner (System.in);
        int trys = 0;
        String password=null;
        do{
            System.out.println("Guess the password");
            password = input.next();

            if(password.equalsIgnoreCase("Password")){
                System.out.println("Great job");
            }else{
                System.out.println("Try again");
                input.next();
                trys++;
            }
        }while(trys<2);
        System.out.println("Try again later!");

        input.close();
    }
}
import java.util.Scanner;
公共类如何添加{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int-trys=0;
字符串密码=null;
做{
System.out.println(“猜密码”);
密码=输入。下一步();
if(password.equalsIgnoreCase(“password”)){
System.out.println(“很棒的工作”);
}否则{
System.out.println(“重试”);
input.next();
trys++;
}

}而(trys您只需要添加一个中断:

if(password.equalsIgnoreCase("Password")){
 System.out.println("Great job");
 break;
}

您只需添加一个中断:

if(password.equalsIgnoreCase("Password")){
 System.out.println("Great job");
 break;
}

我不仅添加了一个中断来解决输入正确密码时不离开循环的问题,而且还添加了一些其他东西来帮助您解决问题,如下所示:

 public static void main (String [] args){
    Scanner input = new Scanner (System.in);
    int trys = 0;
    String password=null;
    System.out.println("Guess the password:");
    while(trys<2)
    {
        password = input.next();

        if(password.equalsIgnoreCase("Password")){
            System.out.println("Great job");
            break;
        }else{
            trys++;
            if(trys != 2)
            {
                System.out.println("Try again:");
            }
        }
    }
    if(trys == 2)
    {
        System.out.println("Try again later!");
    }

    input.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int-trys=0;
字符串密码=null;
System.out.println(“猜密码:”);

虽然(trys我不仅添加了一个中断来解决在输入正确密码时不离开循环的问题,而且还添加了一些其他东西来帮助您解决问题,见下文:

 public static void main (String [] args){
    Scanner input = new Scanner (System.in);
    int trys = 0;
    String password=null;
    System.out.println("Guess the password:");
    while(trys<2)
    {
        password = input.next();

        if(password.equalsIgnoreCase("Password")){
            System.out.println("Great job");
            break;
        }else{
            trys++;
            if(trys != 2)
            {
                System.out.println("Try again:");
            }
        }
    }
    if(trys == 2)
    {
        System.out.println("Try again later!");
    }

    input.close();
}
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
int-trys=0;
字符串密码=null;
System.out.println(“猜密码:”);

while(trys您可以使用
break
关键字修复它,如下所示:

        if(password.equalsIgnoreCase("Password")){
            System.out.println("Great job");
            break;
        }
此处解释了
中断


您可以使用
break
关键字来修复它,如下所示:

        if(password.equalsIgnoreCase("Password")){
            System.out.println("Great job");
            break;
        }
此处解释了
中断


无需使用
break
语句即可实现所需的功能。 试着看看我的代码流

    Scanner scn = new Scanner(System.in);
    int tries=0;
    String pw = "";

    System.out.println("Guess the password");
    pw = scn.nextLine();

    while(!pw.equalsIgnoreCase("Password") && tries < 3){
        tries++;
        System.out.println("Try again");
        pw = scn.nextLine();            
    }

    if(tries >= 3)
        System.out.println("Try again later!");
    else
        System.out.println("Great job!");
测试运行2:

Guess the password
1st try
Try again
2nd try
Try again
3rd try
Try again
last try
Try again later!
Guess the password
password
Great job!

无需使用
break
语句即可实现所需的功能。 试着看看我的代码流

    Scanner scn = new Scanner(System.in);
    int tries=0;
    String pw = "";

    System.out.println("Guess the password");
    pw = scn.nextLine();

    while(!pw.equalsIgnoreCase("Password") && tries < 3){
        tries++;
        System.out.println("Try again");
        pw = scn.nextLine();            
    }

    if(tries >= 3)
        System.out.println("Try again later!");
    else
        System.out.println("Great job!");
测试运行2:

Guess the password
1st try
Try again
2nd try
Try again
3rd try
Try again
last try
Try again later!
Guess the password
password
Great job!

下面是代码的另一个变体:

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int trys = 0;
    String password = null;
    // create variable for a number of tries
    int numberOfCheks = 4;
    do {
        System.out.println("Guess the password");
        password = input.nextLine();

        if (password.equalsIgnoreCase("Password")) {
            System.out.println("Great job");
            break;
        } else if (++trys == numberOfCheks) {
           //Break if you've used all your tries 
            System.out.println("Try again later!");
            break;
        } else {
            System.out.println("Try again");
            // input.next(); - Useless. You don't handle it. Removed
        }
    //There is already tries number check. So 'true' can be used as the condition.
    } while (true); 
    input.close();
}

下面是代码的另一个变体:

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int trys = 0;
    String password = null;
    // create variable for a number of tries
    int numberOfCheks = 4;
    do {
        System.out.println("Guess the password");
        password = input.nextLine();

        if (password.equalsIgnoreCase("Password")) {
            System.out.println("Great job");
            break;
        } else if (++trys == numberOfCheks) {
           //Break if you've used all your tries 
            System.out.println("Try again later!");
            break;
        } else {
            System.out.println("Try again");
            // input.next(); - Useless. You don't handle it. Removed
        }
    //There is already tries number check. So 'true' can be used as the condition.
    } while (true); 
    input.close();
}


密码一般是区分大小写的。您应该考虑用<代码> >均衡器()来切换<代码> > />代码> <代码> > @ USS345 3661查看我的解决方案,它应该给您所需要的。密码一般都是区分大小写的。您应该考虑用<代码>(>代码)切换“代码> .QualAlgReReCasee())/<代码>。
@user3453661请看下面我的解决方案,它应该能满足您的需求。非常感谢。您运行它是因为我发现了一些问题吗?我的意思是它可以工作,但逻辑是正确的wrong@user3453661我编辑了它,看看它是否能修复你所说的。我想你的意思是因为它说再试一次,然后再试一次背对背这将检查是否已经有2次猜测,如果有,则不显示重试。非常感谢。您是否运行了它,因为我发现了一些错误,我的意思是它可以工作,但逻辑是正确的wrong@user3453661我编辑了它,试着看看它是否能修复你所说的。我想你的意思是因为它说再试一次n稍后再试背靠背这将检查是否已经有2次猜测,如果有,则不显示重试。减号-1,这是一个可怕的muy muyPLUS1 WONDFRULWONDFRULWONDFRULminus-1,这是一个可怕的muy muyPLUS1 wondfruwh WONDFRULWONDFRULWONDFRULminus-1,这个可怕的muy muy muyPLUS1 wondfruwh wondfruwh wondfrulwondfrulwondfrulwondfrulwondfile循环它将不断询问“猜测密码重试”这是不必要的,因为在程序结束时,当循环存在为false时,循环将保持说猜密码对不起,我不理解你的评论。这句话太长。如果你输入了错误的密码,这个循环可以工作4次。如果你的密码正确,循环将立即停止。你测试过吗?实际很抱歉,我再次测试了它,这一定是我在运行它时犯的错误。可能您尝试使用空格获取输入sting?
Scanner。next()
方法从该扫描仪返回下一个完整的标记。完整的标记前面和后面是与分隔符模式匹配的输入。已修复为
nextLine()
这有点问题,因为您将猜测密码放入do while循环中,它会不断询问“猜测密码重试”这是不必要的,因为在程序结束时,当循环存在为false时,循环将保持说猜密码对不起,我不理解你的评论。这句话太长。如果你输入了错误的密码,这个循环可以工作4次。如果你的密码正确,循环将立即停止。你测试过吗?实际很抱歉,我再次测试了它,这一定是我在运行它时犯的错误。可能您尝试使用空格获取输入sting?
Scanner。next()
方法从该扫描仪返回下一个完整的标记。完整的标记前面和后面是与分隔符模式匹配的输入。已修复为
nextLine()
这个程序也有问题,我一运行就很快发现了。猜密码你好,再试一次你好,再试一次你好,再试一次密码,再试一次!猜密码你好,再试一次你好,再试一次你好,再试一次密码,再试一次!Som这个程序也有问题,我一运行它就很快发现了猜密码你好再试一次你好再试一次密码再试一次!猜密码你好再试一次