
正文
java掷筛子代码 掷骰子代码
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
完成一个java project的构建,创建一个“骰子”类,命名为Dice?
Dice代码如下:
import java.util.Random;
//(1)创建一个“骰子”类java掷筛子代码,命名为Dice。
public class Dice {
// 提示:初始化两个Dice对象。
//(2)“骰子”类有两个属性:①最大值为固定值6java掷筛子代码,②点数为1-6之间java掷筛子代码的整数。属性均设置为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 (random 0 random = 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("两次点数之和:" + (one + two));
// ②输入设置两个骰子的点数,显示两个骰子的点数之和。
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; i 10; i++) {
int first = dice4.throwDice();
System.out.println("第一个骰子掷:" + (i + 1) + "次点数是:" + first);
int second = dice4.throwDice();
System.out.println("第二个骰子掷:" + (i + 1) + "次点数是:" + 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())));
}
}
相关问答
Q1: 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 = 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;
}
}
}
Q2: 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掷筛子代码和掷骰子代码的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。






