日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java中随机数边界问题,java 简单Dice问题(随机数的运用)

發布時間:2024/9/27 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中随机数边界问题,java 简单Dice问题(随机数的运用) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

[java]代碼庫/**

* Dice Write a program that simulates rolling two dice using the following

* steps: 1. Prompt the user for the number of sides for two dice. 2. “Roll” the

* dice three times by generating a random number between 1 (inclusive) and the

* number of sides (inclusive). 3. Keep track of the sum of the rolls for each

* die and output the sum and average for each die.

*

* Sample Output: How many sides does die 1 have? 6 How many sides does die 2

* have? 20 Die 1 first roll = 5. Die 2 first roll = 14. Die 1 second roll = 1.

* Die 2 second roll = 20. Die 1 third roll = 3. Die 2 third roll = 9. Die 1

* rolled a total of 9 and rolled 3 on average. Die 2 rolled a total of 43 and

* rolled 14.333 on average.

*

* @author jianfeng

*

*/

public class Dice {

public static void main(String[] args) {

int[][] dice = new int[3][2];

int first = 6;

int second = 20;

for (int i = 0; i < 3; i++) {

dice[i][0] = (int) ((Math.random() * first) + 1); // 第一個骰子三次滾動值

dice[i][1] = (int) ((Math.random() * second) + 1);

; // 第二個骰子三次滾動值

}

// 計算總數

int sum1 = 0;

int sum2 = 0;

for (int i = 0; i < 3; i++) {

sum1 += dice[i][0];

sum2 += dice[i][1];

}

// 計算平均值

float avg1 = (float) sum1 / 3;

float avg2 = (float) sum2 / 3;

// 輸出

System.out.println("How many sides does die 1 have?" + first);

System.out.println("How many sides does die 2 have?" + second);

System.out.println("Die 1 first roll = " + dice[0][0]);

System.out.println("Die 2 first roll = " + dice[0][1]);

System.out.println("Die 1 second roll = " + dice[1][0]);

System.out.println("Die 2 second roll = " + dice[1][1]);

System.out.println("Die 1 third roll = " + dice[2][0]);

System.out.println("Die 2 third roll = " + dice[2][1]);

System.out.println("Die 1 rolled a total of " + sum1 + " and rolled "

+ avg1 + " on average");

System.out.println("Die 2 rolled a total of " + sum2 + " and rolled "

+ avg2 + " on average");

}

}

[代碼運行效果截圖]

總結

以上是生活随笔為你收集整理的java中随机数边界问题,java 简单Dice问题(随机数的运用)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。