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

2024-06-09 05:12:06 发布

您现在位置:Python中文网/ 问答频道 /正文

每个玩家掷两个骰子 他们比较每卷的最高数字。数字较大的玩家可获得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();
      }
   }

   }

Tags: the玩家数字randomoutsystemintscanner
1条回答
网友
1楼 · 发布于 2024-06-09 05:12:06

我看到这样的情况:

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
    }

相关问题 更多 >