2017-04-11 4 views
0

if文内にif文があります。私は私のbreak文を入れJavaでのループの停止

while(running) 
    { 
     System.out.println("Your current balance is: " + balance); 
     System.out.println("How much would you like to gamble this round?"); 
     gamble = scan.nextInt(); 

     if(gamble > balance) 
     { 
      System.out.println("Error : Cannot gamble more than your balance"); 
      System.out.println("Enter a new bet less than " + balance); 
      gamble = scan.nextInt(); 
     } 

     System.out.println("Dealing cards..."); 
     if(deck >= 3) 
     { 
     card1 = rand.nextInt(12) + 2; 
     card2 = rand.nextInt(12) + 2; 
     card3 = rand.nextInt(12) + 2; 
     deck = deck - 3; 
     } 
     else 
     { 
      System.out.println("Not enough cards left in the deck"); 
      System.out.println("Ending simulation..."); 
     } 

     System.out.println("Your first card value is : " + card1); 
     System.out.println("Your second card value is : " + card2); 

     if(card1 == card2) 
     { 
      System.out.println("Same value for both cards! You win 2 chips!"); 
      balance = balance + 2; 
      System.out.println("Play again? Type 'Y' for yes or 'N' for no"); 
      playAgain = scan.next(); 
      if(playAgain == "Y") 
      { 
       break; 
      } 
      else 
      { 
       running = false; 
      } 
     } 

最初のコードをお見せしましょう、私はそれがwhileループの先頭に戻る必要があるが、それが唯一のif文1を抜け出しています。私のコードをwhileループの最上位に送るステートメントについて、どうやって行くことができますか?

+0

coninueは私がしたい – stinepike

+1

を継続し続けますこれが重複するとマークされている質問は全く同じ質問ではないことに注意してください。これはブレーク対オンスの誤解であり、コードにネストされたループはありません –

答えて

1

あなたの条件を変更し、休憩の代わりにを続けるを使用してください。

if(playAgain == "Y") 
      { 
       continue;// notice here i changed this to continue   
    } 
0

break完全にループ(forまたはwhile)の外に表示されます。 ifまたはelseブロックには何も行われません。 代わりにcontinue;を使用してください。

continueかかわらず、あなたはループのどこにいるかのループ状態にループして戻り、このサイクルを終了する(もちろん、それだけであなたがそれを置く最も内側のループで動作します)

関連する問題