我有糟糕的编码习惯,我从糟糕的代码中汲取,但是我正努力尝试解决这些问题。我的主要问题是尝试通过构造函数传递初始参数,我还没有用Java编写一年的代码,所以让我知道一切都是错的!
public class AccountHolder {
public static void main(String args[])
{
//Introduce scanner
Scanner sc = new Scanner(System.in); //Used to take input from user
System.out.println("Welcome to the bank program! Can you tell me your current balance?");
input = sc.nextDouble();
AccountHolder(input);
}
// Introduce private field members
private static double annualInterestRate; //Constant to hold annual interest rate
private static double fee; // Constant to hold the withdrawal fee
private double balance; // variable to hold the balance
private double rateUpdate; // variable to hold the value to update the rate
private static double input; // variable to hold user input
private double test; // variable to test whether or not user will drop below $100.
// introduce a DecimalFormat object
DecimalFormat twoPlace = new DecimalFormat("0.00"); // Used to keep values to 2 significant figures.
// Introduce public methods
public AccountHolder(double input)
{
balance = input;
}
public void deposit(double input)
{
balance = balance + input;
System.out.println("Your new balance is: $" + twoPlace.format(balance));
}
public void withdrawl(double input)
{
test = balance;
balance = balance - input;
if (balance < 100.0)
{
balance = balance + input;
System.out.println("Your balance is not allowed to drop below $100.00. Please try again when you have more funds.");
}
if (test >= 500 && balance < 500)
{
balance = balance - fee;
System.out.println("You have been charged an additional $50 for dropping below $500.00.");
}
System.out.println("Your new balance is: $" + twoPlace.format(balance));
}
public void monthlyInterest()
{
balance += balance * (annualInterestRate / 12.0);
}
public static void modifyMonthlyInterest(double rateUpdate)
{
annualInterestRate = rateUpdate;
while (annualInterestRate <= 0 || annualInterestRate >= 1.0)
{
System.out.println("Error! Interest rates must be between 0 and 1. We need to keep our money!");
annualInterestRate = sc.nextDouble();
}
}
public String toString()
{
return String.format("$%.2f", balance);
}
}
答案 0 :(得分:2)
而不是
AccountHolder(input);
你需要做
new AccountHolder(input);
当你缺少&#34; new&#34;时,它被解释为方法调用。使用&#34; new&#34;它被解释为对构造函数的调用。
PS:我想建议你研究一下变量的范围。例如。你可以定义&#34;输入&#34; &#34; main&#34;内部的变量方法而不是静态类变量。这提高了代码的可读性。
答案 1 :(得分:1)
这是你的构造函数:
ControlTemplate
你传递的参数如下:
public AccountHolder(double input) {
balance = input;
}
您缺少使用关键字 new 来实际创建该类的新实例...
喜欢
AccountHolder(input);