网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

java摇骰子的代码 摇骰子编程

写一个java程序,摇两个骰子,用random,直到两个值相等为止

不知道你说的是random类还是math.random,所以写了两个

和平网站建设公司成都创新互联,和平网站设计制作,有大型网站制作公司丰富经验。已为和平1000+提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的和平做网站的公司定做!

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掷骰子(急)

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怎么写出当骰子点数为6时,在掷一遍的代码

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

public

void

Dice(){

Random

random

=

new

Random();

int

count

=

random.nextInt(6)

+

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

//这里写你的代码逻辑

if(count

==

6){

Dice();//再掷一次

}

//这里写你的代码逻辑

}


分享名称:java摇骰子的代码 摇骰子编程
转载来于:http://bjjierui.cn/article/hhgcsj.html

其他资讯