我'm a little bit confused about a homework assignment I'正在努力 . 我想要写123,但只是单词偶数,没有数字,然后转到下一组 .
我使用while循环来跟踪计数集和for循环来进行计数123.但问题是程序写入它只是连续打印 .
我尝试将if条件放在for循环中但是只能连续打印(偶数为123) .
我错误地应用了if条件吗?我错误地申请了条件吗?
import acm.program.*;
public class TestProgram extends ConsoleProgram
{
public void run()
{
int i = 1;
while (i <= 7)
{
if (i % 2 == 0)
{
println("even");
}
else
{
for (int j = 1; j <= 3; j++)
{
println(j);
}
}
}
}
}
2 回答
你根本没有递增我,所以它永远不会终止 . 在你的for语句之后,添加类似的东西
你写的是一个无限循环,因为
i
将始终小于或等于7.它初始化为1
而你没有做任何改变它 .