lundi 30 janvier 2017

For loop not printing what I expect. Need assistance

int marsHeight = ml.getHeight() / 100 * 100; // measure by 100s despite height value
int chartHeight = (marsHeight >= 1000) ? marsHeight : 1000;
for (int i = 0; i <= (chartHeight / 100); i++)
{
    if (i == 0)
    {
        Console.WriteLine("{0}m: \t*", (marsHeight - (i * 100))); // in order to print in descending order: (height - (i * 100)
        continue;
    }
    Console.WriteLine("{0}m:", (marsHeight - (i * 100)));
}

If marsHeight is greater than 1000 the program will print out:

[marsHeight]m: 
[marsHeight - 100]m:  
...  
1000m:   
900m:  
800m:   
...   
0m:  // this works perfectly!

If marsHeight is less than 1000 (like 990)the program prints out:

900m: *  
800m:  
...  
0m:  
-100m:

What I want is this:

1000m:  
900m: *  
800m:  
...  
0m:  

I'm new to programming. Where am I going wrong with my logic?

Aucun commentaire:

Enregistrer un commentaire