Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何消除token上的语法错误;“公众”;在主课上?_Java_Compiler Errors - Fatal编程技术网

Java 如何消除token上的语法错误;“公众”;在主课上?

Java 如何消除token上的语法错误;“公众”;在主课上?,java,compiler-errors,Java,Compiler Errors,当我从主程序运行所需的另一个类调用构造函数时,我的主类中出现语法错误。这个程序的重点是继承以及构造函数和参数的适当调用。这是我在编译期间收到的错误消息: Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error on token "public", record expected after this token

当我从主程序运行所需的另一个类调用构造函数时,我的主类中出现语法错误。这个程序的重点是继承以及构造函数和参数的适当调用。这是我在编译期间收到的错误消息:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
        Syntax error on token "public", record expected after this token

        at a6main.main(a6main.java:7)
这是导致错误的代码行:

PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821",
"2000", true, "1000");
代码的其余部分可在下面找到:

class person {

    String Name;
    String Address;
    String Telephone;

    person (String Name, String Address, String Telephone) {
        this.Name = Name;
        this.Address = Address;
        this.Telephone = Telephone;
    }
    
    String getName() {
        return Name;
    }
    
    String getAddress() {
        return Address;
    }
    
    String getTelephone() {
       return Telephone;
    }
    
    void setName(String Name) {
        this.Name = Name;
    }
    
    void setAddress(String Address) {
        this.Address = Address;
    }
    
    void setTelephone(String Telephone) {
        this.Telephone = Telephone;
    }
}

public class customer extends person {

    String number;
    boolean OnMailingList;

    //constructor and getters and setters

    customer (String Name, String Address, String Telephone, String number, boolean OnMailingList) {
        //inherit persons information
        super(Name, Address, Telephone);
        this.number = number;
        this.OnMailingList = OnMailingList;
    }
    
    String getnumber() {
        return number;
    }

    void setnumber(String number) {
        this.number = number;
    }

    boolean OnMailingList () {
        return OnMailingList;
    }

    void setOnMailingList(boolean OnMailingList) {
        this.OnMailingList = OnMailingList;
    }
}

public class PreferredCustomer extends customer {

    private int purchase;
    double discount;

    /**public constructor so its accessible to main
     * else ifs for certain percentage of discounts
     * getters and setters for purchase and discount
     * super to inherit other features from other classes */
    public int getpurchase() {
        return purchase;
    }

    public double getdiscount () {
        return this.discount;
    }

    public void setPurchase(int purchase) {
        this.purchase = purchase;
    }

    public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, 
                            boolean OnMailingList, double Discount, PreferredCustomer preferredCustomer) {
        
        super(Name, Address, Telephone, number, OnMailingList);

        this.purchase = pur;
        preferredCustomer.discount = discount;

        if (this.purchase>= 2000) {
            this.discount = 10;
        } else if (this.purchase>= 1500) {
            this.discount = 7;
        } else if (this.purchase>= 1000) {
            this.discount = 6;
        } else if (this.purchase >= 500) {
            this.discount = 5;
        }
    }
}
    

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

    }

    PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821","2000", true, "1000");
   
        System.out.println("Name: " + c.getName());
        System.out.println("Address: " + c.getAddress());
        System.out.println("Telephone number: " + c.getTelephone());
        System.out.println("Customer ID: " + c.getnumber());
        System.out.println("Amount spent: " + c.getpurchase());
        System.out.println("On mailing list: " + c.OnMailingList());
        System.out.println("Discount: " + c.getdiscount());
    }
}

你这里有几个错误。我已经纠正了它们,程序启动,提供结果:

名称:Al
地址:222伯克斯特
电话号码:2102223321
客户ID:46821
花费金额:2000
在邮件列表中:true
折扣:10.0

从主方法中删除PreferredCustomer构造函数。这不可能是一场战争的一部分 方法,它是类的一部分。然后,PreferredCustomer的构造函数已经存在于PreferredCustomer类中

希望您的customer和PreferredCustomer类在不同的文件中?如果没有,请将它们放在名为customer.java和PreferredCustomer.java的单独文件中。在
PreferredCustomer
类构造函数中,从参数中删除
PreferredCustomer PreferredCustomer
。这是多余的:为什么需要将一个客户传递给另一个客户?客户之间是否有任何关系?现在,当您调用构造函数时,参数的数量将匹配(不要使用字符串“2000”、“1000”,其中应为整数):

在PreferredCustomer构造函数中,在此处使用
this
而不是
PreferredCustomer
this.discount=discount并打印带有大写字母的
折扣
,如在构造函数的签名中

因此,建造商的代码应为:

public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, boolean OnMailingList, double Discount) {
    super(Name, Address, Telephone, number, OnMailingList);
    this.purchase = pur;
    this.discount = Discount;

    if (this.purchase>= 2000) {
        this.discount = 10;
    } else if (this.purchase>= 1500) {
        this.discount = 7;
    } else if (this.purchase>= 1000) {
        this.discount = 6;
    } else if (this.purchase >= 500) {
        this.discount = 5;
    }
}
a6main类中的主要方法:

