dimanche 10 juin 2018

the rice riddle - why does the "If else" statement not loop correctly?

I am currently learning Java and so far its going well, however now I got a simple problem that I cannot solve:

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. Now, my teacher has added a twist: everytime the amount hits a certain threshold, we are to convert the amount into a different unit (e.g. 10 grains are 1 gram, 1000 grams are 1 kilo,...). Here's my code (there is probably a more elegant solution I am not aware of):

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 ist 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. After looking at the code for 1 hour all I found is that the loop runs through all iterations, but the compiler seems to have trouble wiht both "else if" for tonnage and container - 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.

Aucun commentaire:

Enregistrer un commentaire