2人骰子游戏,关于如何比较骰子和得到20分

dluptydi  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(558)

每个玩家掷两个骰子,然后比较每个骰子上的最高数字。人数多的选手得2分。他们比较每卷的最低数字。人数多的选手得1分。如果数字是平局,则不得分。第一个得到20分的玩家获胜。我怎么做比较部分?这是我目前掌握的密码

import java.util.Scanner;
import java.util.Random;

public class DiceGame
{
     public static void main(String[] args) // method 1
          {
      String again = "y";  // To control the loop
      int die1;            // To hold the value of die #1
      int die2;            // to hold the value of die #2
      int die3;
      int die4;                  
      // Create a Scanner object to read keyboard input.
      Scanner keyboard = new Scanner(System.in);

      // Create a Random object to generate random numbers.
      Random rand = new Random();

      // Simulate rolling the dice.
      while (again.equalsIgnoreCase("y"))
      {
         System.out.println("Rolling the dice...");
         die1 = rand.nextInt(6) + 1;
         die2 = rand.nextInt(6) + 1;
         System.out.println("Player 1's values are:");
         System.out.println(die1 + " " + die2);

         die3 = rand.nextInt(6) + 1;
         die4 = rand.nextInt(6) + 1;
         System.out.println("Player 2's values are:");
         System.out.println(die3 + " " + die4);

         //method 2 = comparing the numbers
          public static void compareRoll(int die1, die2, die3, die4)           
          {

               {
         if(die1 > die2 && die3 > die4)

             else if(die1 < die2 && die3 < die4)

                  else

        }
        }

         //method 3 = getting total number of scores

         System.out.print("Roll them again (y = yes)? ");
         again = keyboard.nextLine();
      }
   }

   }
hwazgwia

hwazgwia1#

我看到这样的东西:

public void compareDices(int die1, int die2, int die3, int die4)
    int firstMax = Math.max(die1, die2);
    int secMax = Math.max(die3, die4);
    if(firstMax > secMax) {
        // add 2 points to first player
    } else if(firstMax < secMax) {
        // add 2 points to sec player
    }

    int firstMin = Math.min(die1, die2);
    int secMin = Math.min(die3, die4);
    if(firstMin > secMin) {
        // add 1 point to first player
    } else if(firstMin < secMin) {
        // add 1 point to sec player
    }

相关问题