Java 代码中函数未被调用/抛出异常

问题描述 投票:0回答:1
import java.util.*;
public class oops{
    public static void main(String args[]){
        System.out.println("Enter the username you want to set the for your Bank Account");
        Scanner sc2=new Scanner(System.in);
        String user=sc2.nextLine();
        Bank_account ba=new Bank_account();
        ba.username=user;
        //ba.set_pw();----not possible directly so i created a class to authenticate.
        ba.authenticate();
        //ba.display_password();-----again not possible as it is also private.
        ba.show_password();
        sc2.close();


    }
}
class Bank_account{
    public String username;
    private int password;
    public int dig_lock=123;
    private void set_pw(int s){
        this.password=s;
    }




    public void authenticate(){
        int lock;
        int count=3;
        Scanner sc1=new Scanner(System.in);

        while(count>0){
            System.out.println("Enter the lock if want to set the password "+username);
            lock=sc1.nextInt();
            sc1.nextLine();
            boolean allow=(dig_lock==lock)?true:false;
            if(allow){
                System.out.println("Enter the paassword you want to set "+username);
                int s1=sc1.nextInt();
                set_pw(s1);
                break;
            }
            count--;
            System.out.println("Sorry try again to aunthenticate "+username);
        }
        if(count<=0){
            System.out.println("Failed to authenticate ,sorry you cant set the password "+username);
        }
        sc1.close();
    }




    public void show_password(){
        int count=3;
        int lock;
        Scanner sc=new Scanner(System.in);
       
        
        while(count>0){
            System.out.println("Enter the lock if u want to access the password "+username);
            lock=sc.nextInt();
            sc.nextLine();
            boolean allow=(dig_lock==lock)?true:false;
            if(allow){
                display_password();
                break;
            }
            count--;
            System.out.print("Sorry wrong passwrod Try again "+username);

        }
        if(count<=0){
            System.out.print("You have used ur all terns,sorry can't show password "+username);
        }
        sc.close();
    }
    private void display_password(){
        System.out.print(password);

    }


}

它是银行系统的代码,用户必须在设置和显示其密码之前通过输入应与 dig_lock 匹配的预定义锁进行身份验证

名为 set_pw 的函数在此代码中未按预期工作,并引发一些错误,并且在验证函数中,即使在输入正确的锁定后,它也应该打破循环,但它仍然要求用户输入锁定,但它应该中断并且只需调用 set_pw 请早点解决这个问题,我也问过这个问题,但没有回复。--------即使在 vscode 中也没有提到该错误,它只是在主线程中显示错误。请帮我解决这个问题。

java function oop while-loop break
1个回答
0
投票
        ...
49      if(count <= 0) {
50          System.out.println("Failed to authenticate ,sorry you cant set the password " + username);
51      }
52      sc1.close(); // Cause of the issue
53  }

...

您正在关闭扫描仪,这意味着您无法从

System.in
获取输入,这通常是一种不好的做法。 如果您有兴趣,可以阅读更多这里


此外,命名约定是一种很好的做法,可以使您的代码看起来不错。

© www.soinside.com 2019 - 2024. All rights reserved.