代码是:
Person aTrainee = new Trainee (firstName,lastName,streetAddress,postCode,phoneNumb,performanceAverage,trainingArea);
错误说
no suitable constructor found for Trainee...ectect
Person是一个数组
实习生是阵中的一个项目(三个中的一个,受训者,员工和管理人员)
如果我删除括号中的项目,那么我没有得到错误,但没有它,代码不起作用。 我确保括号中的项目与代码的其余部分匹配10次以上,我花了1个多小时试图弄清楚这一点,我很乐意为您提供任何建议。
编辑: 这是代码,有很多,所以这里是重要的部分。
else if (userChoice.equalsIgnoreCase(toTrainee))
{
Scanner in = new Scanner(System.in);
Scanner choice = new Scanner (System.in);
System.out.println("To select a Trainee to edit, enter a value from 0 to 3: \n");
int selection = choice.nextInt();
if(selection <= 8)
{
System.out.println("Incorrect, please enter a valid number!");
sc.nextLine();
break;
}
System.out.println("Please enter a first name for the Trainee \n");
String firstName = in.nextLine();
System.out.println("Please enter a Last name for the Trainee \n");
String lastName = in.nextLine();
System.out.println("Please enter a Street Address for the Trainee: \n");
String streetAddress = in.nextLine();
System.out.println("Please enter a Post code for the Trainee: \n");
int postCode = in.nextInt();
in.nextLine();
System.out.println("Please enter a Phone Number for the Trianee: \n");
String phoneNumb = in.nextLine();
System.out.println("Please enter a performance average for the Trainee: \n");
String performanceAverage = in.nextLine();
System.out.println("Please enter a Training area for the Trainee: \n");
String trainingArea = in.nextLine();
Person aTrainee = new Trainee (firstName,lastName,streetAddress,postCode,phoneNumb,performanceAverage,trainingArea);
System.out.println(aTrainee);
myWorker.set(selection,aTrainee);
}
实习生构造函数
public class Trainee extends Person
{
private String performanceAverage, trainingArea;
public Trainee()
{
super();
performanceAverage = "";
trainingArea = "";
}
public Trainee(String myFirstName, String myLastName, String myStreetAddress, int myPostCode, int myPhoneNumb, String myPerformanceAverage, String myTrainingArea)
{
super(myFirstName,myLastName,myStreetAddress,myPhoneNumb,myPostCode);
performanceAverage = myPerformanceAverage;
trainingArea = myTrainingArea;
}
public void setPerformanceAverage(String myPerformanceAverage)
{
this.performanceAverage = myPerformanceAverage;
}
public void setTrainingArea(String myTrainingArea)
{
this.trainingArea = myTrainingArea;
}
public String toString()
{
return super.toString() + ", Performance Average is " + performanceAverage + ", and Training Analysis is " + trainingArea;
}
}
这是Person,它为Trainee提供了更多名称等等。
public class Person
{
private String firstName;
private String lastName;
private String streetAddress;
private int postCode;
private int phoneNumb;
public Person()
{
firstName = lastName = streetAddress = "";
postCode = phoneNumb = 0;
}
public Person(String myFirstName, String myLastName, String myStreetAddress, int myPostCode, int myPhoneNumb)
{
firstName = myFirstName;
lastName = myLastName;
streetAddress = myStreetAddress;
postCode = myPostCode;
phoneNumb = myPhoneNumb;
}
public void setFirstName(String myFirstName)
{
this.firstName = myFirstName;
}
public String getFirstName()
{
return firstName;
}
public void setLastName(String myLastName)
{
this.lastName = myLastName;
}
public String getLastName()
{
return lastName;
}
public void setStreetAdress(String myStreetAddress)
{
this.streetAddress = myStreetAddress;
}
public String getStreetAdress()
{
return streetAddress;
}
public void setPostCode(int myPostCode)
{
this.postCode = myPostCode;
}
public int getPostCode()
{
return postCode;
}
public void setPhoneNumb(int myPhoneNumb)
{
this.phoneNumb = myPhoneNumb;
}
public int getPhoneNumb()
{
return phoneNumb;
}
public String toString()
{
return "This person's information is: " + firstName + " " + lastName + ", " + " " + streetAddress + " " + postCode + ", phone number is " + phoneNumb;
}
}
这是数组(注意第一行只是一个测试,所以忽略条目)
import java.util.ArrayList;
public class ShopMan
{
public static void main()
{
ArrayList<Person>myWorker = new ArrayList<Person>();
Person aShopEmployee = new ShopEmployee("Yazz","Hasan","1/43", 4215, 55271095, "Timber", 010101001, 50000);
myWorker.add(aShopEmployee);
aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);
myWorker.add(aShopEmployee);
aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);
myWorker.add(aShopEmployee);
aShopEmployee = new ShopEmployee("","","",00,00,"",00,00);
Person aManagement = new Management("","","",00,00,"",00,00,false);
myWorker.add(aManagement);
aManagement = new Management("","","",00,00,"",00,00,false);
myWorker.add(aManagement);
aManagement = new Management("","","",00,00,"",00,00,false);
myWorker.add(aManagement);
Person aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
aTrainee = new Trainee("", "", "", 00, 00, "", "");
myWorker.add(aTrainee);
for (int i=0; i < myWorker.size(); i++)
{
System.out.println(myWorker.get(i));
}
while (true)
{
Menu.main(myWorker);
}
}
}
找到答案!
String phoneNumb = in.nextLine();
需要
int phoneNumb = in.nextInt();
in.nextLine();
非常感谢你 Russell Zahniser 和creinig 和其他人
答案 0 :(得分:2)
检查phoneNumb
参数的类型。