如果您想知道我的程序的目的是什麼,那麼您可以查看我以前的問題。這是我的幾乎完成的代碼:類構造函數對象無法正確顯示
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;
}
}
我想我的程序,用於通過命令行中指定的僱員的量顯示信息(因此,例如,I型的Java AssignmentTen 3'記錄用於三個不同的僱員數據對象)。我想顯示這樣的信息:
[#] EmployeeNumber
名姓
Street市StateInitials郵編
聘用日期:MM DD YYYY
例如:
#123
約翰·史密斯
123例街CA 12345
聘用日期:2013年9月30日
然而,是什麼最終顯示是不正確的,在所有的。員工編號顯示正常,但名稱行的名稱爲'Name @ [RandomCharacters]',地址行的地址爲'Address @ [RandomCharacters]',Hire Date行顯示'Hire Date:Date @ [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;
}
}
但它仍然想出了隨機字符。
而不是要求用戶將MM DD YYYY設置爲3個不同的字段?爲什麼不要求他們把一個領域(例如DDMMYYYY)?稍後,您可以使用SimpleDateFormat輕鬆顯示它? – Rudy