2016-07-07 109 views
-2

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

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); 
    } 
    } 
} 

}

+2

此代碼不能編譯。 –

+0

*「or indentation」*這隻會導致StackOverflow用戶出現問題或代碼可讀性問題,而不是JVM本身。 – Tom

+0

這裏沒有無限循環。你將循環6次並完成(在代碼被固定到足以編譯之後)。你沒有做任何事情來影響循環索引。什麼讓你覺得你有一個無限循環? – azurefrog

回答

1

我也不清楚,但我認爲這個代碼應該很好地工作:

import java.util.Scanner; 
import java.util.Random; 
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.println("heads"); 
        heads++; 
       } else { 
        System.out.println("Lost"); 
       } 
      } else { 
       System.out.print(heads); 
      } 
     } 
    } 
} 
+0

它沒有。回答'N'仍然會問「你想繼續嗎?」反覆。 ---另外,你沒有解釋你改變了什麼以及你爲什麼改變了。總之,答案並不是那麼有用。該代碼中至少存在兩個問題。 – Andreas

+0

非常感謝..它確實編譯..我唯一的錯誤是一個額外的cosing大括號..你幫我了很多..我也投票了你up –

1
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.println("heads"); 
        heads++; 
       } 
       else{ 
        System.out.println("Lost"); 
       } 
      } 

      else{ 
       System.out.println(heads); 
       break; 
      } 
     } 
    } 

} 

我不知道是什麼你試圖實現,但是這個代碼編譯,而且當你說你不想繼續時,它會停止循環。使用休息不是最好的做法,但它有效。它仍然只會要求最多5次,因爲這是您放入的限制(由TRIES變量給出)。如果你想運行一個無限循環停止,當用戶不希望繼續下去,你migth想用一個布爾變量,一段時間,像這樣:你正變成輸入字符

boolean stop = false; 
    while(!stop) 
    { 
     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.println("heads"); 
       heads++; 
      } 
      else{ 
       System.out.println("Lost"); 
      } 
     } 

     else{ 
      System.out.println(heads); 
      stop=true; 
     } 

注大寫,然後將它壓縮到'y',這將永遠是錯誤的。

+0

我喜歡它,你用兩種方法解決這個問題..非常感謝!不幸的是我只能選擇一個答案.. –