dice.java

来自「Java Classic Examples是我买的两本书:《JAVA经典实例》和」· Java 代码 · 共 33 行

JAVA
33
字号
import java.util.Random;
import java.io.IOException;

public class Dice
{
  public static void main(String[] args)
  {
    System.out.println("You have six throws of a pair of dice.\n" + 
               "The objective is to get a double six. Here goes...\n");

    Random diceValues = new Random();      // Random number generator
    String[] theThrow = {"First ", "Second ", "Third ",
                         "Fourth ", "Fifth ", "Sixth "};
    int die1 = 0;                          // First die value
    int die2 = 0;                          // Second die value

    for(int i = 0; i < 6; i++)
    {
      die1 = 1 + Math.abs(diceValues.nextInt())%6;  // Number from 1 to 6
      die2 = 1 + Math.abs(diceValues.nextInt())%6;  // Number from 1 to 6
      System.out.println(theThrow[i] + "throw: " + die1 + ", " + die2);

      if(die1 + die2 == 12)                         // Is it double 6?
      {
        System.out.println("    You win!!");        // Yes !!!
        return;
      }
    }
    System.out.println("Sorry, you lost...");
    return;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?