私は大学の部に関数を書く必要があります:java - ファンクションのクラス
追加機能は講師を追加します。 アクションは、追加の講師を追加する場所がない場合はfalseを返し、講師が追加された場合は同じことを返します。私がこれまでに書いた何
:
はpublic boolean newLecturer(Lecturer[] AllLecturer) {
int MaxLecturer = 0;
MaxLecturer = this.maxLecturer;
int sum = 0;
sum += 1;
if (sum < MaxLecturer) {
System.out.println("true");
return true;
}
else {
System.out.println("false");
return false;
}
}
機能が正しく動作しませんが、それは常にtrueを返します(理由は合計よりも常に大きくなっている最大講師)。
メイン:
パブリッククラス
public static void main(String[]args){
Lecturer[] L1 = new Lecturer[]{new Lecturer("David",3,"Banana",1001)};
Lecturer[] L2 = new Lecturer[]{new Lecturer("Yossi",5,"apple",1002)};
Lecturer[] L3 = new Lecturer[]{new Lecturer("Y",2,"t",1003)};
College myCollege = new College("College1",20,L1,3);
//System.out.println(myCollege);
//myCollege.allLecturer=L2;
//System.out.println(myCollege);
myCollege.newLecturer(L1);
myCollege.newLecturer(L2);
myCollege.newLecturer(L3);
}
}
クラス・カレッジ(ここでは機能){メイン:
public class College {
public String name;
public int numOfLecturer;
public Lecturer[] allLecturer;
public int maxLecturer;
// constructor
public College(String Name, int NumOfLecturer, Lecturer[] AllLecturer,
int MaxLecturer) {
this.name = Name;
this.numOfLecturer = NumOfLecturer;
this.allLecturer = AllLecturer;
this.maxLecturer = MaxLecturer;
}
public College(String Name) {
this.name = Name;
}
public College(Lecturer[] AllLecturer) {
this.allLecturer = AllLecturer;
}
public boolean newLecturer(Lecturer[] AllLecturer) {
int MaxLecturer = 0;
MaxLecturer = this.maxLecturer;
int sum = 0;
sum += 1;
if (sum < MaxLecturer) {
System.out.println("true");
return true;
}
else {
System.out.println("false");
return false;
}
}
@Override
public String toString() {
String lecturers = "";
for (Lecturer lecturer : allLecturer) {
lecturers += lecturer;
}
return "[Name College: " + name + "] " + " [num Of Lecturer: "
+ numOfLecturer + "]" + " [all Lecturer: " + lecturers + "]"
+ " [max Lecturer " + maxLecturer + "]";
}
}
クラス講師:
public class Lecturer {
public String name;
public int numOfTimesPenFalls;
public String favoriteIceCream;
public int autoNumber;
// constructor
public Lecturer(String Name, int NumOfTimesPenFalls,
String FavoriteIceCream, int AutoNumber) {
this.name = Name;
this.numOfTimesPenFalls = NumOfTimesPenFalls;
this.favoriteIceCream = FavoriteIceCream;
this.autoNumber = AutoNumber;
}
public Lecturer(String Name) {
this.name = Name;
}
@Override
public String toString() {
return "[name: " + name + "] " + " [num Of Times Pen Falls: "
+ numOfTimesPenFalls + "] " + " [favorite Ice Cream: "
+ favoriteIceCream + "] " + " [auto Number: " + autoNumber
+ "]";
}
}
そして最後にどのようにすることができますそれを印刷する? このようにコンパイラエラーが発生します。
myCollege.newLecturer("David",2,"Apple",1004);
ありがとうございます。
ありがとうございます。私はこのように追加しようとします:myCollege.addLecturer( "David"、4、 "Banana"、1004);しかし、私はエラーコンパイラ – liran123
を取得してください。息を飲んで、コンパイラのエラーを読んでください。あなたはそのメソッドに4つの引数を渡します。あなたは何をするべきか?ヒント:新しい講師を作成し、その講師に渡します。 – duffymo
まだエラーがあります: 私は新しい講師を作成します>>講師[] L4 =新しい講師[] {新規講師( "David"、3、 "Banana"、 1001)}; >> myCollege.addLecturer(L4); stiilエラー – liran123