有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java它显示客户已定义的错误。请让我知道什么地方出了问题以及如何纠正

有一个遵从性错误,说明类名已被定义,我找不到解决它的方法

此外,类名只声明一次,无法找到出错的地方

package practo;
import java.io.*;
import java.lang.*;
import java.util.*;

@SuppressWarnings("unused")
class Customer     /* compilation error occurs here */
{
    private int id;
    private String name;
    private String email;
    private String address;
    void setid(int id)
    {
        this.id=id;
    }
    int getid()
    {
        return id;
    }
    void setname(String name)
    {
        this.name=name;
    }
    String getname()
    {
        return name;
    }
    void setemail(String email)
    {
        this.email=email;
    }
    String getemail()
    {
        return email;
    }
    void setaddress(String address)
    {
        this.address=address;
    }
    String getaddress()
    {
        return address;
    }
    class PhoneNumber
    {
        private String phoneNumber;
        private String heldFromDate;
        private String heldToDate;
        void setphoneNumber(String phoneNumber)
        {
            this.phoneNumber=phoneNumber;
        }
        String getphoneNumber()
        {
            return phoneNumber;
        }

        void setheldToDate(String heldToDate)
        {
            this.heldToDate=heldToDate;
        }
        String getheldToDate()
        {
            return heldToDate;
        }

        public String getHeldFromDate() {
            return heldFromDate;
        }
        public void setHeldFromDate(String heldFromDate) {
            this.heldFromDate = heldFromDate;
        }
        class NumberType
        {
            private String code;
            private String description;
            void setcode(String code)
            {
                this.code=code;
            }
            void setdescription(String description)
            {
                this.description=description;
            }
            String getcode()
            {
                return code;
            }
            String getdescription()
            {
                return description;
            }
        }
    }
}

class x1
{
    public void main(String args[])
    {
        @SuppressWarnings("resource")
        Scanner s=new Scanner(System.in);
        Customer c=new Customer();
        Customer.PhoneNumber p=c.new PhoneNumber();
        Customer.PhoneNumber.NumberType n=p.new NumberType();
        System.out.println("Enter the customer details");
        System.out.println("Enter the  id :");
        int id=s.nextInt();
        c.setid(id);
        System.out.println(c.getid());
        System.out.println("Enter the name :");
        String name=s.nextLine();
        c.setname(name);
        System.out.println(c.getname());
        System.out.println("Enter the email :");
        String email=s.nextLine();
        c.setemail(email);
        System.out.println(c.getemail());
        System.out.println("Enter the address :");
        String address=s.nextLine();
        c.setaddress(address);
        System.out.println(c.getaddress());
        System.out.println("Enter the customer contact details");
        System.out.println("Enter the phone number :");
        String phoneNumber=s.nextLine();
        p.setphoneNumber(phoneNumber);
        System.out.println(p.getphoneNumber());
        System.out.println("Enter the held from date (dd/MM/yyyy)  :");
        String heldFromDate=s.next();
        p.setHeldFromDate(heldFromDate);
        System.out.println(p.getHeldFromDate());
        System.out.println("Enter the held to date (dd/MM/yyyy)  :");
        String heldToDate=s.next();
        p.setheldToDate(heldToDate);
        System.out.println(p.getheldToDate());
        System.out.println("Enter number type code :");
        String code=s.next();
        n.setcode(code);
        System.out.println(n.getcode());
        System.out.println("Enter number type description");
        String description=s.next();
        n.setdescription(description);
        System.out.println(n.getdescription());
    }

}

共 (4) 个答案

  1. # 1 楼答案

    你们班没有给我任何编译错误。您可以尝试将类公开,即public class Customer,文件名为Customer.java。包practo可能已经包含一个名为Customer的类

  2. # 2 楼答案

    请确认客户类别是否重复?如果没有,可以从“项目”菜单中选择“清理”,它可能会修复这些错误。 有时日食会困扰我们

  3. # 3 楼答案

    许多改进建议:

    1. 每个文件打开公共类,每个类打开一个文件。你的安排令人困惑
    2. 学习并遵循Java编码标准
    3. 使用日期代替字符串是一种更好的设计,尤其是对于JDK 8和java。时间包
    4. 学习JUnit而不是x1。梅因

    以下是你的课程应该是什么样子的例子

    顾客。爪哇

        package practo;
    
    /**
     * Created by Michael
     * Creation date 5/29/2016.
     * @link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
     */
    public class Customer {
    
        private int id;
        private String name;
        private String email;
        private String address;
    
        public Customer() {
            this(0, "", "", "");
        }
    
        public Customer(int id, String name, String email, String address) {
            this.id = id;
            this.name = name;
            this.email = email;
            this.address = address;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getAddress() {
            return address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    }
    

    电话号码。爪哇:

    package practo;
    
    /**
     * Created by Michael
     * Creation date 5/29/2016.
     * @link https://stackoverflow.com/questions/37511168/it-show-the-error-that-customer-is-already-defined-please-let-me-know-whats-wron
     */
    public class PhoneNumber {
    
        private String phoneNumber;
        private String heldFromDate;  // Bad design.  This ought to be a Date, not a String
        private String heldToDate;    // Bad design.  This ought to be a Date, not a String
    
        public PhoneNumber() {
            this("", "", "");
        }
    
        public PhoneNumber(String phoneNumber, String heldFromDate, String heldToDate) {
            this.phoneNumber = phoneNumber;
            this.heldFromDate = heldFromDate;
            this.heldToDate = heldToDate;
        }
    
        public String getPhoneNumber() {
            return phoneNumber;
        }
    
        public void setPhoneNumber(String phoneNumber) {
            this.phoneNumber = phoneNumber;
        }
    
        public String getHeldFromDate() {
            return heldFromDate;
        }
    
        public void setHeldFromDate(String heldFromDate) {
            this.heldFromDate = heldFromDate;
        }
    
        public String getHeldToDate() {
            return heldToDate;
        }
    
        public void setHeldToDate(String heldToDate) {
            this.heldToDate = heldToDate;
        }
    }
    
  4. # 4 楼答案

    检查包practo中是否有另一个名为Customer的类。这会导致名称冲突