Come in please
1. Answer: During this while loop
for (int i=0;i<10;++i){
y+=i;
}
i is a local variable in this loop, which means i only exists within this loop. Before the loop and after the loop, i will no longer be existing in the whole program. So we may use this kind of variable many times in many while or for loops and defined it everytime. eg.
for (int i=0;i<10;++i){
y+=i;
}
int z = y;
for (int i=10; i>0; i--){
z-=i;
}
If you have a loop in another loop, you must use another variable if you have use i for the upper level, like the following example.
for (int i=0; i<10; i++)
{
for (int j = 10; j>0; j--)
{
z+=i*j;
}
}
A kind of stupid bug is mis-using i as j in the statements in the inner loop. The program may compile, but occur a runtime error, even infinite loop. You need to pay much attention of using them.
2. Answer: The program compiles well. But as the answer said, it will be an infinite loop. You can modify the program like
while (d<=10.0)
{
//codes to be added.
}
What's 我心为谁动 said is true, 0.2+0.5 does not equal to 0.7 in the program. You can test with some more cases. That's due to limitation of computer itself.
3. Answer: if (expression) { }. This expression must result in a boolean value. In java, = is used as assignment. == will result a boolean value, means whether the former equals to the latter.
4. Answer: the expression is correct. And here, even must be declared as a boolean.
boolean even;
even = (number%2 ==0);
In another words, if ( number%2==0) even=true; else even=false;
for (int i=0;i<10;++i){
y+=i;
}
i is a local variable in this loop, which means i only exists within this loop. Before the loop and after the loop, i will no longer be existing in the whole program. So we may use this kind of variable many times in many while or for loops and defined it everytime. eg.
for (int i=0;i<10;++i){
y+=i;
}
int z = y;
for (int i=10; i>0; i--){
z-=i;
}
If you have a loop in another loop, you must use another variable if you have use i for the upper level, like the following example.
for (int i=0; i<10; i++)
{
for (int j = 10; j>0; j--)
{
z+=i*j;
}
}
A kind of stupid bug is mis-using i as j in the statements in the inner loop. The program may compile, but occur a runtime error, even infinite loop. You need to pay much attention of using them.
2. Answer: The program compiles well. But as the answer said, it will be an infinite loop. You can modify the program like
while (d<=10.0)
{
//codes to be added.
}
What's 我心为谁动 said is true, 0.2+0.5 does not equal to 0.7 in the program. You can test with some more cases. That's due to limitation of computer itself.
3. Answer: if (expression) { }. This expression must result in a boolean value. In java, = is used as assignment. == will result a boolean value, means whether the former equals to the latter.
4. Answer: the expression is correct. And here, even must be declared as a boolean.
boolean even;
even = (number%2 ==0);
In another words, if ( number%2==0) even=true; else even=false;