日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/9/27 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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问题(随机数的运用)的全部內容,希望文章能夠幫你解決所遇到的問題。

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