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();//再掷一次
}
//这里写你的代码逻辑
}
推荐阅读
- word中怎么让文字竖着,word怎么让文字竖着并居中
- 怎么用路由器连电视,怎么从路由器连接电视
- 路由器的网络灯晃什么原因,家里网络路由器灯光一闪一闪是什么问题
- 江苏书画电商平台代理,江苏书画展览
- iis运行php数据库 iis和php可以共存吗
- 电影拍摄中期阶段是什么,中期拍摄的内容
- 飞机飞行体验游戏推荐,飞机航行小游戏
- C语言字符取出函数 c语言取字符串中的字符
- 如何用新媒体做好工作总结,如何用新媒体做好工作总结发言