有 Java 编程相关的问题?

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

java中的ATM pin验证

我对编程非常陌生,需要一些帮助。我需要写一个简单的程序来验证ATM用户的pin码。该程序要么接受pin并退出,告诉用户pin不正确,并让他们最多重试三次,要么告诉用户他们的卡被锁定,因为他们错误了三次。我已经搜索了一个多小时,找不到这样的例子。我知道我需要一个扫描仪和一个回路来完成这项工作,但除此之外就不需要太多了。任何帮助都将在午夜前到期,我们将不胜感激


共 (3) 个答案

  1. # 1 楼答案

    import java.util.Arrays;
    import java.io.Console;  
    
    public class atm {
    
        public static void main(String[] args) {
    
            int counter = 3;
            int attempt = 3;
            char[] ch = null;
            Console c=System.console();    
            System.out.println("Enter PIN: ");    
            ch=c.readPassword();    
                String pass=String.valueOf(ch);
                if(pass.equals("enigma")){
                    System.out.println("Correct PIN entered!");
                }
            while(!pass.equals("enigma") && attempt != 0){
                System.out.println("Invalid PIN entered!. " +  attempt + " attempts remaining.");  
                counter ;
                if(attempt != 0){
                    c=System.console();
                    System.out.println("Enter PIN: ");
                    ch=c.readPassword(); 
                    pass=String.valueOf(ch);
                    if(pass.equals("enigma")){
                        System.out.println("Correct PIN entered!");
                    }
                }
                else{
                    System.out.println("your card has locked!");
                    break;
                }
            }  
        }
    }
    

    请注意,您可以输入PIN码,而不是“谜”字符串,我希望这会有所帮助

  2. # 2 楼答案

    下面是上面@Jared回答的伪代码的实现

    boolean cardLockFlag = false ;
    String password;
    Scanner scan = new Scanner (System.in);
    password = "password" ;
    if(!cardLockFlag){
    for(int i = 0 ; i < 3 ; i++){
       if(password.equals(scan.next().trim())){
     System.out.println("Success :) ");
     break ;
    
    }else{
    if(i==2){
    cardLockFlag = true ;
    }else{
    System.out.println("Wrong Password");
    }
    }
    }
    
    }else{
    System.out.println("Card is Locked");
    }
    
  3. # 3 楼答案

    for i = 1..3
        prompt user for pin
        read pin
        check pin
        if pin is correct, exit
        tell user they were wrong and try again
    
    tell user they got it incorrect three times and their card is locked.
    

    我将给出一个可能会绊倒一些新手的提示。pin是一个整数,对吗?因此,您可能会尝试使用Scanner.nextInt()来获取输入不要那样做!只需获取下一行并比较字符串(您可能需要使用String.trim()来去除空白)。如果您尝试使用Scanner.nextInt()(如果用户输入了无法解析为整数的内容,该怎么办)