首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码、密码和密码的toWriting程序

密码、密码和密码的toWriting程序
EN

Stack Overflow用户
提问于 2015-09-12 17:35:15
回答 2查看 10.9K关注 0票数 1

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

我想我得用循环,但我不知道怎么做。我只是个新手。

代码语言:javascript
复制
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.
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-09-12 17:43:00

这可以通过使用以下代码片段来完成:

代码语言:javascript
复制
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");
}
票数 0
EN

Stack Overflow用户

发布于 2015-09-12 17:44:48

代码语言:javascript
复制
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");
       }
   }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32541756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档