For 循环不打印我期望的内容。需要帮助
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)));
}
如果 marsHeight 大于 1000
(目前确实如此),我希望我的程序打印出来:
[marsHeight]m:
[marsHeight - 100]m:
...
1000m:
900m:
800m:
...
0m: // this works perfectly!
目前,如果 marsHeight 小于 1000(如 990),程序会打印出:
900m: *
800m:
...
0m:
-100m:
如果小于1000m我要的是这个:
1000m:
900m: *
800m:
...
0m:
我是编程新手。我的逻辑哪里出了问题?
改变这个
int chartHeight = (marsHeight >= 1000) ? marsHeight : 1000;
到
int chartHeight = (marsHeight <= 1000) ? marsHeight : 1000;
^
并且如果您希望 的输出在两种情况下都相同,比如它更大或更小。你可以点赞Not Equal to
点赞
int chartHeight = (marsHeight =! 1000) ? marsHeight : 1000;
^^
// 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);
输出将是:
如果身高 990
1000m
900m*
...
0m
如果身高 >1000
1100m*
1000m
...
0m
如果身高540
1000m
...
500m*
...
0m
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)));
}
如果 marsHeight 大于 1000
(目前确实如此),我希望我的程序打印出来:
[marsHeight]m:
[marsHeight - 100]m:
...
1000m:
900m:
800m:
...
0m: // this works perfectly!
目前,如果 marsHeight 小于 1000(如 990),程序会打印出:
900m: *
800m:
...
0m:
-100m:
如果小于1000m我要的是这个:
1000m:
900m: *
800m:
...
0m:
我是编程新手。我的逻辑哪里出了问题?
改变这个
int chartHeight = (marsHeight >= 1000) ? marsHeight : 1000;
到
int chartHeight = (marsHeight <= 1000) ? marsHeight : 1000;
^
并且如果您希望 的输出在两种情况下都相同,比如它更大或更小。你可以点赞Not Equal to
点赞
int chartHeight = (marsHeight =! 1000) ? marsHeight : 1000;
^^
// 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);
输出将是:
如果身高 990
1000m
900m*
...
0m
如果身高 >1000
1100m*
1000m
...
0m
如果身高540
1000m
...
500m*
...
0m