這是一本來自教科書的練習課程。我需要弄清楚這個程序的出版物。 這裏的程序:如何計算此程序中的最終輸出印數?
public class EchoTestDrive {
public static void main(String[] args) {
Echo e1 = new Echo();
Echo e2 = new Echo();
int x = 0;
while (x < 4) {
e1.hello();
e1.count = e1.count + 1;
if (x == 3) {
e2.count = e2.count + 1;
}
if (x > 0) {
e2.count = e2.count + e1.count;
}
x = x + 1;
}
System.out.println(e2.count);
}
}
class Echo {
int count = 0;
void hello() {
System.out.println("helloooo... ");
}
}
對這一計劃的絕版的答案是:
helloooo...
helloooo...
helloooo...
helloooo...
10
我不太明白這是如何計算爲主。它似乎像x循環4次。 X = 0; X = 1; X = 2; X = 3。並且e1應該具有值1,2,3,4,因爲e1.count = e1.count + 1。 然後我很困惑,在這種情況下如何計算e2?
請正確縮進您的代碼。 – arshajii
你到底想做什麼?你能發佈你的預期結果嗎? – Andromeda
最好的方法是在IDE中調試代碼。如果不寫入控制檯,while循環內的值可以看到循環如何執行 –