摇色子代码java java摇骰子( 二 )


sum += diceCount;// 每一次都加上骰子点数
while (ifLoop(sum)) {
sum = changeSum(sum);
}
}
System.out.println(count);
}
public static int changeSum(int sum) {
// 开始判断退格
while (ifLoop(sum)) {
Random r = new Random();
int num = r.nextInt(2);
if (sum == 8) {
if (num == 0) {// 使用随机数模拟随机退4或6
sum -= 4;
} else {
sum -= 6;
}
} else if (sum == 15 || sum == 17 || sum == 19 || sum == 23 || sum == 21 || sum == 25) {
sum = 20;
} else if (sum == 20) {
if (num == 0) {
sum = 70;
} else {
sum = 90;
}
} else if (sum == 28) {
if (num == 0) {// 使用随机数模拟随机进3或5
sum += 3;
} else {
sum += 5;
}
} else if (sum == 30 || sum == 34 || sum == 80) {
if (num == 0) {// 使用随机数模拟随机退3或5
sum -= 3;
} else {
sum -= 5;
}
} else if (sum == 36 || sum == 40) {
sum -= 5;
} else if (sum == 43 || sum == 67 || sum == 81 || sum == 92) {
sum -= 4;
} else if (sum == 47 || sum == 52 || sum == 61 || sum == 69 || sum == 85 || sum == 83) {
sum -= 6;
} else if (sum == 56 || sum == 65 || sum == 91 || sum == 93) {
sum -= 5;
} else if (sum == 72 || sum == 74 || sum == 76 || sum == 78) {
sum = 70;
} else if (sum == 87) {
sum = 80;
} else if (sum == 94 || sum == 95 || sum == 97) {
sum = 90;
} else if (sum == 98) {
if (num == 0) {// 使用随机数模拟随机退2或5
sum -= 2;
} else {
sum -= 5;
}
} else if (sum == 99) {
sum -= 4;
}
}
return sum;
}
public static boolean ifLoop(int sum) {
int array[] = { 8, 15, 17, 19, 20, 21, 23, 25, 28, 30, 34, 36, 40, 43, 47, 52, 56, 61, 65, 67, 69, 70, 72, 74,
76, 78, 80, 81, 83, 85, 87, 90, 91, 92, 93, 94, 95, 97, 98, 99 };
for (int i = 0; iarray.length; i++) {
if (sum == array[i]) {
return true;
} else if (sum == 70) {
System.out.println("乐趣怪摇色子代码java!");
return false;
} else if (sum == 90) {
System.out.println("惊喜怪摇色子代码java!");
return false;
}
}
return false;
}
}
写一个java程序,摇两个骰子,用random,直到两个值相等为止不知道摇色子代码java你说摇色子代码java的是random类还是math.random摇色子代码java,所以写摇色子代码java了两个
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怎么写出当骰子点数为6时,在掷一遍的代码加一个判断就好了,比如这样写
public
void
Dice(){
Random
random
=
new
Random();
int
count
=
random.nextInt(6)
+
1;//这里的骰子点数用随机数生成一个[1,6]之间的整数
//这里写你的代码逻辑
if(count
==
6){
Dice();//再掷一次
}
//这里写你的代码逻辑
}

推荐阅读