有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java编写密码、PIN和PUK程序

我试图创建一个程序,提示用户输入正确的密码。第三次未正确输入密码时,程序应向用户询问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.
    }
}

共 (3) 个答案

  1. # 1 楼答案

    请尝试以下方法:

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

    输出1:

    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!
    

    输出2:

    Enter Pin Code: 324234
    PIN is incorrect! Try again!
    123
    Welcome!
    

    输出3:

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

    然后在if语句中使用以下命令:

    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!");
                }
                }
    
  2. # 2 楼答案

    public class PinPUK {
        public static void main(String[] a) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Enter Pin Code: ");
        int tries = 0;
        boolean correctPin = false;
        while(!correctPin) {
            //It is better to scan the next line and parse the integer
            int choice  = Integer.parseInt(keyboard.nextLine());
            if (choice == 123) {
               System.out.println("Welcome!"); 
               correctPin = true;   
            }
            else if (tries < 3{
                System.out.println("Password is incorrect! Try again!"); 
           }    
           else if (tries >= 3)
              System.out.println("SIM blocked");
           }
       }
    }
    
  3. # 3 楼答案

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

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