Java Gurus,
处理课程作业,我们获得了一套两个课程。一个人打电话给另一个人来计算银行账户的利率,余额等。我遇到的问题是,找出我应该输入的变量,以便为我们的程序成功编译。下面是我们给出的两个Java文件。我做了调整以纠正代码中的所有有目的的错误,所以到目前为止一切都很好地编译。
public class BankAccount {
private double balance; // Account balance
private double interestRate; // Interest rate
private double interest; // Interest earned
/**
* The constructor initializes the balance
* and interestRate fields with the values
* passed to startBalance and intRate. The
* interest field is assigned to 0.0.
*/
public BankAccount(double startBalance, double intRate) {
balance = startBalance;
interestRate = intRate;
interest = 0.0;
}
/**
* The deposit method adds the parameter
* amount to the balance field.
*/
public void deposit(double amount) {
balance += amount;
}
/**
* The withdraw method subtracts the
* parameter amount from the balance
* field.
*/
public void withdraw(double amount) {
balance -= amount;
}
/**
* The addInterest method adds the interest
* for the month to the balance field.
*/
public void addInterest() {
interest = balance * interestRate;
balance += interest;
}
/**
* The getBalance method returns the
* value in the balance field.
*/
public double getBalance() {
return balance;
}
/**
* The getInterest method returns the
* value in the interest field.
*/
public double getInterest() {
return interest;
}
}
这是我们需要编译的Program2.java
:
import java.text.DecimalFormat; // Needed for 2 decimal place amounts
import java.util.Scanner; // Needed for the Scanner class
public class Program2 {
public static void main(String[] args) {
BankAccount account; // To reference a BankAccount object
double balance, // The account's starting balance
interestRate, // The annual interest rate
pay, // The user's pay
cashNeeded; // The amount of cash to withdraw
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat("#0.00");
// Get the starting balance.
System.out.print("What is your account's " + "starting balance? ");
balance = keyboard.nextDouble();
// Get the monthly interest rate.
System.out.print("What is your monthly interest rate? ");
interestRate = keyboard.nextDouble();
// Create a BankAccount object.
account = new BankAccount(balance, interestRate);
// Get the amount of pay for the month.
System.out.print("How much were you paid this month? ");
pay = keyboard.nextDouble();
// Deposit the user's pay into the account.
System.out.println("We will deposit your pay " + "into your account.");
account.deposit(pay);
System.out.println("Your current balance is %bodyquot; + formatter.format( account.getBalance() )");
// Withdraw some cash from the account.
System.out.print("How much would you like " + "to withdraw? ");
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);
// Add the monthly interest to the account.
account.addInterest();
// Display the interest earned and the balance.
System.out.println("This month you have earned %bodyquot; + formatter.format( account.getInterest() )" +
" in interest.");
System.out.println("Now your balance is %bodyquot; + formatter.format( account.getBalance() ) )");
}
}
我需要输入的是起始余额为500,每月利率为0.00125(代码中的利息每月复利,并且非常确定我知道将此变量放在何处),1000月薪和900撤销金额。最终结果应为600.75美元。
我的所有代码是否存在或是否需要声明变量起始余额,利率,每月支付和提款金额的值?
请告诉我,如果我做错了什么,或者如果答案是在我面前,我今天只是失明。
答案 0 :(得分:0)
您的代码中存在复制粘贴问题。
更改发件人:
System.out.println("Your current balance is %bodyquot; + formatter.format( account.getBalance() )");
收件人:
System.out.println("Your current balance is " + formatter.format( account.getBalance() ));
使用下面提到的更新代码。
import java.text.DecimalFormat; // Needed for 2 decimal place amounts
import java.util.Scanner; // Needed for the Scanner class
public class Program2 {
public static void main(String[] args) {
BankAccount account; // To reference a BankAccount object
double balance, // The account's starting balance
interestRate, // The annual interest rate
pay, // The user's pay
cashNeeded; // The amount of cash to withdraw
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Create an object for dollars and cents
DecimalFormat formatter = new DecimalFormat("#0.00");
// Get the starting balance.
System.out.print("What is your account's " + "starting balance? ");
balance = keyboard.nextDouble();
// Get the monthly interest rate.
System.out.print("What is your monthly interest rate? ");
interestRate = keyboard.nextDouble();
// Create a BankAccount object.
account = new BankAccount(balance, interestRate);
// Get the amount of pay for the month.
System.out.print("How much were you paid this month? ");
pay = keyboard.nextDouble();
// Deposit the user's pay into the account.
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() ));
// Withdraw some cash from the account.
System.out.print("How much would you like to withdraw? ");
cashNeeded = keyboard.nextDouble();
account.withdraw(cashNeeded);
// Add the monthly interest to the account.
account.addInterest();
// Display the interest earned and the balance.
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() ) );
}
}
答案 1 :(得分:0)
在第二个程序或运行时创建BankAccount类时,您将要输入这些值。你可以尝试这样的事情:
BankAccount account = new BankAccount(500, 0.00125)
然后在运行时输入其余的值,或者您可以在程序2中为所有内容创建局部变量,然后使用它们作为参数创建一个新的BankAccount。
希望它有所帮助。