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:
- field: 1 grains
- field: 2 grains
- field: 4 grains
- field: 8 grains
- field: 16 grains
- field: 32 grains
- field: 64 grains
- field: 12.8 gram
- field: 25.6 gram
- field: 51.2 gram
- field: 102.4 gram
- field: 204.8 gram
- field: 409.6 gram
- field: 819.2 gram
- field: 1.6384 kilos
- field: 3.2768 kilos
- field: 6.5536 kilos
- field: 13.1072 kilos
- field: 26.2144 kilos
- field: 52.4288 kilos
- field: 104.8576 kilos
- field: 209.7152 kilos
- field: 419.4304 kilos
- field: 838.8608 kilos
- field: 1.6777216 tons
- field: 3.3554432 tons
- field: 6.7108864 tons
- field: 13.4217728 tons
- field: 26.8435456 tons
- field: 53.6870912 tons
- field: 107.3741824 tons
- field: 1.145373833409067 container ships
- field: 2.290747666818134 container ships
- field: 4.581495333636268 container ships
- field: 9.162990667272537 container ships
- field: 18.325981334545073 container ships
- field: 36.651962669090146 container ships
- field: 73.30392533818029 container ships
- field: 146.60785067636058 container ships
- field: 293.21570135272117 container ships
- field: 586.4314027054423 container ships
- field: 1172.8628054108847 container ships
- field: 2345.7256108217694 container ships
- field: 4691.451221643539 container ships
- field: 9382.902443287077 container ships
- field: 18765.804886574155 container ships
- field: 37531.60977314831 container ships
- field: 75063.21954629662 container ships
- field: 150126.43909259324 container ships
- field: 300252.8781851865 container ships
- field: 600505.756370373 container ships
- field: 1201011.512740746 container ships
- field: 2402023.025481492 container ships
- field: 4804046.050962984 container ships
- field: 9608092.101925967 container ships
- field: 1.9216184203851935E7 container ships
- field: 3.843236840770387E7 container ships
- field: 7.686473681540774E7 container ships
- field: 1.5372947363081548E8 container ships
- field: 3.0745894726163095E8 container ships
- field: 6.149178945232619E8 container ships
- field: 1.2298357890465238E9 container ships
- field: 2.4596715780930476E9 container ships
- 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