gpt4 book ai didi

java - 在java中创建帐户类并输出

转载 作者:行者123 更新时间:2023-12-02 05:09:44 24 4
gpt4 key购买 nike

我正在尝试在 java 中创建一个帐户类。我的主要问题不在于类(class)本身,而在于输出。简而言之,我使用数组来存储银行帐户信息,然后在存入/提取一定金额后检查 id。我的问题是,当我尝试输入无效的 id 值(这是用于检查/搜索部分)时,我应该得到“未找到帐户”的输出,但是,出于某种原因,我最终得到了相同的五个输出行“找不到帐户”(可能必须对数组大小做一些事情,但我不太明白)。有没有办法让它只打印一次该行?

tl;dr:当输入无效 ID 时,我应该只得到一行“未找到帐户”,而是得到多行

import java.util.Scanner;

public class AccountDriver
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
int id =0;
double balance = 0;
double interest = 0;
Account[] accounts = new Account[5];
//this part fills in the array by asking the user information about each account
System.out.println("Enter the annual interest rate ");
interest = kb.nextDouble();
for (int i = 0; i < 5; i++)
{
System.out.println("Enter the account number: ");
id = kb.nextInt();
System.out.println("Enter the initial amount to deposit: ");
balance = kb.nextDouble();
Account a = new Account(id,balance,interest);
accounts[i] = a;
}
boolean repeat = true;
boolean found = false;
while(repeat)
{
System.out.println("\n\n*******************************");
System.out.println("Welcome to the BANK OF AMERICA");
System.out.println("\n\n*******************************");
System.out.println("Enter the account number to deposit, withdraw money :");
id = kb.nextInt();
int i = 0;
while (!found && i < 5)
{

if(id == accounts[i].getId())
{

System.out.println("Here is the account information currently:");
System.out.println(accounts[i]);
System.out.println("*******************");
System.out.println("Enter the amount of deposit: ");
double depositAmount = kb.nextDouble();
accounts[i].deposit(depositAmount);
System.out.println("Enter the amount to withdraw: ");
double withdrawAmount = kb.nextDouble();
if(!accounts[i].withdraw(withdrawAmount))
{
System.out.println("*******Not enough money in your account*********");
}
else
{
System.out.println("Here is the account information after your transaction:");
System.out.println(accounts[i]);
}
}
**//confused about this part**
else
{
System.out.println("Account not found");
}
i++;
}
//this asks the user if they have any other accounts to continue running the program
System.out.println("\nDo you have any other account?");
String answer = kb.next();
if(answer.equalsIgnoreCase("no"))
{
repeat = false;
}
}
}

}

如果有人需要的话,这是该类(class)

public class Account{

//These are instance variables
private static double annualInterestRate;
private int id;
private double balance;
//This creates an object from the date class in java
private java.util.Date dateCreated;

//This no argument constructor creates a default account
public Account()
{

dateCreated = new java.util.Date();
id = 0;
balance = 0;
annualInterestRate = 0;
}
//This constructor creates an account with
public Account(int newId, double newBalance, double newAnnualInterestRate)
{
//initialize the instance variables to the given values
dateCreated = new java.util.Date();
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;

}
//accessors (get)fill in the following methods
public int getId()
{
return id;
}
public double getBalance()
{
return balance;
}
public static double getAnnualInterestRate()
{
return annualInterestRate;
}
//mutators (get)
public void setId(int newId)
{
if (newId > 0)
{
id = newId;
}

}
public void setBalance(double newBalance)
{
if(newBalance >0)
{
newBalance = balance;
}

}
public static void setAnnualInterestRate(double newAnnualInterestRate)
{
if(newAnnualInterestRate > 0)
{
newAnnualInterestRate = annualInterestRate;
}

}
public double getMonthlyInterest()
{
double monthlyInterest = (balance * annualInterestRate/100 / 12);
return monthlyInterest;
}
public java.util.Date getDateCreated()
{
return dateCreated;
}
//this boolean method checks to see if the user has enough money to withdraw
public boolean withdraw(double amount)
{

if(amount > balance)
{
return false;
}
else
{
balance = balance - amount;
return true;
}
}
//this method returns the balance after the user enters an amount to deposit
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}
//this method compares two accounts
public boolean equals(Account a)
{
return this.id == a.id;
}
//this method outputs the account information for each account
public String toString()
{
String s = "";
s = s + "ID: " + id;
s = s + String.format("\nBalance: %.2f", balance);
s = s + "\nAnnual interest rate: " + annualInterestRate;
double value = getMonthlyInterest();
s = s + String.format( "\nmonthly interest: %.2f", value);
s = s + "\nDate account created: " + dateCreated;
return s;
}

}

最佳答案

在 System.out.println(); 之后休息一下

      else{
System.out.println("Account not found");

//this will break your loop;
break;
}

或设置您的foundtrue

      else{
System.out.println("Account not found");

//this will stop your inner while loop;
found = true;
}

已更新

我发现你的代码有很多错误。

修复#1:将您的 boolean found = false;在你的外环里面while(repeat)因为稍后您将在内部 while 循环中更改该 boolean 变量的值。动动你的boolean found = false;在此声明之后int i = 0;

修复#2:交易结束后输入 found = true;在您的System.out.println(accounts[i]);之后

原因:这是为了停止你的内部 while 循环。

修复#3:根据您的其他情况,输入 if条件将验证是否不再有下一个帐户。在终止内循环之前。

      else{
// i == 4 will return true after the loop reached the last account.
if(i == 4){
System.out.println("Account not found");
// this is to stop the inner loop if no account found .
found = true;
}
}

关于java - 在java中创建帐户类并输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27436491/

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