不知道爲什麼我的代碼被無限循環捕獲..也許我寫的代碼錯了?或縮進? 輸出沒有給出任何錯誤它陷入了無限循環
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