java - 数组输入时 Atm 机逻辑错误

标签 java arrays loops

我创建了一个关于 ATM 机的应用程序,其数组帐户大小为 10,但由于某些奇怪的原因,输入仅接受 0(第一个 id),但不接受其他 id 的其他 9 个 id,它总是显示无效 ID 错误消息。我花了几个小时寻找错误。

与检查 id 相关的是这两个;

public static boolean hasID(Account acc[], int id){ //this is to check whether the id exist or not.
    for(int i=0; i<acc.length; i++){
        if(id == acc[i].getID()){
            return true;
        }
    }
    return false;
}

public static int gID(Account[] acc){  //this is the login method to initiate the hasID method.
    Scanner sc = new Scanner(System.in);
    int id=0;
    boolean valid = false;
    while(!valid){
        System.out.println("Enter ID: ");
        id = sc.nextInt();

        if(!hasID(acc, id)){
            System.out.println("YOUR ID IS INVALID.");
        } else{
            valid = true;
        }
    }
    return id;
}

public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = getID();
    this.balance = getBalance();
    //this.interestRate = getInterestRate();
}

public static Account getAccount(Account acc[], int id){
    for(int i = 0; i<acc.length; i++){
        if(id == acc[i].getID()){
            return acc[i];
        }
    }
    return null;
}

这是我的主要内容:

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    Account acc[] = new Account[10];
    for(int i = 0; i<acc.length; i++){
        acc[i] = new Account(i, 100.0);
    }

    int a = 0;
    int id = gID(acc);

    while(a != 4){
        Account ac = getAccount(acc,id);
        System.out.println("1: Check Balance");
        System.out.println("2: Withdraw");
        System.out.println("3: Deposit");
        System.out.println("4: Exit");

        System.out.println("Enter your choice: ");
        a = sc.nextInt();
        switch(a){
        case 1:
            System.out.println("Your Balance is: RM " + ac.getBalance());
            break;

        case 2:
            System.out.println("Amount to withdraw: RM ");
            ac.withdraw(sc.nextDouble());
            break;

        case 3:
            System.out.println("Amount to deposit: RM ");
            ac.deposit(sc.nextDouble());
            break;

        case 4:
            id = gID(acc);
            a = 0;
            break;

        default:
            System.out.println("Invalid input!");
        }
    }

}

编辑:帐户构造函数,不使用 insterestrate。 id和balance的setter和getter:

public int getID(){
    return id;
}

public void setID(int mID){
    id = mID;
}

public double getBalance(){
    return balance;
}

public void setBalance(double mBalance){
    balance = mBalance;
}

感谢您的宝贵时间。

最佳答案

public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = getID();
    this.balance = getBalance();
    //this.interestRate = getInterestRate();
}

你不应该使用

 public Account(int mID, double mBalance/*, double mInterestRate*/){
    this.id = mID;
    this.balance = mBalance;
    //this.interestRate = getInterestRate();
 } 

getter 和 setter 是字段封装的一部分,但为了更好的用户体验,我建议您使用以下代码

public Account(/*you dont need a parameter*/){
   this.id = setIdByScannedValue();
   this.balance = setBalanceByScannedValue();
   //this.interestRate = setInterestRateByScannedValue();
}

private void setIdByScannedValue(){
    Scanner sc = new Scanner(System.in);
    boolean provided = false;
    while(!provided)
    try{
        System.out.print("provide id: ");
        this.id = sc.nextInt();
    }catch(NumberFormatException e){
        System.out.println("you must provide an integer id format !!!");
    }finally{
        provided = true;
    }
}

我会让您执行其他方法,以便您可以自己学习使用这个想法,这样我将确保您理解我的代码。

关于java - 数组输入时 Atm 机逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47314371/

相关文章:

ios - 将 Swift 的 Structs 数组转换为 NSArray 的问题,反之亦然以与 NSUserDefaults 一起使用

Python:降低复杂性的建议

java - 在 clojure 中使用第三方 java 库,例如 com.jcraft.jsch

Java 多个字符串返回

c++ - C/C++ 将字符串放入二维数组?

Java动态while表达式

excel - 如何使用VBA将excel中的单元格值循环到网站文本框中?

java - Android - MPAndroid 图表显示 x 轴值

javascript - jQuery 日期选择器和自动完成功能结合在一起

c# - 如何检查数组是否存在?