Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 为密码、PIN和PUK编写程序_Java_Loops - Fatal编程技术网

Java 为密码、PIN和PUK编写程序

Java 为密码、PIN和PUK编写程序,java,loops,Java,Loops,我试图创建一个程序,提示用户输入正确的密码。第三次未正确输入密码时,程序应向用户询问PIN,如果用户仍有3次未能正确输入PUK,则程序现在应打印SIM卡 我想我必须使用循环,但我不知道如何使用。我只是个新手 import java.util.Scanner; import java.io.*; public class PinPUK { public static void main(String[] a) { Scanner keyboard = new Scanner

我试图创建一个程序,提示用户输入正确的密码。第三次未正确输入密码时,程序应向用户询问PIN,如果用户仍有3次未能正确输入PUK,则程序现在应打印SIM卡

我想我必须使用循环,但我不知道如何使用。我只是个新手

import java.util.Scanner;
import java.io.*;
    public class PinPUK {
    public static void main(String[] a) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Enter Pin Code: ");
    int choice  = keyboard.nextInt();
    if (choice == 123) {
        System.out.println("Welcome!");    
        }
    else {
        System.out.println("Password is incorrect! Try again!"); // This is the 1st time the wrong password has been entered.
    }                                                           // 2 more and the program should ask for the PIN 3 times if incorrectly entered, and program should ask the PUK 3 times if it is incorrect, the program should now print SIM BLOCKED.
    }
}

可以使用以下代码段完成此操作:

int count = 0;
while(count<3) {
    if (choice == 123) {
        System.out.println("Welcome!"); 
        break;   //break from the loop
    }
    else {
        System.out.println("Password is incorrect! Try again!");
    }
    count++;
}

if(count == 3) {
     System.out.println("SIM BLOCKED");
}
int count=0;
而(计数<代码>公共类平铺){
公共静态void main(字符串[]a){
扫描仪键盘=新扫描仪(System.in);
系统输出打印(“输入Pin码:”);
int=0;
布尔校正pin=false;
而(!correctPin){
//最好扫描下一行并解析整数
int choice=Integer.parseInt(keyboard.nextLine());
如果(选项==123){
System.out.println(“欢迎!”);
correctPin=true;
}
否则,如果(尝试<3{
System.out.println(“密码不正确!请重试!”);
}    
else if(尝试次数>=3)
System.out.println(“SIM卡被阻止”);
}
}
}
在main中尝试以下操作:

int attemps = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Pin Code: ");
int PIN = 0;
int PUK = 0;
int CORRECT_PIN = 123;
int CORRECT_PUK = 1234;
while(PIN != CORRECT_PIN && attemps < 3)
        {
            PIN  = keyboard.nextInt();
            attemps++;
            if (PIN != CORRECT_PIN && attemps < 3) { 
               System.out.println("PIN is incorrect! Try again!" ); // This is the 1st time the wrong password has been entered.
            }
        }
        if (PIN == CORRECT_PIN && attemps <= 3) {  
            System.out.println("Welcome!");  
        }
         else {
           System.out.println("PIN is incorrect! Try again with PUK");
           attemps = 0;
           while(PUK != CORRECT_PUK && attemps < 3)
           {
            PUK  = keyboard.nextInt();
            attemps++;
            if (PUK != CORRECT_PUK && attemps < 3) { 
               System.out.println("PUK is incorrect! Try again!"); // This is the 1st time the wrong password has been entered.
            }
           }
        if (PUK == CORRECT_PUK && attemps <= 3) {  
            System.out.println("Welcome!");  
        }
        else
        {
           System.out.println("PUK is incorrect! SIM Blocked! See you!");
        }
        }
输出2:

Enter Pin Code: 33 
PIN is incorrect! Try again!
3333
PIN is incorrect! Try again!
33333
PIN is incorrect! Try again with PUK
3333
PUK is incorrect! Try again!
333
PUK is incorrect! Try again!
333
PUK is incorrect! SIM Blocked! See you!
Enter Pin Code: 324234
PIN is incorrect! Try again!
123
Welcome!
Enter Pin Code: 4354
PIN is incorrect! Try again!
345
PIN is incorrect! Try again!
3455
PIN is incorrect! Try again with PUK
1234
Welcome!
输出3:

Enter Pin Code: 33 
PIN is incorrect! Try again!
3333
PIN is incorrect! Try again!
33333
PIN is incorrect! Try again with PUK
3333
PUK is incorrect! Try again!
333
PUK is incorrect! Try again!
333
PUK is incorrect! SIM Blocked! See you!
Enter Pin Code: 324234
PIN is incorrect! Try again!
123
Welcome!
Enter Pin Code: 4354
PIN is incorrect! Try again!
345
PIN is incorrect! Try again!
3455
PIN is incorrect! Try again with PUK
1234
Welcome!
如果要将PIN与0进行比较,请使用以下选项:

String PIN = null;
String CORRECT_PIN = "0123";
do{
        PIN  = keyboard.next();
        attemps++;
        if (!PIN.equals(CORRECT_PIN) && attemps < 3) 
            { 
               System.out.println("PIN is incorrect! Try again!" );
            }
     }while(!PIN.equals(CORRECT_PIN) && attemps < 3);
PIN.equals(CORRECT_PIN)
而不是

PIN == CORRECT_PIN
完整代码如下:

int attemps = 0;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter Pin Code: ");
String PUK = null;
String PIN = null;
String CORRECT_PIN = "0123";
String CORRECT_PUK = "01234";
do{
        PIN  = keyboard.next();
        attemps++;
        if (!PIN.equals(CORRECT_PIN) && attemps < 3) 
            { 
               System.out.println("PIN is incorrect! Try again!" );
            }
     }while(!PIN.equals(CORRECT_PIN) && attemps < 3);
            if (PIN.equals(CORRECT_PIN) && attemps <= 3) {  
                System.out.println("Welcome!");  
            }
             else {
               System.out.println("PIN is incorrect! Try again with PUK");
               attemps = 0;
            do{
                PUK  = keyboard.next();
                attemps++;
                if (!PUK.equals(CORRECT_PUK) && attemps < 3) 
                    { 
                       System.out.println("PIN is incorrect! Try again!" );
                    }
             }while(!PUK.equals(CORRECT_PUK) && attemps < 3);
            if (PUK.equals(CORRECT_PUK) && attemps <= 3) {  
                System.out.println("Welcome!");  
            }
            else
            {
               System.out.println("PUK is incorrect! SIM Blocked! See you!");
            }
            }
int attemps=0;
扫描仪键盘=新扫描仪(System.in);
系统输出打印(“输入Pin码:”);
字符串PUK=null;
字符串PIN=null;
字符串正确_PIN=“0123”;
字符串正确_PUK=“01234”;
做{
PIN=键盘。next();
attemps++;
如果(!引脚等于(正确的引脚)&&attemps<3)
{ 
System.out.println(“PIN不正确!请重试!”);
}
}而(!PIN.equals(CORRECT_PIN)&&attemps<3);

如果(PIN.equals(CORRECT_PIN)&&attemps谢谢!它可以工作,但是尝试应该包括用户第一次键入PIN,无论如何,我已经将3更改为2,与PUK尝试次数相同。并且,您可以删除“尝试”吗我还在琢磨如何在你提供的源代码中删除如果你想控制转发的引脚0,你应该比较字符串而不是整数@TristanCoal CrescentI我很困惑,字符串和int,我无法将它们分开huhuI编辑它@TristanCoal Crescent如果你困惑我添加完整的代码哇!你真是个好人!谢谢你!言语不足以表达我的感激之情,但至少我可以通过这句话来部分表达……发自内心的感谢……这真的很有帮助♥♥♥