gpt4 book ai didi

java - 错误: cannot find symbol while passing value

转载 作者:行者123 更新时间:2023-12-01 06:11:33 25 4
gpt4 key购买 nike

我用两个参数实例化一个对象。同时尝试传递另一个值(变量“利息”)。

我收到错误:

error: cannot find symbol
investor1.initialize(interest);
^

符号:方法初始化(double) 位置:投资者类型的变量投资者11 个错误

工具已完成,退出代码为 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

感谢您的帮助

最佳答案

我按照如下方式重建了您的代码,错误消失了(而且它是可执行的)。请看一下是否可以正常运行:

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

关于java - 错误: cannot find symbol while passing value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33727177/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com