[英]Why I have wrong output java headfirst?
我是Java的初学者。 我正在从Java headfirst开始练习第90页第4章。 我对我的输出感到困惑。 为什么在书中,在本练习的解决方案部分中,输出(x <9)和(index <5)= 141。为什么我输出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 14
根据您提到的书籍示例(Java Head First,第90页)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.