2017-02-01 13 views
0

私は、プレーヤーとコンピュータがゲームをしているクラスのプログラムを書いています。ゲームは、1,2,3、または4個のマッチを選ぶことで構成されています。プレイヤーは21のうち最後のスティックを拾う。ゲームはコンピュータが常に勝つように調整されています。シンプルなJavaゲームに問題がある

とにかく、私の問題はゲームを終了するロジックの中にあります。ここに私のコードです:私はするSystem.out.println後の行を飛ばし行くのですか

public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in); 
    String player; 
    int matchstick; 
    int total = 0; 

    System.out.println("Rules of the game: \nThe player and computer take turns picking 1, 2, 3 , or 4 matchsticks." 
         + " \nThe player to pick the last stick out of 21 loses."); 
    System.out.print("\nHello player. Please enter your name: "); 
    player = keyboard.next(); 

    while (total < 21) { 

     System.out.print("\nHow many matchsticks do you pick, " + player + "? "); 
     matchstick = keyboard.nextInt(); 

     while (matchstick > 4) { 
      System.out.print("You have exceeded the limit of 4 matchsticks. Please try again. \n\nHow many matchsticks do you pick? "); 
      matchstick = keyboard.nextInt(); 
     } 

     total += matchstick; 

     System.out.println(player + " has picked " + matchstick + " matchstick(s) and brings the total to " + total + " matchsticks."); 

     if (total == 21) { 
      System.out.println("You picked the last matchstick... YOU LOSE!!!"); 
     } 

     System.out.println("\nNow it's the computer's turn."); 

     if (matchstick == 1) { 
      total += 4; 
      System.out.println("The computer chooses 4 matchsticks, bringing the total to " + total + " matchsticks."); 
     } 
     if (matchstick == 2) { 
      total += 3; 
      System.out.println("The computer chooses 3 matchsticks, bringing the total to " + total + " matchsticks."); 
     } 
     if (matchstick == 3) { 
      total += 2; 
      System.out.println("The computer chooses 2 matchsticks, bringing the total to " + total + " matchsticks."); 
     } 
     if (matchstick == 4) { 
      total += 1; 
      System.out.println("The computer chooses 1 matchstick, bringing the total to " + total + " matchsticks."); 
     } 

    } 

} 

}

(< 21の合計)whileループの条件が偽である、( "あなたは選びました最後のマッチ棒...あなたは失ってしまった!!! ");条件が満たされた後に中間ループを破る方法はありますか?または私の論理は完全に間違っていますか?完全wrong.bczすでにお使いのコンピュータを与え

答えて

1

することはできだけreturn

... 
if (total == 21) { 
    System.out.println("You picked the last matchstick... YOU LOSE!!!"); 
    return; 
} 
    ... 
+0

くそに勝ちます!私はそれが何か簡単だと分かっていました。私はまだこれにnewbであり、問​​題を過度に思っていた。ありがとう! – smj7v3

+0

これが正しい答えであれば、それを受け入れるべきです。 –

0

main方法からあなたのロジックは、プレーヤーに応じ 値は毎回プレイヤーが選択した各time.soプレイヤーが起動しvalues.game選びます値はcomputer.each time total = 5となります。

ex: player play 4 times + computer play 4 times : total =20 

プレイヤーはプレイする必要があります。プレイヤーは毎回失敗します。

whileループ(合計< 21)の条件が偽の場合?これを試して。

public static void main(String[] args) { 
     Scanner keyboard = new Scanner(System.in); 
     String player; 
     int matchstick; 
     int total = 0; 

     System.out.println("Rules of the game: \nThe player and computer take turns picking 1, 2, 3 , or 4 matchsticks." 
       + " \nThe player to pick the last stick out of 21 loses."); 
     System.out.print("\nHello player. Please enter your name: "); 
     player = keyboard.next(); 

     while (total < 21) { 

      System.out.print("\nHow many matchsticks do you pick, " + player + "? "); 
      matchstick = keyboard.nextInt(); 

      while (matchstick > 4) { 
       System.out.print("You have exceeded the limit of 4 matchsticks. Please try again. \n\nHow many matchsticks do you pick? "); 
       matchstick = keyboard.nextInt(); 
      } 

      total += matchstick; 

      System.out.println(player + " has picked " + matchstick + " matchstick(s) and brings the total to " + total + " matchsticks."); 

      if (total >= 21) { 
       System.out.println("You picked the last matchstick... YOU LOSE!!!"); 
      } 
      else { 
       System.out.println("\nNow it's the computer's turn."); 

       if (matchstick == 1) { 
        total += 4; 
        System.out.println("The computer chooses 4 matchsticks, bringing the total to " + total + " matchsticks."); 
       } 
       if (matchstick == 2) { 
        total += 3; 
        System.out.println("The computer chooses 3 matchsticks, bringing the total to " + total + " matchsticks."); 
       } 
       if (matchstick == 3) { 
        total += 2; 
        System.out.println("The computer chooses 2 matchsticks, bringing the total to " + total + " matchsticks."); 
       } 
       if (matchstick == 4) { 
        total += 1; 
        System.out.println("The computer chooses 1 matchstick, bringing the total to " + total + " matchsticks."); 
       } 
      } 
     } 
    } 

が、コンピュータは常に

関連する問題