gpt4 book ai didi

Java - 类无法看到其他类中的对象

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

我遇到了一个常见的编译错误:无法在 java 中找到符号。 Product 类中的方法不能在主类中使用。代码如下。我尝试过以任何可能的方式通过类(class)(据我所知),但没有成功。

我得到的错误是:

CashRegisterTester.java:108: error: cannot find symbol
double iprice = products[i].getPrice();
^
symbol: variable products
location: class CashRegister
1 error

这是代码。

import java.util.Scanner;

class CashRegisterTester {
public static void main(String[] args) {

Product[] products = new Product[5];

products[0] = new Product();
products[0].setCode(0);
products[0].setName("psomi");
products[0].setPrice(0.50);

products[1] = new Product();
products[1].setCode(1);
products[1].setName("gala");
products[1].setPrice(1.30);

products[2] = new Product();
products[2].setCode(2);
products[2].setName("mila");
products[2].setPrice(1.80);

products[3] = new Product();
products[3].setCode(3);
products[3].setName("zaxari");
products[3].setPrice(2.40);

products[4] = new Product();
products[4].setCode(4);
products[4].setName("krasi");
products[4].setPrice(13.20);


for (int i = 0; i < 5; i++)
System.out.println("\n Product code: " + products[i].getCode() + " \n Product Name: " + products[i].getName() + "\n Product Price: " +
products[i].getPrice());

System.out.println("Which product would you like to buy?");
Scanner myscanner = new Scanner(System.in);
int iEntered;
iEntered = myscanner.nextInt();

CashRegister register = new CashRegister(products);

register.recordPurchase(iEntered);

System.out.println("Would you like to buy something more? [y/n]");
String answer;
answer = myscanner.next();


while (answer.equals('y')) {
System.out.println("Which product would you like to buy?");

register.recordPurchase(iEntered);

System.out.println("Would you like to buy something more? [y/n]");
answer = myscanner.next();

}

System.out.println("How much money will you give?");

double pay = myscanner.nextDouble();
register.enterPayment(pay);

double change = register.giveChange();
System.out.println(change);

// register.finalReceipt();


}

}
<小时/>
class Product{

//Product fields
public int code;
public String name;
public double price;


//Getters & setters
public int getCode(){
return code;
}

public void setCode(int scode){
code = scode;
}

public String getName(){
return name;
}

public void setName(String sname){
name = sname;
}

public double getPrice(){
return price;
}

public void setPrice(double sprice){
price = sprice;
}
}
<小时/>
public class CashRegister extends Product{


public CashRegister(Product[] products){
purchase = 0;
payment = 0;
}

public void recordPurchase(int i){
double iprice = products[i].getPrice;
double total = purchase + iprice;
purchase = total;
}

public void enterPayment(double amount){
payment = amount;
}

public double giveChange(){
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

private double purchase;
private double payment;

}

最佳答案

products 变量未在任何地方声明。这就是您收到此异常的原因。

products 声明为 CashRegister 类的私有(private)成员,并在 CashRegister 构造函数中对其进行初始化。请参阅下面的代码:

public class CashRegister extends Product {

public CashRegister(Product[] products){
purchase = 0;
payment = 0;
this.products=products; // Initialize private member
}

public void recordPurchase(int i){
double iprice = products[i].getPrice();
double total = purchase + iprice;
purchase = total;
}

public void enterPayment(double amount){
payment = amount;
}

public double giveChange(){
double change = payment - purchase;
purchase = 0;
payment = 0;
return change;
}

private double purchase;
private double payment;
private Product[] products; // Declare private member
}

最后一点,为了防止资源泄漏,永远不要忘记关闭扫描仪。因此,在 CashRegister 类中的 main 方法末尾添加 myscanner.close();

关于Java - 类无法看到其他类中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29990560/

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