我会尽可能清楚地解释这一点。我试图通过将三个使用的类转换为程序将通过导入语句访问的包来缩小分配的代码。这是我的原始代码:
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;
}
public String toString()
{
return firstName + " " + lastName;
}
}
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;
}
public String toString()
{
return eStreet + " " + eCity + " " + eState + " " + eZipCode;
}
}
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;
}
}
我需要将 Name 类、Address 类和 Date 类放入一个名为 util 的包中。首先,按照指示,我将所有 Java 程序组织在一个名为 My Java Programs (C:\MyJavaPrograms) 的文件夹中。为了创建我认为应该是包的东西,我复制了每个类并将它们放入自己的单独文件中,我将语句“package.util”放在顶部,然后我将它们命名为 Name.java、Address.java , 和 Date.java 我将它们放在一个名为 util 的文件夹中,该文件夹位于 C:\MyJavaPrograms 中。
之后,我把语句'import util.*;' 在我原始代码的顶部并将其放在 C:\MyJavaPrograms 中。由于某种原因,该文件无法编译。我已经为我的问题做了很多谷歌搜索,包括阅读这个,这对我没有帮助,至少在我理解错误的程度上。
我怀疑我的类路径有问题,尽管我不知道我能做些什么来修复它。如果它有助于找出问题,我知道我不能在命令提示符下使用 javac 命令,而无需转到该线程并输入其中一个响应。
理想情况下,我想编译我的新作业并让它完全按照我在这个问题顶部发布的代码现在所做的那样,除了它在使用带有额外类的包装时这样做。如果有人能把我送到正确的方向,我将不胜感激。