我试图创建一个程序,将提示用户输入正确的密码。第三次输入密码不正确时,程序应向用户询问PIN,如果用户仍未能正确输入PUK 3次尝试,程序现在应打印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.
}
}发布于 2015-09-12 17:43:00
这可以通过使用以下代码片段来完成:
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");
}发布于 2015-09-12 17:44:48
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");
}
}
}https://stackoverflow.com/questions/32541756
复制相似问题