gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:25:11 24 4
gpt4 key购买 nike

我创建了一个关于 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com