我正在尝试创建一个程序,提示用户输入正确的密码。第三次没有正确输入密码时,程序应该询问用户PIN,如果用户仍然无法正确输入PUK 3次,程序现在应该打印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.
}
}
答案 0 :(得分:1)
在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!");
}
}
答案 1 :(得分:0)
可以使用以下代码段完成:
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");
}
答案 2 :(得分:0)
.block {
width: 90%;
margin: 0 auto;
}