可以有一個人,請幫助我我有這個代碼 的最後一部分編譯錯誤的說法創建構造請幫助這是寫這個<class name><object name> =新的構造函數名的正確方法嗎?
public class Officer {
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of the Soldier: ");
String name = input.nextLine();
System.out.print("Enter the sex of the Soldier: ");
String sex = input.nextLine();
System.out.print("Enter the Age of the Soldier: ");
String age = input.nextLine();
Soldier soldier = new Soldier(name, sex, age);
}
}
package officer;
public class Soldier {
private String soldierName;
private int soldierAge;
private char soldierSex;
public void Soldier(String name, char sex, int age) {
soldierName = name;
soldierSex = sex;
soldierAge = age;
}
public String getSoldierName() {
return soldierName;
}
public char getSoldierSex() {
return soldierSex;
}
public int getSoldierAge() {
return soldierAge;
}
}
你有一個構造函數來匹配'Soldier(name,sex,age)'嗎? – lakshman
print stackTrace here !!!!! – Devavrata
問題可能是,Soldier類中沒有任何構造函數,或像'Soldier(String name,String sex,int age)'' – iMBMT