java制作篩子的代碼 如何編篩子

java怎么寫出當(dāng)骰子點(diǎn)數(shù)為6時,在擲一遍的代碼

加一個判斷就好了,比如這樣寫

創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、藍(lán)田網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、H5開發(fā)成都做商城網(wǎng)站、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為藍(lán)田等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。

public

void

Dice(){

Random

random

=

new

Random();

int

count

=

random.nextInt(6)

+

1;//這里的骰子點(diǎn)數(shù)用隨機(jī)數(shù)生成一個[1,6]之間的整數(shù)

//這里寫你的代碼邏輯

if(count

==

6){

Dice();//再擲一次

}

//這里寫你的代碼邏輯

}

完成一個java project的構(gòu)建,創(chuàng)建一個“骰子”類,命名為Dice?

Dice代碼如下:

import java.util.Random;

//(1)創(chuàng)建一個“骰子”類,命名為Dice。

public class Dice {

// 提示:初始化兩個Dice對象。

//(2)“骰子”類有兩個屬性:①最大值為固定值6,②點(diǎn)數(shù)為1-6之間的整數(shù)。屬性均設(shè)置為private。

private static int max = 6;

private int point;

// (3)“骰子”類有兩個構(gòu)造函數(shù):①無形參的構(gòu)造函數(shù),將點(diǎn)數(shù)默認(rèn)值設(shè)置為1;②有1個形參的構(gòu)造函數(shù),將形參賦值給點(diǎn)數(shù)。

public Dice() {

this.point = 1;

}

public Dice(int point) {

this.point = point;

}

// (4)自動生成骰子點(diǎn)數(shù)的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)編寫一個表示“擲骰子”的方法,將點(diǎn)數(shù)和函數(shù)返回值設(shè)置為1-6之間的隨機(jī)整數(shù)。

// 提示:Math.random() //隨機(jī)選取=0.0且1.0的double值

// double轉(zhuǎn)int的方法:(int) double值 //轉(zhuǎn)換后會舍棄小數(shù)點(diǎn)后面的值

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()函數(shù)中調(diào)用Dice類,實(shí)現(xiàn)以下功能:

public class TestDice {

public static void main(String[] args) {

// ①擲兩個骰子,顯示每個骰子的點(diǎn)數(shù),以及點(diǎn)數(shù)之和;

Dice dice = new Dice();

int one = dice.throwDice();

int two = dice.throwDice();

System.out.println("兩次點(diǎn)數(shù)之和:" + (one + two));

// ②輸入設(shè)置兩個骰子的點(diǎn)數(shù),顯示兩個骰子的點(diǎn)數(shù)之和。

Dice dice2 = new Dice(2);

Dice dice3 = new Dice(6);

System.out.println("所設(shè)置的點(diǎn)數(shù)之和:" + (dice2.getPoint() + dice3.getPoint()));

// ③連續(xù)10次擲兩個骰子,顯示每次擲骰子的結(jié)果,以及兩個骰子10次點(diǎn)數(shù)的平均值。

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) + "次點(diǎn)數(shù)是:" + first);

int second = dice4.throwDice();

System.out.println("第二個骰子擲:" + (i + 1) + "次點(diǎn)數(shù)是:" + second);

points1.add(first);

points2.add(second);

}

long sum1 = points1.stream().reduce(Integer::sum).orElse(0);

System.out.println("第一個骰子10次點(diǎn)數(shù)的平均值:" + 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次點(diǎn)數(shù)的平均值:" + 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("點(diǎn)數(shù)為:"+num);

}

}

java中編程實(shí)現(xiàn)如下的骰子游戲:丟下兩個骰子,若分值的總值為7點(diǎn),則“贏”;否則“輸”。

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("第一次點(diǎn)數(shù):" + die1.getFaceValue());

die2.roll();

System.out.println("第二次點(diǎn)數(shù):" + 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);

}

}

當(dāng)前名稱:java制作篩子的代碼 如何編篩子
分享URL:http://muchs.cn/article28/hgcjjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站收錄、電子商務(wù)、服務(wù)器托管、全網(wǎng)營銷推廣、軟件開發(fā)做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)