0

如果您想知道我的程序的目的是什么,可以查看我以前的问题。这是我几乎完成的代码:

import javax.swing.JOptionPane;

public class AssignmentTen
{
    public static void main (String[] args)
    {
        System.out.println();
        int num = Integer.parseInt(args[0]);
        int eNumber;
        String input2;
        String input3;
        String input4;
        String input5;
        String input6;
        int input7;
        int input8;
        int input9;
        int input10;

        Employee[] employees = new Employee[num];
        for (int i = 0; i < num; i++)
        {
            eNumber = getInt ("Enter Employee Number:");
            input2 = getString ("Enter Employee First Name:");
            input3 = getString ("Enter Employee Last Name:");
            input4 = getString ("Enter Employee Street:");
            input5 = getString ("Enter Employee City:");
            input6 = getString ("Enter Employee State (Initials):");
            input7 = getInt ("Enter Employee Zip Code (5 Digits):");
            input8 = getInt ("Enter Employee Hire Month (MM):");
            input9 = getInt ("Enter Employee Hire Day (DD):");
            input10 = getInt ("Enter Employee Hire Year(YYYY):");

            Name name = new Name(input2, input3);
            Address address = new Address (input4, input5, input6, input7);
            Date hireDate = new Date (input8, input9, input10);
            employees[i] = new Employee (eNumber, name, address, hireDate);

            System.out.println("#" + employees[i].empNumber + "\n" + employees[i].empName + "\n" + employees[i].empAddress + "\nHire Date: " + employees[i].empHireDate + "\n\n");
        }
    }

    public static int getInt(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return Integer.parseInt(str);
    }

    public static String getString(String paramString)
    {
        String str = JOptionPane.showInputDialog(paramString);
        return str;
    }
}

class Employee
{
    Number empNumber;
    Name empName;
    Address empAddress;
    Date empHireDate;

    public Employee(Number empNumber, Name empName, Address empAddress, Date empHireDate)
    {
        this.empNumber = empNumber;
        this.empName = empName;
        this.empAddress = empAddress;
        this.empHireDate = empHireDate;
    }
}

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }
}

class Address
{
    String eStreet;
    String eCity;
    String eState;
    int eZipCode;

    Address(String street, String city, String state, int zipCode)
    {
        eStreet = street;
        eCity = city;
        eState = state;
        eZipCode = zipCode;
    }
}

class Date
{
    int month;
    int day;
    int year;

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }
}

我希望我的程序显示通过命令行指定的员工数量的信息(例如,我键入“java AssignmentTen 3”来记录三个不同员工对象的数据)。我希望信息显示如下:

[#]员工编号

名字姓氏

街道城市州首字母邮政编码

入职日期:MM DD YYYY

例如:

#123

约翰·史密斯

123 示例街 CA 12345

入职日期:2013 年 9 月 30 日

但是,最终显示的内容根本不正确。员工编号显示正常,但名称行显示为“姓名@[RandomCharacters]”,地址行显示为“地址@[RandomCharacters]”,雇用日期行显示为“雇用日期:日期@[随机字符]”。我怎样才能让它显示我想要的方式(包括单词之间的空格和分隔月、日和年的斜线)?

谢谢。

编辑:我添加了接受的答案建议的更改,但我仍然遇到日期线问题。我使用了这段代码:

class Date
{
    int month;
    int day;
    int year;

    Date(int eMonth, int eDay, int eYear)
    {
        month = eMonth;
        day = eDay;
        year = eYear;
    }

    public String toString()
    {
        return month + "/" + day + "/" + year;
    }
}

但它仍然想出了随机字符。

4

3 回答 3

1

您看到的字符是您的类从 class 继承的未覆盖toString()方法的结果Object(因为所有类都扩展自Object)。您需要实现您的自定义toString()方法来为每个类返回您想要的格式。例如,

class Name
{
    String firstName;
    String lastName;

    Name(String first, String last)
    {
        firstName = first;
        lastName = last;
    }

    public String toString() 
    {
        return "[firstName = " + firstName + ", lastName = " + lastName + "]";
    }
}

然后你可以做

Name name = new Name("John", "Doe");
System.out.println(name);

会打印

[firstName = John, lastName = Doe]
于 2013-10-01T02:38:18.597 回答
0

您需要为每个和编写一个toString方法。当您尝试打印任何对象时,这就是在幕后调用的内容。这是该类的方法可能的样子。自己动手做另外两个。NameAddressDatetoStringDate

public String toString() {
    return month + "/" + day + "/" + year;
}

您可能会发现阅读http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString()很有用。

于 2013-10-01T02:42:01.633 回答
0

您添加到员工的姓名、地址和日期变量是具有自己变量的对象。要么覆盖 toString() 以打印出每个变量,要么通过 employees[i].name.firstname 手动调用它们。

@Override
public String toString(){
    return firstName + lastName;
}

此外,您应该将私有变量与 getter 和 setter 访问方法一起使用。

于 2013-10-01T02:42:40.103 回答