我用两个参数实例化一个对象。在尝试传递另一个值时(变量"兴趣")。
我收到错误:
error: cannot find symbol
investor1.initialize(interest);
^
symbol:方法初始化(double) location:投资者类型的可变投资者1 1错误
工具已完成,退出代码为1
我需要创建另一个对象吗?或者传递这个值的正确方法是什么?
public static void main(String[] args){
Scanner stdIn = new Scanner(System.in);
Investor investor1 = new Investor (1001, 2000); //
System.out.print("Please enter the Annual Interest Rate you hope to earn; ");
double interest = stdIn.nextDouble();
investor1.initialize(interest); //here is where I get the error.
System.out.println("Monthly balances for one year with " + interest + " annual interest:");
System.out.printf("\n%s\t%s\t%s", "Month", "Account #", "Balance");
System.out.printf("\n%s\t%s\t%s\n", "-----", "---------", "-------");
for (int i = 0; i < 12; i++){
System.out.printf("\n%5d\t%9d\t%7.2f", i, investor1.getAccount(),investor1.getBalance());
}
代码的第二部分:
public class Investor{
private double interest;
private final int Account_Number;
private double balance;
//***********************************************************
public Investor(int account, double bal){
this.Account_Number = account;
this.balance = bal;
}
//********************************************************************
public void initialize(double temp){
this.interest = temp;
}
public double getInt(){
return interest;
}
public int getAccount(){
return this.Account_Number;
}
public double getBalance(){
return this.balance;
}
}//end of investor
感谢您的帮助
答案 0 :(得分:-1)
我重建了你的代码如下,错误消失了(也是可执行的)。请看看你是否可以毫无例外地运行它:
import java.util.Scanner;
public class Investor {
public static void main(String[] args) {
Scanner stdIn = new Scanner(System.in);
Investor investor1 = new Investor(1001, 2000); //
System.out.print("Please enter the Annual Interest Rate you hope to earn; ");
double interest = stdIn.nextDouble();
investor1.initialize(interest); // here is where I get the error.
System.out.println("Monthly balances for one year with " + interest + " annual interest:");
System.out.printf("\n%s\t%s\t%s", "Month", "Account #", "Balance");
System.out.printf("\n%s\t%s\t%s\n", "-----", "---------", "-------");
for (int i = 0; i < 12; i++) {
System.out.printf("\n%5d\t%9d\t%7.2f", i, investor1.getAccount(), investor1.getBalance());
}
}
private double interest;
private final int Account_Number;
private double balance;
// ***********************************************************
public Investor(int account, double bal) {
this.Account_Number = account;
this.balance = bal;
}
// ********************************************************************
public void initialize(double temp) {
this.interest = temp;
}
public double getInt() {
return interest;
}
public int getAccount() {
return this.Account_Number;
}
public double getBalance() {
return this.balance;
}
}// end of investor