gpt4 book ai didi

java - 帐户类别错误 2

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

我仍然无法弄清楚最有效的方法到底是如何做到这一点的。基本上,我试图为创建的 Account 数组中的每个对象设置 balance = 0。我尝试使用 for 循环并为每个创建的帐户设置 balance = 0,但我不确定如何进行这项工作,因为 balance 变量是在具有所有方法的类中创建的。我整天都在尝试解决这个问题,但没有运气。谢谢。

Main method:
import java.util.Scanner;
import java.text.NumberFormat;
public class Account2
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
Account[] acct = new Account[30];
for (int count2; count2 < 30; count2++)
{
balance = 0; //Initial balance is always set to zero to be able to run fresh program every time
}
System.out.println("Enter your account number (1-30): ");
int key = scan.nextInt() - 1;
int reset = 0;
while (reset == 0)
{
System.out.println("Enter W for withdrawl; D for deposit; X to escape");
char choice = scan.next().charAt(0);

if (choice == 'W' || choice == 'w' || choice == 'D' || choice == 'd' || choice == 'x' || choice == 'X')
{
if (choice == 'W' || choice == 'w')
{
System.out.println("Enter amount to withdraw: ");
Double withdraw1 = scan.nextDouble();
if (withdraw1 <= acct[key].getBalance())
{
acct[key].withdraw(withdraw1);
System.out.println("User # " + key++ + " funds after withdraw: " + acct[key].getBalance() + "$");
System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
reset++;
}
else
System.out.println("Insufficient funds.");
}

if (choice == 'D' || choice == 'd')
{
System.out.println("Enter amount to deposit: ");
Double deposit1 = scan.nextDouble();
if (deposit1 > 0)
{
acct[key].deposit(deposit1);
System.out.println("User # " + key++ + " funds after deposit: " + acct[key].getBalance() + "$");
System.out.println("User # " + key++ + " funds after interest: " + acct[key].addInterest() + "$");
reset++;
}
else
System.out.println("Use the withdrawl feature to withdrawl money.");

}
if (choice == 'x' || choice == 'X')
System.out.println("Thank You for using this bank.");
reset++;
}
else
{
System.out.println("Invalid entry, please try again");
reset = 0;
}
}
}
}

支持类:

public class Account
{
private final double RATE = 0.03; //Interest is 3%

private int acctNumber;
private String name;
private balance;

//Defines owner, account number, and initial balance.
public Account(String owner, int account, double initial)
{
name = owner;
acctNumber = account;
balance = initial;
}

//deposits a specified amount and returns new balance
public double deposit(double amount)
{
balance = balance + amount;
return balance;
}

//withdraws the specified amount from the account and applies the fee
// + returns balance
public double withdraw(double amount)
{
int fee = 1;
balance = balance - amount - fee;
return balance;
}

//Adds interest to the account
public double addInterest()
{
balance += (balance * RATE);
return balance;
}
public double getBalance()
{
return balance;
}

//returns a one line description of the account as a string
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return acctNumber + "/t" + name + "/t" + fmt.format(balance);
}
}

最佳答案

在外部类中,您只能以其可见(例如公共(public))API 中公开的方式与 Account 进行交互。

在这种情况下,当前的方法是withdraw()当前的balance:

acct[i].withdraw(acct[i].getBalance());

尽管这种特定情况会使余额变为负数,因为您收取取款费用(这对调用类是隐藏的)。

如果您要在 Account 上公开一个 setBalance 方法,您可以改为这样做

acct[i].setBalance(0);

但是仔细观察,您遇到的问题似乎实际上是在初始化帐户数组。你可以这样做:

for (int count2; count2 < 30; count2++)
{
acct[count2] = new Account(owner, count2, 0);
}

关于java - 帐户类别错误 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34259331/

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