登录 | 首页 -> 华新鲜事 -> 社会百科 | 切换到:传统版 / 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:05, Long long ago)] [ 传统版 | sForum ][登录后回复]1楼

here1. you can't assume y = 0.

2. it may not be exact 10.0, may be 9.9998 or 10.0001.

3. even=true!!? you need to put if((even=true)){...}. you try compile. see the warning.

4. don't know. you go have a try.
[吴永铮 (8-27 22:22, Long long ago)] [ 传统版 | sForum ][登录后回复]2楼

(引用 吴永铮:here1. you can't assume y = 0. 2. it may not be exact 10.0, may be 9.9998 or 10.0001. 3. even=true!!? you need to put if((even...)但是对第一题问的是i的值不是y的值啊,跟y有没有initialized有关吗?
[tonymmm (8-27 22:27, Long long ago)] [ 传统版 | sForum ][登录后回复]3楼

(引用 tonymmm:但是对第一题问的是i的值不是y的值啊,跟y有没有initialized有关吗? )看错了。loop完了i就不存在了。the scope of variable i is just the loop.

if you don't know what is "scope", theck your text book
[吴永铮 (8-27 22:31, Long long ago)] [ 传统版 | sForum ][登录后回复]4楼

come in1。what is i after the following for loop?
for (int i=0;i<10;++i){
y+=i;
}
为什莫答案是undefined?不应该是10吗?

>> i is only defined within the for loop structure

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.
为啥?不懂~~~~

>> floating number is represented in IEEE 754 standards, it means that for any number of float type in java, the system keeps only the first 23 bit after decimal point and thus there is some error in magnitude....just search on google use IEEE 754 to find out more details

3。analyze the following code:
boolean even=false;
if(even=true){
system.out.println(" it is even!");
}
写得有错吗?结果是什莫?

>>true is first assign to variable even, and then the value of variable even is evaluated (it's true by now).
>> = is not equivalent to == , the first one is assignment and the second is comparison....


4。even=number%2==0
有这种写法么? 是什莫意思呢?计算机看到这个expression后又是怎样工作的?

>> number % 2 == 0 is first evaluated, it may be true or false and this boolean value is then assigned to variable even. for additional knowledge check out "java operator precedence"
[想念老乡 (8-27 23:16, Long long ago)] [ 传统版 | sForum ][登录后回复]5楼

(引用 想念老乡:come in1。what is i after the following for loop? for (int i=0;i> i is only defined within the for loop structure 2。analyze th...)for 2, more details heresorry for my careless mistake you are actually using double here which is 64 bits in java, but the argument is the same. you can use the following program to find out what exactly happens:

class T {

public static void main (String args[]) {
double d = 0;
for(; d < 10.1; d += 0.1){
if(d > 9.8){
System.out.print(Long.toBinaryString(Double.doubleToLongBits(d))+" == >"+d+"\n");
}
}

System.out.print("\n\n"+Long.toBinaryString(Double.doubleToLongBits(10))+" == >10\n");
}
}


The output is like:

100000000100011110011001100110011001100110011001100110011000010 == >9.89999999999998
100000000100011111111111111111111111111111111111111111111110101 == >9.99999999999998
100000000100100001100110011001100110011001100110011001100101000 == >10.09999999999998


100000000100100000000000000000000000000000000000000000000000000 == >10


so ......
[想念老乡 (8-27 23:44, Long long ago)] [ 传统版 | sForum ][登录后回复]6楼

(引用 想念老乡:for 2, more details heresorry for my careless mistake you are actually using double here which is 64 bits in java, but the argum...)唔,第1,3,4题俺都懂了。谢谢学长....第2题嘛,不好意思,没明白。可能俺还没学到那的缘故。是不是这个意思:计算机对于double 的数在represent时不会很精确,所以永远不会正好出现10.0, 因此就成了一个infinite loop?[tonymmm (8-28 0:45, Long long ago)] [ 传统版 | sForum ][登录后回复]7楼


<<始页  [1]  末页>> 
登录 | 首页 -> 华新鲜事 -> 社会百科 | [刷新本页] | 切换到:传统版 / sForum