java随机扔色子代码 java掷骰子代码

完成一个java project的构建,创建一个“骰子”类,命名为Dice?Dice代码如下:
import java.util.Random;
//(1)创建一个“骰子”类 , 命名为Dice 。
public class Dice {
//提示:初始化两个Dice对象 。
//(2)“骰子”类有两个属性:①最大值为固定值6,②点数为1-6之间的整数 。属性均设置为private 。
private static int max = 6;
private int point;
//(3)“骰子”类有两个构造函数:①无形参的构造函数,将点数默认值设置为1;②有1个形参的构造函数,将形参赋值给点数 。
public Dice() {
this.point = 1;
}
public Dice(int point) {
this.point = point;
}
//(4)自动生成骰子点数的get和set方法 。
public static int getMax() {
return max;
}
public static void setMax(int max) {
Dice.max = max;
}
public int getPoint() {
return point;
}
public void setPoint(int point) {
this.point = point;
}
// (5)编写一个表示“掷骰子”的方法,将点数和函数返回值设置为1-6之间的随机整数 。
//提示:Math.random() //随机选取=0.0且1.0的double值
//double转int的方法:(int) double值 //转换后会舍弃小数点后面的值
public int throwDice() {
int result = 0;
while (true) {
int random = (int) (Math.random() * 10);
if (random0random = max) {
result = random;
break;
}
}
return result;
}
}
测试类方法如下:
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
//(6)新建一个类,在其main()函数中调用Dice类,实现以下功能:
public class TestDice {
public static void main(String[] args) {
// ①掷两个骰子,显示每个骰子的点数,以及点数之和;
Dice dice = new Dice();
int one = dice.throwDice();
int two = dice.throwDice();
System.out.println("两次点数之和:"(onetwo));
// ②输入设置两个骰子的点数,显示两个骰子的点数之和 。
Dice dice2 = new Dice(2);
Dice dice3 = new Dice(6);
System.out.println("所设置的点数之和:"(dice2.getPoint()dice3.getPoint()));
// ③连续10次掷两个骰子,显示每次掷骰子的结果,以及两个骰子10次点数的平均值 。
Dice dice4 = new Dice();
ListInteger points1 = new ArrayList();
ListInteger points2 = new ArrayList();
for (int i = 0; i10; i) {
int first = dice4.throwDice();
System.out.println("第一个骰子掷:"(i1)"次点数是:"first);
int second = dice4.throwDice();
System.out.println("第二个骰子掷:"(i1)"次点数是:"second);
points1.add(first);
points2.add(second);
}
long sum1 = points1.stream().reduce(Integer::sum).orElse(0);
System.out.println("第一个骰子10次点数的平均值:"new BigDecimal(Long.valueOf(sum1).toString()).divide(new BigDecimal(Integer.valueOf(points1.size()).toString())));
long sum2 = points2.stream().reduce(Integer::sum).orElse(0);
System.out.println("第二个骰子10次点数的平均值:"new BigDecimal(Long.valueOf(sum2).toString()).divide(new BigDecimal(Integer.valueOf(points2.size()).toString())));
}
}
java中编程实现如下的骰子游戏:丢下两个骰子,若分值的总值为7点 , 则“赢”;否则“输” 。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 = https://www.04ip.com/post/faceValue;
}
public void roll() {
this.faceValue = https://www.04ip.com/post/(int) (Math.random() * 61);
}
}
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;
【java随机扔色子代码 java掷骰子代码】} else {
return false;
}
}
}
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 = https://www.04ip.com/post/faceValue;
}
public void roll() {
this.faceValue = https://www.04ip.com/post/(int) (Math.random() * 61);
}
}
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怎么写出当骰子点数为6时,在掷一遍的代码加一个判断就好java随机扔色子代码了java随机扔色子代码,比如这样写
public void Dice(){
Random random = new Random();
int count = random.nextInt(6)1;//这里的骰子点数用随机数生成一个[1java随机扔色子代码,6]之间的整数
//这里写java随机扔色子代码你的代码逻辑
if(count == 6){
Dice();//再掷一次
}
//这里写你的代码逻辑
}
java随机扔色子代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java掷骰子代码、java随机扔色子代码的信息别忘了在本站进行查找喔 。

    推荐阅读