public static void main (String [] args) {
    PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821", 2000, true, 1000);

    System.out.println("Name: " + c.getName());
    System.out.println("Address: " + c.getAddress());
    System.out.println("Telephone number: " + c.getTelephone());
    System.out.println("Customer ID: " + c.getnumber());
    System.out.println("Amount spent: " + c.getpurchase());
    System.out.println("On mailing list: " + c.OnMailingList());
    System.out.println("Discount: " + c.getdiscount());
}

正如其他人指出的那样,还要注意命名约定。

这里有几个错误。我已经纠正了它们,程序启动,提供结果:

名称:Al
地址:222伯克斯特
电话号码:2102223321
客户ID:46821
花费金额:2000
在邮件列表中:true
折扣:10.0

从主方法中删除PreferredCustomer构造函数。这不可能是一场战争的一部分 方法,它是类的一部分。然后,PreferredCustomer的构造函数已经存在于PreferredCustomer类中

希望您的customer和PreferredCustomer类在不同的文件中?如果没有,请将它们放在名为customer.java和PreferredCustomer.java的单独文件中。在
PreferredCustomer
类构造函数中,从参数中删除
PreferredCustomer PreferredCustomer
。这是多余的:为什么需要将一个客户传递给另一个客户?客户之间是否有任何关系?现在,当您调用构造函数时,参数的数量将匹配(不要使用字符串“2000”、“1000”,其中应为整数):

在PreferredCustomer构造函数中,在此处使用
this
而不是
PreferredCustomer
this.discount=discount并打印带有大写字母的
折扣
,如在构造函数的签名中

因此,建造商的代码应为:

public PreferredCustomer(String Name, String Address, String Telephone, String number, int pur, boolean OnMailingList, double Discount) {
    super(Name, Address, Telephone, number, OnMailingList);
    this.purchase = pur;
    this.discount = Discount;

    if (this.purchase>= 2000) {
        this.discount = 10;
    } else if (this.purchase>= 1500) {
        this.discount = 7;
    } else if (this.purchase>= 1000) {
        this.discount = 6;
    } else if (this.purchase >= 500) {
        this.discount = 5;
    }
}
a6main类中的主要方法:

public static void main (String [] args) {
    PreferredCustomer c = new PreferredCustomer("Al", "222BurdSt", "2102223321", "46821", 2000, true, 1000);

    System.out.println("Name: " + c.getName());
    System.out.println("Address: " + c.getAddress());
    System.out.println("Telephone number: " + c.getTelephone());
    System.out.println("Customer ID: " + c.getnumber());
    System.out.println("Amount spent: " + c.getpurchase());
    System.out.println("On mailing list: " + c.OnMailingList());
    System.out.println("Discount: " + c.getdiscount());
}

正如其他人指出的那样,注意命名约定。

您的所有代码都在同一个文件中吗?@azurefrog是的,它们在同一个文件夹中。它不知道如何解析
public PreferredCustomer(){}
,因为这将是
PreferredCustomer
类中的构造函数。相反,它被随机放在
a6main
类中程序的
main()
方法中。只需删除这三行,它们什么也不做。不管怎么说,这可能是一个复制粘贴错误?@PetrJaneček好吧,我这么做了,现在主要的首选客户构造函数还没有定义。。。PreferredCustomer c=新的PreferredCustomer(“Al”、“222BurdSt”、“2102223321”、“46821”、“2000”、true、“1000”)@AlwinBayan嗨,您的PreferredCustomer构造函数接受9个参数。您正在使用新的PreferredCustomer(“Al”、“222BurdSt”、“2102223321”、“46821”、“2000”、true、“1000”)调用此构造函数;因此,您要么修复公共PreferredCustomer(xxxxx)构造函数,使其仅接受7个参数,要么使用此构造函数并向其传递9个值。。。另外,我在公共PreferredCustomer()构造函数中看到了一些奇怪的东西……为什么要将PreferredCustomer(PreferredCustomer)传递给它?:)您的所有代码都在同一个文件中吗?@azurefrog是的,它们在同一个文件夹中。它不知道如何解析
public PreferredCustomer(){}
,因为这将是
PreferredCustomer
类中的构造函数。相反,它被随机放在
a6main
类中程序的
main()
方法中。只需删除这三行,它们什么也不做。不管怎么说,这可能是一个复制粘贴错误?@PetrJaneček好吧,我这么做了,现在主要的首选客户构造函数还没有定义。。。PreferredCustomer c=新的PreferredCustomer(“Al”、“222BurdSt”、“2102223321”、“46821”、“2000”、true、“1000”)@AlwinBayan嗨,您的PreferredCustomer构造函数接受9个参数。您正在使用新的PreferredCustomer(“Al”、“222BurdSt”、“2102223321”、“46821”、“2000”、true、“1000”)调用此构造函数;因此,您要么修复公共PreferredCustomer(xxxxx)构造函数,使其仅接受7个参数,要么使用此构造函数并向其传递9个值。。。另外,我在公共PreferredCustomer()构造函数中看到了一些奇怪的东西……为什么要将PreferredCustomer(PreferredCustomer)传递给它?:)