2017-02-01 2 views
0

Я пишу программу для класса, в котором игрок и компьютер играют в игру. Игра состоит из выбора 1, 2, 3 или 4 спичек. Игрок забирает последнюю палку из 21 проигрыша. Игра оснащена так, что компьютер всегда выигрывает.Проблемы с простой Java-игрой

В любом случае, моя проблема заключается в логике окончания игры. Вот мой код:

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) является ложным, как я могу идти о пропуская строки после System.out.println ("Вы выбрали последняя спичка ... ВЫ ЛЮС !!! ») ;? Есть ли способ преодолеть средний цикл после выполнения условия? Или моя логика совершенно неправа?

ответ

1

Вы можете просто return от метода main

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

Проклятье !!! Я знал, что это просто. Я все еще новичок в этом и переусердствовал с проблемой. Благодаря! – smj7v3

+0

Если это правильный ответ, вы должны его принять. –

0

Ваша логика полностью wrong.bcz уже ваш давая компьютер выбирает значения в соответствии с игроком values.game начать игроком каждый time.so каждый раз, когда игрок выбрать значение then computer.each 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."); 
       } 
      } 
     } 
    } 

но компьютер всегда побеждает

Смежные вопросы