gpt4 book ai didi

java - 编写密码、PIN 和 PUK 程序

转载 作者:行者123 更新时间:2023-11-30 07:56:56 24 4
gpt4 key购买 nike

我正在尝试创建一个程序来提示用户输入正确的密码。第三次未正确输入密码时,程序应询问用户 PIN,如果用户连续 3 次尝试仍无法正确输入 PUK,则程序现在应打印 SIM BLOCKED。

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

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.
}
}

最佳答案

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

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

关于java - 编写密码、PIN 和 PUK 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com