Java 将参数传递给构造函数
我有一些糟糕的编码习惯,这些习惯是我从糟糕的代码中养成的,但是我正在努力解决这些问题。我的主要问题是试图通过构造函数传递初始参数,我已经有一年没有用Java编码了,所以让我知道所有的错误Java 将参数传递给构造函数,java,constructor,Java,Constructor,我有一些糟糕的编码习惯,这些习惯是我从糟糕的代码中养成的,但是我正在努力解决这些问题。我的主要问题是试图通过构造函数传递初始参数,我已经有一年没有用Java编码了,所以让我知道所有的错误 public class AccountHolder { public static void main(String args[]) { //Introduce scanner Scanner sc = new Scanner(System.in);
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);
}
}
公共类账户持有人{
公共静态void main(字符串参数[])
{
//介绍扫描仪
Scanner sc=new Scanner(System.in);//用于从用户获取输入
System.out.println(“欢迎参加银行计划!您能告诉我您当前的余额吗?”);
输入=sc.nextDouble();
账户持有人(输入);
}
//介绍私人领域的成员
私人静态双年度利率;//保持年利率的常数
private static double fee;//保存取款费的常量
private double balance;//保存余额的变量
private double rate update;//用于保存更新速率的值的变量
私有静态双输入;//用于保存用户输入的变量
private double test;//用于测试用户是否会降到$100以下的变量。
//引入DecimalFormat对象
DecimalFormat twoPlace=新的DecimalFormat(“0.00”);//用于将值保留为2位有效数字。
//介绍公共方法
公共账户持有人(双重输入)
{
平衡=输入;
}
公共作废押金(双输入)
{
平衡=平衡+输入;
System.out.println(“您的新余额为:$”+twoPlace.format(余额));
}
公共作废取款(双输入)
{
测试=平衡;
平衡=平衡-输入;
如果(余额<100.0)
{
平衡=平衡+输入;
System.out.println(“您的余额不允许低于$100.00。请在您有更多资金时重试。”);
}
如果(测试>=500和平衡<500)
{
余额=余额-费用;
System.out.println(“您因低于$500.00而被额外收取$50”);
}
System.out.println(“您的新余额为:$”+twoPlace.format(余额));
}
公共无效月利息()
{
余额+=余额*(年利率/12.0);
}
公共静态无效modifyMonthlyInterest(双费率更新)
{
年度利率=利率更新;
而(年利率=1.0)
{
System.out.println(“错误!利率必须介于0和1之间。我们需要保留我们的钱!”);
AnnualInterestate=sc.nextDouble();
}
}
公共字符串toString()
{
返回String.format(“$%.2f”,余额);
}
}
这是您的构造函数:
public AccountHolder(double input) {
balance = input;
}
您传递的参数如下所示:
AccountHolder(input);
您无法使用关键字new实际创建该类的新实例
像
而不是
AccountHolder(input);
你需要做什么
new AccountHolder(input);
当您缺少“new”时,它被解释为一个方法调用。使用“new”将其解释为对构造函数的调用
PS:我想建议你们研究变量的范围。例如,您可以在“main”方法内部定义“input”变量,而不是静态类变量。这也提高了代码的可读性。此外,你应该考虑使用一个整型变量来跟踪美分的数目,而不是使用双变量来跟踪美元的数量(或者你使用的货币单位)。你会发现使用double会在以后引起很多舍入和截断的麻烦。谢谢……可能是,我没有检查代码,只是关注与构造函数相关的问题……谢谢你的帮助和建议!我非常感激。此外,我们必须在这个项目中使用double,但在个人编程中我一定会记住这一点。谢谢你的建议和帮助。我很感激!你可能应该把这个问题转移到你有困难的地方,更准确地说。
new AccountHolder(input);