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)));
}
I want my program to print out this if marsHeight is greater than 1000
(and it currently does):
[marsHeight]m:
[marsHeight - 100]m:
...
1000m:
900m:
800m:
...
0m: // this works perfectly!
Currently if marsHeight is less than 1000 (like 990)the program prints out:
900m: *
800m:
...
0m:
-100m:
What I want is this if it's less than 1000m:
1000m:
900m: *
800m:
...
0m:
I'm new to programming. Where am I going wrong with my logic?
// First get the value.
int height = ml.getHeight();
// Now round to nearest even value.
int chartHeight = height / 100 * 100;
// Find initial value of cycle.
int forStart;
if (chartHeight > 1000)
forStart = chartHeight;
else
forStart = chartHeight < 0 ? 0 : 1000;
// Also you can simplify cycle.
for (int i = forStart; i >= 0; i -= 100)
if(i==chartHeight)
Console.WriteLine("{0}m:*", i);
else
Console.WriteLine("{0}m:", i);
The output will be:
if height 990
1000m
900m*
...
0m
if height >1000
1100m*
1000m
...
0m
if height 540
1000m
...
500m*
...
0m