「摇骰子java」摇骰子飞是什么意思

博主:adminadmin 2022-11-30 18:40:06 63

今天给各位分享摇骰子java的知识,其中也会对摇骰子飞是什么意思进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

如何用java实现掷骰子?

package me.flyshow.test;

public class Test {

public static void main(String[] argv) {

int[] result = new int[11];

for (int i = 0; i 5000; i++) {

int a = (int) (Math.random() * 6 + 1);

int b = (int) (Math.random() * 6 + 1);

int sum = a + b;

switch (sum) {

case 2:

result[0]++;

break;

case 3:

result[1]++;

break;

case 4:

result[2]++;

break;

case 5:

result[3]++;

break;

case 6:

result[4]++;

break;

case 7:

result[5]++;

break;

case 8:

result[6]++;

break;

case 9:

result[7]++;

break;

case 10:

result[8]++;

break;

case 11:

result[9]++;

break;

case 12:

result[10]++;

break;

default:

break;

}

}

for (int n = 0; n 11; n++) {

System.out.println((n + 2) + "出现了" + result[n] + "次");

写一个java程序,摇两个骰子,用random,直到两个值相等为止

不知道你说的是random类还是math.random,所以写了两个

1. Math.random

public class Test1 {

public static void main(String[] args) {

int a, b;

a = (int)(1+Math.random()*(6));

b = (int)(1+Math.random()*(6));

while (a != b) {

System.out.println("Not equal! a=" + a + ", b=" + b);

a = (int)(1+Math.random()*(6));

b = (int)(1+Math.random()*(6));

}

System.out.println("Equal! a=b=" + a);

}

}

2. random类

import java.util.Random;

public class Test2 {

public static void main(String[] args) {

int a, b;

Random ra = new Random();

a = ra.nextInt(6)+1;

b = ra.nextInt(6)+1;

while (a != b) {

System.out.println("Not equal! a=" + a + ", b=" + b);

a = ra.nextInt(6)+1;

b = ra.nextInt(6)+1;

}

System.out.println("Equal! a=b=" + a);

}

}

java掷骰子(急)

public class Test {

public static void main(String[] args){

DieGame dieGame = new DieGame();

if (dieGame.play()) {

System.out.println("你赢了!");

} else {

System.out.println("你输了!");

}

}

}

class Die {

private int faceValue;

public int getFaceValue() {

return faceValue;

}

public void setFaceValue(int faceValue) {

this.faceValue = faceValue;

}

public void roll() {

this.faceValue = (int) (Math.random() * 6 + 1);

}

}

class DieGame {

private Die die1 = new Die();

private Die die2 = new Die();

public boolean play() {

die1.roll();

System.out.println("第一次点数:" + die1.getFaceValue());

die2.roll();

System.out.println("第二次点数:" + die2.getFaceValue());

if (die1.getFaceValue() + die2.getFaceValue() == 7) {

return true;

} else {

return false;

}

}

}

摇骰子java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于摇骰子飞是什么意思、摇骰子java的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-30,除非注明,否则均为首码项目网原创文章,转载请注明出处。