登录 | 首页 -> 华新鲜事 -> 求学狮城 | 切换到:传统版 / sForum | 树形列表
不好意思,还想问学长几个问题~~
<<始页  [1]  末页>> 

不好意思,还想问学长几个问题~~1。what is i after the following for loop?
for (int i=0;i<10;++i){
y+=i;
}
为什莫答案是undefined?不应该是10吗?
2。analyze the following fragment:
double sum=0;
double d=0;
while(d!=10.0){
d+=0.1;
sum+=sum+d;
}
答案:the program may not stop because of the phenomenon referred t as numerical inaccuracy for uperating with floating-pointing numbers.
为啥?不懂~~~~
3。analyze the following code:
boolean even=false;
if(even=true){
system.out.println(" it is even!");
}
写得有错吗?结果是什莫?
4。even=number%2==0
有这种写法么? 是什莫意思呢?计算机看到这个expression后又是怎样工作的?
多谢学长的帮助!!
[tonymmm (8-27 22:06, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

我的一点想法1. the three parts of for loop
part I: initialization
part II: governing condition (for the loop to continue)
part III: post condition, i.e. what's going to happen after each loop
however, ++i requires that increase i by 1 before entering a loop, this conflict with part III definition.

2. because the way float(double) numbers are represented in computer, u will find some floating point computing "wrong": 0.5 + 0.2 /= 0.7.

3. if (even==true)
in C, you can write if (var = somevalue), which will assign somevalue to var and return true, but in java, this is not allowed.

4. even = number%2==0 <==> even = (number%2==0)
sequence of actions taken by computer:
(1) computer number%2
(2) compare the previous result and 0 and return comparision result as a boolean value
(3) assign the boolean value to even.

其实每个问题都有想对应的理论(或者说术语)来解释的, 不过我一时间都想不起来是什么, 希望你大概明白吧。
如有纰漏,敬请各位更正。
[我心为谁动 (8-27 23:42, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 我心为谁动:我的一点想法1. the three parts of for loop part I: initialization part II: governing condition (for the loop to continue) ...)第一个问题我说错了,这样就没问题了
int i;
for (i=0; i<10; ++i) {
y += i;
}
[我心为谁动 (8-28 0:12, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

(引用 我心为谁动:第一个问题我说错了,这样就没问题了 int i; for (i=0; i...)嗯嗯,很有道理...尤其是第一题的该法,真得太好了!谢谢谢谢!!!:)[tonymmm (8-28 0:39, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

Come in please1. 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;
[duck (8-28 9:59, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 求学狮城 | [刷新本页] | 切换到:传统版 / sForum