java掷筛子代码 用java怎样做一个骰子游戏

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中编程实现如下的骰子游戏:丢下两个骰子,若分值的总值为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);
}
【java掷筛子代码 用java怎样做一个骰子游戏】}
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编程 请帮忙这游戏设计的,电脑哪有胜算啊,点数到了20就全都放弃了,永远也到不了100啊 。以下是代码:
package com.sino.baiduzhidao.throwDie;
import java.util.Random;
/**
* 骰子
* @author Yanghl
*
*/
public class Die implements Runnable {
private Random r;
private int point;
public Die() {
r = new Random();
}
/**
* 获得骨子点数
* @return 骰子点数
*/
public int getPoint() {
return point;
}
@Override
public void run() {
//设置骰子点数为1-6的随机数
point = r.nextInt(6)1;
System.out.print(point" ");
}
}
package com.sino.baiduzhidao.throwDie;
public class PairOfDice {
/**
* 骰子1
*/
private Die die1;
/**
* 骰子2
*/
private Die die2;
public PairOfDice() {
die1 = new Die();
die2 = new Die();
}
/**
* 掷出两个骰子,并打印结果
*/
public void throwDie() {
System.out.print("骰子点数:");
//启用两个线程,分别转动两个骰子,模拟两个骰子同时被掷出
Thread t1 = new Thread(die1);
Thread t2 = new Thread(die2);
t1.start();
t2.start();
try {
//等待骰子结束
t1.join();
t2.join();
System.out.println();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* 两个骰子点数都为6
* @return
*/
public boolean isDouble6() {
if(die1.getPoint() == 6die2.getPoint() == 6) {
return true;
}
return false;
}
/**
* 只有一个骰子点数为1
* @return
*/
public boolean isSingle1() {
if(die1.getPoint() == 1 || die2.getPoint() == 1) {
if(isDouble1()) {
return false;
}
return true;
}
return false;
}
/**
* 两个骰子点数都为1
* @return
*/
public boolean isDouble1() {
if(die1.getPoint() == 1die2.getPoint() == 1) {
return true;
}
return false;
}
/**
* 获取两个骰子点数总和
* @return
*/
public int getPointNum() {
return die1.getPoint()die2.getPoint();
}
public Die getDie1() {
return die1;
}
public Die getDie2() {
return die2;
}
}
package com.sino.baiduzhidao.throwDie;
public class BoxCars {
public static void main(String[] args) {
PairOfDice pod = new PairOfDice();
int times = 0;
for(int i=0;i100;i) {
pod.throwDie();
if(pod.isDouble6()) {
times;
}
}
System.out.println("掷骰子100次,都是6的次数:"times);
}
}
以下是人机大战的代码:
package com.sino.baiduzhidao.throwDie;
/**
* 玩家类
*/
public class Player {
/**
* 玩家名称
*/
private String name;
/**
* 掷骰子总数
*/
private int throwTimes;
/**
* 总点数
*/
private int pointNum;
public Player(String name) {
this.name = name;
this.pointNum = 0;
this.throwTimes = 0;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getThrowTimes() {
return throwTimes;
}
public void setThrowTimes(int throwTimes) {
this.throwTimes = throwTimes;
}
public int getPointNum() {
return pointNum;
}
public void setPointNum(int pointNum) {
this.pointNum = pointNum;
}
}
package com.sino.baiduzhidao.throwDie;
import java.util.Scanner;
public class Pig {
/**
* 电脑默认最大点数,超过后均放弃掷骰子
*/
private static int COMPUTER_DEFAULT_MAX_POINT = 20;
/**
* 目标分数
*/
private static int TARGET_POINT = 100;
/**
* 一对骰子
*/
private PairOfDice pod;
/**
* 玩家
*/
private Player player;
/**
* 电脑
*/
private Player computer;
/**
* 构造函数,初始化骰子
*/
public Pig() {
pod = new PairOfDice();
player = new Player("player");
computer = new Player("computer");
}
/**
* 开始游戏
*/
public void startPlay() {
while(true) {
//先玩家,后电脑
System.out.println("-----------------玩家---------------");
if(isThrowDie()) {
if(playerThrow(player)) {
break;
}
} else {
System.out.println("放弃掷骰子");
}
System.out.println();
System.out.println("-----------------电脑---------------");
if(computer.getPointNum()COMPUTER_DEFAULT_MAX_POINT) {
if(playerThrow(computer)) {
break;
}
} else {
System.out.println("放弃掷骰子");
}
System.out.println();
}
}
/**
* 玩家掷骰子,并返回玩家总分是否达到TARGET_POINT
* @return
*/
private boolean playerThrow(Player p) {
pod.throwDie();
//掷骰子次数1
p.setThrowTimes(p.getThrowTimes()1);
if(pod.isDouble1()) {
//掷出两个1 , 分数清0
p.setPointNum(0);
System.out.println("两个点数均为1,总分清0");
} else if(pod.isSingle1()) {
//掷出单个1,不加分
System.out.println("有一个点数为1,不能计入总分");
} else {
p.setPointNum(p.getPointNum()pod.getPointNum());
}
System.out.println("当前总分:"p.getPointNum());
//判断玩家总分是否达到TARGET_POINT
if(p.getPointNum() = TARGET_POINT) {
System.out.println("Game Over! "p.getName()" win!");
return true;
} else {
return false;
}
}
/**
* 选择是否掷骰子
* @return
*/
private boolean isThrowDie() {
Scanner sc = new Scanner(System.in);
System.out.print("选择是否掷骰子(Y:掷骰子; N:放弃):");
while(true) {
String in = sc.nextLine();
if("y".equalsIgnoreCase(in)) {
return true;
} else if("n".equalsIgnoreCase(in)) {
return false;
} else {
System.out.print("请选择Y/N:");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
new Pig().startPlay();
}
}
java掷筛子代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于用java怎样做一个骰子游戏、java掷筛子代码的信息别忘了在本站进行查找喔 。

    推荐阅读