java制作筛子的代码 如何编筛子

java怎么写出当骰子点数为6时,在掷一遍的代码

加一个判断就好了,比如这样写

创新互联专注于企业全网营销推广、网站重做改版、蓝田网站定制设计、自适应品牌网站建设、H5开发成都做商城网站、集团公司官网建设、外贸网站建设、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为蓝田等各大城市提供网站开发制作服务。

public

void

Dice(){

Random

random

=

new

Random();

int

count

=

random.nextInt(6)

+

1;//这里的骰子点数用随机数生成一个[1,6]之间的整数

//这里写你的代码逻辑

if(count

==

6){

Dice();//再掷一次

}

//这里写你的代码逻辑

}

完成一个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 (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())));

}

}

Java如何编写骰子程序,急!

import java.util.Random;

public class T1227 {

public static void main(String[] args) {

Random r = new Random();

int num = 0;

while(true){

num = r.nextInt(7);

if(num!=0)break;

}

System.out.println("点数为:"+num);

}

}

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;

}

}

}

写一个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制作筛子的代码 如何编筛子
标题路径:http://myzitong.com/article/hgcjjp.html