简体   繁体   English

大米之谜-为什么“ If else”语句不能正确循环?

[英]The rice riddle - why does the “If else” statement not loop correctly?

I am sure you all know that "rice riddle" where you put a single grain of rice on the first field of a chess board and then double the amount for every subsequent field. 我相信你们都知道“米饭之谜”是在棋盘的第一个区域上放一粒米,然后在随后的每个区域加一粒米。 My teacher has added a twist: every time the amount hits a certain threshold, we are to convert the amount into a different unit (eg 10 grains are 1 gram, 1000 grams are 1 kilo,...). 我的老师加了一个道理:每当量达到某个阈值时,我们就将其转换成不同的单位(例如10克等于1克,1000克等于1公斤,...)。

Here's my code: 这是我的代码:

public class Test_korn
{

    public static void main(String[] args)
    {   

     double grain ,gram, kilo, ton, container, ship;    
     int i; 

     grain = 1; 
            i = 1; 


    System.out.println( i  + ". field:  " + (int) grain + " grains" );            

     while(i <= 63)  // 2^0 = 1  bis  2^63!
        {   
        grain *=  2 ; 

        if (grain/100 <= 1)  
            {
            System.out.println( (i+1)  + ". field:  " + (int) grain + " grains" );  
            }

        else if( grain/100 > 1 && grain/10_000 <= 1 )   
            {
            gram = grain/10;    
            System.out.println( (i+1) + ". field:  " + gram + " grams" );   
            }

        else if( grain/10_000 > 1 && grain/(10_000 * 1000) <= 1)  
            {
            kilo = grain/(10 * 1000);   
            System.out.println( (i+1)  + ". field:  " + kilo + " kilogram" );       
            }

        else if( grain/(10_000 * 1000) > 1  &&  grain/(10_000 * 1000 * 1000)  <= 1 )  
            {
            ton = grain/( 10 * 1000 * 1000);    
            System.out.println( (i+1)  + ". field:  " + ton + " tons" ); 
            }

        else if(   grain/(10_000 * 1000 * 1000)  > 1   &&  grain/(10_000 * 1000 * 1000 * 1000)  <= 1  )    
            {
            container = grain/(10 * 1000 * 1000 *1000);     
            System.out.println( (i+1)  + ". field:  " + container + " container" );     
            }

        else if(  grain/(10_000 * 1000 * 1000 * 1000)  > 1 )   
            {
            ship = grain/(10000 * 1000 * 1000 * 1000 * 1000);   
            System.out.println( (i+1)  + ". field:  " + ship + "container ship" );                  
            }   

        i++;    
        }       
    }
} 

And here is the output: 这是输出:

1. field:  1 grains
2. field:  2 grains
3. field:  4 grains
4. field:  8 grains
5. field:  16 grains
6. field:  32 grains
7. field:  64 grains
8. field:  12.8 gram
9. field:  25.6 gram
10. field:  51.2 gram
11. field:  102.4 gram
12. field:  204.8 gram
13. field:  409.6 gram
14. field:  819.2 gram
15. field:  1.6384 kilos
16. field:  3.2768 kilos
17. field:  6.5536 kilos
18. field:  13.1072 kilos
19. field:  26.2144 kilos
20. field:  52.4288 kilos
21. field:  104.8576 kilos
22. field:  209.7152 kilos
23. field:  419.4304 kilos
24. field:  838.8608 kilos
25. field:  1.6777216 tons
26. field:  3.3554432 tons
27. field:  6.7108864 tons
28. field:  13.4217728 tons
29. field:  26.8435456 tons
30. field:  53.6870912 tons
31. field:  107.3741824 tons
32. field:  1.145373833409067 container ships
33. field:  2.290747666818134 container ships
34. field:  4.581495333636268 container ships
35. field:  9.162990667272537 container ships
36. field:  18.325981334545073 container ships
37. field:  36.651962669090146 container ships
38. field:  73.30392533818029 container ships
39. field:  146.60785067636058 container ships
40. field:  293.21570135272117 container ships
41. field:  586.4314027054423 container ships
42. field:  1172.8628054108847 container ships
43. field:  2345.7256108217694 container ships
44. field:  4691.451221643539 container ships
45. field:  9382.902443287077 container ships
46. field:  18765.804886574155 container ships
47. field:  37531.60977314831 container ships
48. field:  75063.21954629662 container ships
49. field:  150126.43909259324 container ships
50. field:  300252.8781851865 container ships
51. field:  600505.756370373 container ships
52. field:  1201011.512740746 container ships
53. field:  2402023.025481492 container ships
54. field:  4804046.050962984 container ships
55. field:  9608092.101925967 container ships
56. field:  1.9216184203851935E7 container ships
57. field:  3.843236840770387E7 container ships
58. field:  7.686473681540774E7 container ships
59. field:  1.5372947363081548E8 container ships
60. field:  3.0745894726163095E8 container ships
61. field:  6.149178945232619E8 container ships
62. field:  1.2298357890465238E9 container ships
63. field:  2.4596715780930476E9 container ships
64. field:  4.919343156186095E9 container ships

As you can see, it works as intented until the loop hits the 32nd iteration and than it goes all wrong. 如您所见,它按预期工作,直到循环达到第32次迭代为止,然后全部出错。 After looking at the code for 1 hour all I've found is that the loop runs through all iterations, but the compiler seems to have trouble with both else if s for tonnage and container. 在看了1个小时的代码后,我发现循环遍历了所有迭代,但是else if s用于吨位和容器,则编译器似乎在else if都遇到了麻烦。 It seems that it cannot deal with the logical expression behind the && --but why? 看来它不能处理&&背后的逻辑表达式-但是为什么呢? After all, it works great the other times. 毕竟,它在其他时间效果很好。

It would be great if anyone could point me into the right direction, as I am currently completely lost. 如果有人能指出正确的方向,那将非常好,因为我目前完全迷路了。

Although you declared grain as a double which can go well beyond 2^63, your calculations like 10_000 * 1000 * 1000 will be registered as int which only range to 2,147,483,647 leading to an overflow and these high unrecognizable values. 尽管您将grain声明为可以远远超过2 ^ 63的double,但是您的计算(如10_000 * 1000 * 1000将被注册为int ,其范围仅为2,147,483,647,从而导致溢出和这些无法识别的高值。 They have to be calculated as double so just add a .0 to all your multipliers, eg 10_000.0, 1000.0 and so on). 它们必须计算为double因此只需向所有乘数添加.0(例如10_000.0、1000.0等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM