我没有从Eclipse获得错误,但我的控制台输出不是应该的。这是我正在研究的两个班级。
头等舱
public class BankAccount
{
private double balance;
private double interestRate;
private double interest;
public BankAccount(double startBalance, double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
public void deposit(double amount)
{
balance += amount;
}
public void withdraw(double amount)
{
balance -= amount;
}
public void addInterest()
{
interest = balance * interestRate;
balance += interest;
}
public double getBalance()
{
return balance;
}
public double getInterest()
{
return interest;
}
}
下一课
import java.util.Scanner;
import java.text.DecimalFormat;
public class Program2
{
public static void main(String[] args)
{
BankAccount account;
double balance = 500,
interestRate = 0.00125,
pay = 1000,
cashNeeded = 900;
Scanner keyboard = new Scanner(System.in);
DecimalFormat formatter = new DecimalFormat ("#0.00");
System.out.print("What is your account's starting balance?");
balance = keyboard.nextDouble();
System.out.print("What is your monthly interest rate?");
interestRate = keyboard.nextDouble();
account = new BankAccount(balance, interestRate);
System.out.print("How much were you paid this month? ");
pay = keyboard.nextDouble();
System.out.println("We will deposit your pay into your account.");
account.deposit(pay);
System.out.println("Your current balance is "
+ formatter.format(account.getBalance()));
System.out.print("How much would you like to withdraw? ");
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);
account.getInterest();
System.out.println("This month you have earned "
+ formatter.format( account.getInterest() )
+ " in interest.");
System.out.println("Now your balance is "
+ formatter.format( account.getBalance()));
}
}
我正在获得的输出:
您的帐户的起始余额是多少?
输出我应该得到:
您的帐户的起始余额是多少? 500个
你的月利率是多少? 0.00125
你这个月付了多少钱? 1000
我们会将您的付款存入您的帐户 您当前的余额是1500.00
你要退多少钱? 900个
本月你的利息为0.75 现在你的余额是600.75左
答案 0 :(得分:0)
扫描仪等待输入,直到您提供信息为止,它不会继续前进。
答案 1 :(得分:0)
工作得很好。 我没有修改你的代码中的任何内容。 实际上我正在考虑当程序要求你输入一些内容时,那时你没有给出输入。
在询问 What is your account's starting balance?