2015-11-06 135 views
-1

那裏有一個錯誤。我試圖寫一個繼續循環,這不起作用(或跳回)。我是begginer soo,讀後請不要生氣。請幫幫我!用「繼續」或回到條件循環

somepoint: { Random first = new Random(); 
     int b = first.nextInt(8); 
     if (b==0) 
     { 
      x=x+20; 
      for (int c=0; c<100; c++) 
      { 
       if (x>=950) 
       { 
        JOptionPane.showMessageDialog(null, "Winner is white car!"); 
        return; 
       } 
       else 
       { 
        continue somepoint; //cannot be used outside, help 
       } 
      } 
     } 
     else if (b==1) 
     { 
      x2=x2+20; 
      for (int c=0; c<100; c++) 
      { 
       if (x2>=950) 
       { 
        JOptionPane.showMessageDialog(null, "Winner is red car!"); 
        return; 
       } 
       else 
       { 
        continue somepoint; //cannot be used outside, 
       } 
      } 
     } 
     else if (b==2) ... 
+0

人們通常認爲這是不好的做法跳到這樣的代碼。您是否嘗試過使用其他循環結構(如while循環)來包含代碼? – phflack

+0

@phflack noo,okey我會試試這個 –

回答

0

跳這樣的代碼是一個非常糟糕的做法。你有沒有考慮使用遞歸方法?

如果你想正確使用continue,你應該閱讀這個question