snakeeyes.java
来自「java源程序 对初学者有很大的帮助 从简单到复杂」· Java 代码 · 共 35 行
JAVA
35 行
//********************************************************************
// SnakeEyes.java Author: Lewis/Loftus
//
// Demonstrates the use of a class with overloaded constructors.
//********************************************************************
public class SnakeEyes
{
//-----------------------------------------------------------------
// Creates two die objects, then rolls both dice a set number of
// times, counting the number of snake eyes that occur.
//-----------------------------------------------------------------
public static void main (String[] args)
{
final int ROLLS = 500;
int snakeEyes = 0, num1, num2;
Die die1 = new Die(); // creates a six-sided die
Die die2 = new Die(20); // creates a twenty-sided die
for (int roll = 1; roll <= ROLLS; roll++)
{
num1 = die1.roll();
num2 = die2.roll();
if (num1 == 1 && num2 == 1) // check for snake eyes
snakeEyes++;
}
System.out.println ("Number of rolls: " + ROLLS);
System.out.println ("Number of snake eyes: " + snakeEyes);
System.out.println ("Ratio: " + (float)snakeEyes/ROLLS);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?