This question already has answers here:
What does a “Cannot find symbol” or “Cannot resolve symbol” error mean?
                                
                                    (13个回答)
                                
                        
                2个月前关闭。
            
        

在netbeans的第192行出现问题
似乎无法弄清楚问题是

Line 192: System.out.println("Percent off: "+customer.getcustomerDiscount());

Error:
cannot find symbol
symbol: method getcustomerDiscount()
location: variable customer of type Customer

(PS yes i know it's in 1 java file, it's supposed to be)

package Driver2;

import java.util.Scanner;
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
class Person{
private String name;
private String address;
private String number;
private int customerPurchase;

//Constructors
public Person(String name, String address, String number, int customerPurchase){
    this.name = name;
    this.address = address;
    this.number = number;
    this.customerPurchase = customerPurchase;
}

public Person(){}

//Accessors
public String getName(){
    return this.name;
}

public String getAddress(){
    return this.address;
}

public String getNumber(){
    return this.number;
}

public int getcustomerPurchase(){
    return this.customerPurchase;
}

//Mutators
public void setName(String n){
    this.name = n;
}

public void setAddress(String a){
    this.address = a;
}

public void setNumber(String n){
    this.number = n;
}

public void setcustomerPurchase(int a){
    this.customerPurchase = a;
}
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
class Customer extends Person{
private String customerNumber;
private boolean recieveMail;

//Constructors
public Customer(String name, String address, String number, String customerN, boolean rm, int customerPurchase) {
    super(name, address, number, customerPurchase);
    this.customerNumber = customerN;
    this.recieveMail = rm;
}

public Customer(){}

//Accessors
public String getCustomerNumber(){
    return this.customerNumber;
}

public boolean getRecieveMail(){
    return this.recieveMail;
}

//Mutators
public void setCustomerNumber(String c){
    this.customerNumber = c;
}

public void setRecieveMail(boolean r){
    this.recieveMail = r;
}
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
class Driver1 extends Customer
{
private int customerPurchase = 0;
private int customerDiscount;

//Constructors
   /* public Driver1(String name, String address, String number, String customerN, boolean rm, int customerPurchase)
{
    super();
    this.customerPurchase = customerPurchase;
    //this.customerDiscount = customerDiscount;
}*/
    public Driver1(String name, String address, String number, String customerN, boolean rm, int customerPurchase) {
    //super(name, address, number, customerPurchase, customerN, rm);
    //this.customerPurchase = customerN;
    //this.customerDiscount = pc;
}

public Driver1()
{}

//Accessors

//@Override
//public int getcustomerPurchase()
//{
//    return this.customerPurchase;
//}

public int getcustomerDiscount()
{
    return this.customerDiscount;
}

//Mutators
/*
@Override
public void setcustomerPurchase(int c)
{
    this.customerPurchase = c;
}*/


public void setcustomerDiscount(int r)
{
    this.customerPurchase = r;
    if (r >= 500)
    {
        System.out.print("5%");
    }
    else if (r >= 1000)
    {
        System.out.print("6%");
    }
    else if (r >= 1500)
    {
        System.out.print("7%");
    }
    else if (r >= 2000)
    {
        System.out.print("10%");
    }
    else
    {
        System.out.print("");
    }
}
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
class Driver3
{

public static void main(String args[]){

    Scanner scanner = new Scanner(System.in);

    System.out.print("Enter name of customer:");
    String name1 = scanner.nextLine();
    System.out.print("Enter address of customer:");
    String address1 = scanner.nextLine();
    System.out.print("Enter phone number of customer:");
    String number1 = scanner.nextLine();
    System.out.print("Enter customer number:");
    String customerNumber = scanner.nextLine();
    System.out.print("Enter yes/no -- does the customer want to recieve mail?:");
    String answer = scanner.nextLine();
    boolean recieveMail = (answer.equals("yes"));
    System.out.print("Enter amount customer has spent:");
    int customerPurchase = scanner.nextInt();

    Customer customer = new Customer(name1, address1, number1, customerNumber, recieveMail, customerPurchase);

    System.out.println("\nCustomer: ");
    System.out.println("Name: "+customer.getName());
    System.out.println("Address: "+customer.getAddress());
    System.out.println("Phone Number: "+customer.getNumber());
    System.out.println("Customer Number: "+customer.getCustomerNumber());
    System.out.println("Recieve Mail?: "+customer.getRecieveMail());
    System.out.println("Amount Purchased: "+customer.getcustomerPurchase());
    System.out.println("Percent off:  "+customer.getcustomerDiscount());

}
}


额外
我有点困惑,我给驱动程序1打电话,甚至做成了一个对象,可能使它工作。当我以其他方式测试if else语句时,又弹出了另一个问题

问题我应该将if else语句保留在setter中吗?或放入吸气剂?我认为我没有检索字符串是因为我将其全部设置为INT

最佳答案

您试图在getcustomerDiscount对象上调用Customer。该方法不是在此类上定义的,而是在Driver1上定义的。

另外:您应尝试使用Java命名约定,在这种情况下,应将getcustomerDiscountsetcustomerDiscount分别重命名为getCustomerDiscountsetCustomerDiscount

关于java - 错误找不到符号,方法getcustomerdiscount(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60274515/

10-12 05:11