不知道为什么我的代码被无限循环捕获..也许我写的代码错了?或缩进? 输出没有给出任何错误它陷入了无限循环
public class HelloWorld
{
public static void main(String[] args)
{
//Not sure if it's a better way but I did this way:
final int TRIES = 5;
int heads = 0;
for(int i=0; i<=TRIES; i++)
{
Scanner input = new Scanner(System.in);
System.out.print("Do you wanna continue? Y or N? ");
char c = input.next().charAt(0);
c = Character.toUpperCase(c);
if(c =='y'){
int r = (int) Math.random * 2) + 1; //A number between 1 & 2
if(r == 1)
{
System.out.print("heads");
heads++;
}
else{
System.out.print("Lost");
}
}
else{
System.out.print(heads);
}
}
}
}
此代码不能编译。 –
*“or indentation”*这只会导致StackOverflow用户出现问题或代码可读性问题,而不是JVM本身。 – Tom
这里没有无限循环。你将循环6次并完成(在代码被固定到足以编译之后)。你没有做任何事情来影响循环索引。什么让你觉得你有一个无限循环? – azurefrog