我是java初学者。我正在做第 90 页第 4 章的练习,来自 java headfirst 一书。我对我的输出感到困惑。为什么在书中,在这个练习的解决方案部分正确输出 (x < 9) 和 (index < 5) = 14 1. 为什么我有输出 81?
请告知我做错了什么或解释我做错了什么。
public class Mix4 {
int counter = 0;
public static void main(String[] args) {
int count = 0;
Mix4[] m4a = new Mix4[20];
int x = 0;
while (x < 9){
m4a[x] = new Mix4();
m4a[x].counter = m4a[x].counter + 1;
count = count + 1;
count = count = m4a[x].maybeNew(x);
x = x + 1;
}
System.out.println(count + " " + m4a[1].counter);
}
public int maybeNew(int index){
if (index < 5){
Mix4 m4 = new Mix4();
m4.counter = m4.counter + 1;
return counter;
}
return index;
}
}
错误在第 15 行和第 27 行:
线路:
count = count = m4a[x].maybeNew(x);
应该替换为
count = count + m4a[x].maybeNew(x);
线路:
return index;
应替换为:
return 0;
这将为您提供 14 1 的输出
这是根据您提到的书籍示例(Java Head First,第 90 页)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句