當前位置:首頁 » 編程語言 » java骰子

java骰子

發布時間: 2022-08-09 04:37:49

java擲骰子游戲

估計是沒有人幫你做這個游戲,這么簡單,為何不自己想想怎麼做呢?作為一個程序員:以跪求其解為恥!

❷ Java程序求教,擲骰子

importjava.util.Random;

publicclassThrowDice{
publicstaticint[]result={1,2,3,4,5,6};

publicstaticvoiddoThrow(){
Randomrand=newRandom();
inti=rand.nextInt(6);
System.out.println("骰子的點數為-->"+result[i]);
}

publicstaticvoidmain(String[]args){
//執行10次
for(inti=0;i<10;i++){
doThrow();
}
}
}

❸ 編寫一個簡單的 Java程序,模擬兩個玩家擲骰子

Java程序,模擬兩個玩家擲骰子,玩家按照下列

❹ 如何用java實現擲骰子

package me.flyshow.test;

public class Test {
public static void main(String[] argv) {
int[] result = new int[11];
for (int i = 0; i < 5000; i++) {
int a = (int) (Math.random() * 6 + 1);
int b = (int) (Math.random() * 6 + 1);
int sum = a + b;
switch (sum) {
case 2:
result[0]++;
break;
case 3:
result[1]++;
break;
case 4:
result[2]++;
break;
case 5:
result[3]++;
break;
case 6:
result[4]++;
break;
case 7:
result[5]++;
break;
case 8:
result[6]++;
break;
case 9:
result[7]++;
break;
case 10:
result[8]++;
break;
case 11:
result[9]++;
break;
case 12:
result[10]++;
break;
default:
break;
}
}
for (int n = 0; n < 11; n++) {
System.out.println((n + 2) + "出現了" + result[n] + "次");

❺ 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 骰子游戲

importjava.util.Scanner;
publicclassDice_骰子{
staticScannersc=newScanner(System.in);
publicstaticvoidmain(String[]args){
inttem=0;
DiceTestd1=newDiceTest();
while(true){
if(d1.display()==7){
tem+=5;
}
System.out.println("當前有money:"+tem+"分 是否繼續?輸入Y或者N");
if(sc.nextLine().compareToIgnoreCase("Y")!=0)return;
}
}
}//骰子類!
classDiceRes{
privateintnumber=0;
publicintgetTer(){
returnnumber;
}
publicvoidsetTer(intnumber){
this.number=number;
}
publicintroll(){
returnnumber=(int)(Math.random()*6+1);
}
}//操作類!
classDiceTest{
privatefinalArrayList<DiceRes>ad=newArrayList<>();
DiceTest(){
this(2);
}
DiceTest(intn){
for(inti=0;i<n;i++){
ad.add(newDiceRes());
}
}
publicintdisplay(){
inttem=0;
for(ListIterator<DiceRes>it=ad.listIterator();it.hasNext();){
intn=it.next().roll();
System.out.println(n+":點");
tem+=n;
}
System.out.println("共:"+tem+"點");
returntem;
}
}

❼ java 骰子游戲

import java.util.*;
import java.io.*;
public class Game{
public static void main(String args[]) throws Exception{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String player1="",player2="";
int score1=0,score2=0,goal=0;
char resp=' ';
System.out.print("Type the name of the first player:");
player1=reader.readLine();
System.out.print("Type the name of the second player:");
player2=reader.readLine();
System.out.print("How much total point is the goal for the game:");
goal=Integer.parseInt(reader.readLine());
int round1=0,round2=0;
while((score1<goal)&&(score2<goal)){
System.out.println("Now it's "+player1+"'s turn,"+player2+" scored

"+String.valueOf(round2)+" and has a total of "+String.valueOf(score2));
round1=0;
System.out.println(player1+",total point:"+String.valueOf(score1)+",Round

point:"+String.valueOf(round1)+"(R)oll a dice or (E)nd turn:");
while((resp=reader.readLine().charAt(0))!='e'){
int i=0;
while((i=new Random().nextInt(6))<0){}
System.out.println("The dice rolled a "+String.valueOf(i));
if(i==6){
round1=0;
System.out.println(player1+" lost it all,and has 0 points this turn.");
break;
}
round1+=i;
if(score1+round1>=goal) break;
System.out.println(player1+",total point:"+String.valueOf(score1)+",Round

point:"+String.valueOf(round1)+" (R)oll a dice or (E)nd turn:");
}
score1+=round1;
if(score1>=goal) break;
System.out.println("Now it's "+player2+"'s turn,"+player1+" scored

"+String.valueOf(round1)+" and has a total of "+String.valueOf(score1));
round2=0;
System.out.println(player2+",total point:"+String.valueOf(score2)+",Round

point:"+String.valueOf(round2)+" (R)oll a dice or (E)nd turn:");
while((resp=reader.readLine().charAt(0))!='E'){
int i=0;
while((i=new Random().nextInt(6))<0){}
System.out.println("The dice rolled a "+String.valueOf(i));
if(i==6){
round2=0;
System.out.println(player2+" lost it all,and has 0 points this turn.");
break;
}
round2+=i;
if(score2+round2>=goal) break;
System.out.println(player2+",total point:"+String.valueOf(score2)+",Round

point:"+String.valueOf(round2)+"(R)oll a dice or (E)nd turn:");
}
score2+=round2;
if(score2>=goal) break;
}
if(score1>=goal) System.out.println(player1+" wins!");
if(score2>=goal) System.out.println(player2+" wins!");
}
}

❽ 用Java寫一個擲骰子的code

請採納

❾ 求教java骰子編程問題

importjava.util.Random;
importjava.util.Scanner;


publicclassGame{

Playerhuman;
Playercomputer;
Rulerule;
intturnNo;
/**
*開始游戲
*/
publicvoidstart(){
/*初始化*/
human=newHumanPlayer();
computer=newComputerPlayer();
rule=newRule();
turnNo=1;
/*開始*/
while(true){
System.out.println("turn"+turnNo+++":");
human.turn(rule);
if(human.isWin()){
System.out.println("humanwin!");
break;
}
System.out.println("");
computer.turn(rule);
if(computer.isWin()){
System.out.println("computerwin!");
break;
}
System.out.println("*******************************************");
System.out.println("human:"+human.score);
System.out.println("computer:"+computer.score);
System.out.println(" ");
}

}
/**
*游戲規則
*@authorAdministrator
*
*/
classRule{
booleanisMustRollAgain;//是否必須再擲骰子
booleanisMustEndTurn;//是否必須結束當前回合

publicvoidinit(){
isMustRollAgain=false;
isMustEndTurn=false;
}
publicintcountScore(intdice1,intdice2){
init();
if(dice1==1||dice2==1){//有一個骰子是1回合得0分且必須結束回合
isMustEndTurn=true;
return0;
}
if(dice1%2==1&&dice2%2==1){//兩個都是單數積25分且必須再擲骰子
isMustRollAgain=true;
return25;
}
if(dice1%2==0&&dice2%2==0){//兩個都是雙數積兩倍骰子的數值且必須再擲骰子
isMustRollAgain=true;
return2*(dice1+dice2);
}
returndice1+dice2;
}
}
/**
*游戲選手——公共方法
*@authorAdministrator
*
*/
abstractclassPlayer{
protectedintscore=0;
protectedintdice1,dice2;
Stringname="player";
publicPlayer(){
this.score=0;
}
publicvoidturn(Rulerule){
System.out.println(name+"'sturn:");
intturnScore=roll(rule,0,score);
System.out.println("return"+turnScore);
score+=turnScore;
System.out.println(name+"get"+turnScore+".aggregatescore:"+score);
}

publicbooleanisWin(){
returnscore>=100;
}
publicintgetRollScore(Rulerule){
Randomrandom=newRandom();
dice1=random.nextInt(6)+1;
dice2=random.nextInt(6)+1;
System.out.println("dice1:"+dice1+"dice2:"+dice2);
introllScore=rule.countScore(dice1,dice2);
System.out.println("rollscore:"+rollScore+".");
returnrollScore;
}
publicintroll(Rulerule,intturnScore,intscore){
introllScore=getRollScore(rule);
if(rule.isMustEndTurn){
turnScore=0;
System.out.println(".");
returnturnScore;
}
else{
turnScore+=rollScore;
if(turnScore+score>=100)
returnturnScore;
System.out.println("get"+turnScore+"pointforthisturnnow");
if(rule.isMustRollAgain){
System.out.println("mustrollagain!");
turnScore=roll(rule,turnScore,score);
}
else{
charch=thinkingStrategy(rule,turnScore,score);
if(ch=='y'){
turnScore=roll(rule,turnScore,score);
}
else{
returnturnScore;
}
}
}
returnturnScore;
}
(Rulerule,intturnScore,intscore);
}
/**
*人類玩家
*@authorAdministrator
*
*/
classHumanPlayerextendsPlayer{
Stringname="human";
publicHumanPlayer(){
super();
super.name=this.name;
}
@Override
publiccharthinkingStrategy(Rulerule,intturnScore,intscore){
System.out.println("rollagin?(y/n)");
charch=newScanner(System.in).next().charAt(0);
returnch;
}
}
/**
*電腦玩家
*@authorAdministrator
*
*/
{
Stringname="compter";
publicComputerPlayer(){
super();
super.name=this.name;
}
@Override
publiccharthinkingStrategy(Rulerule,intturnScore,intscore){
// if(newRandom().nextInt(100)<100-turnScore-score){
if(turnScore<40){
System.out.println("computerchoosetorollagain!");
return'y';
}
else{//回合得分超過40選擇結束回合
System.out.println("computerchoosetohold(endthisturn)!");
return'n';
}
}
}

publicstaticvoidmain(String[]args){
newGame().start();
}
}

不知道英語理解的是否正確。但是大致上就是這樣的。

❿ 擲骰子模擬程序 Java

publicclassTestDieRoll{
privateclassDieRoll{
privatefinalintDIES=6;
privateintroll(){
return(int)(Math.floor(Math.random()*DIES+1));
}
}
publicstaticvoidmain(String[]args){
DieRolld=newTestDieRoll().newDieRoll();
int[]arr=newint[6];
for(inti=0;i<1000;i++)
arr[d.roll()-1]++;
for(inti=0;i<arr.length;i++){
System.out.println("擲得"+(i+1)+"點的次數為"+(arr[i])+"次");
}
}
}

熱點內容
做解壓橡皮 發布:2025-01-21 15:03:06 瀏覽:990
雙系統win訪問mac 發布:2025-01-21 14:53:52 瀏覽:484
安卓車機系統如何安裝carplay 發布:2025-01-21 14:52:24 瀏覽:589
sql操作手冊 發布:2025-01-21 14:46:08 瀏覽:311
青橙腳本 發布:2025-01-21 14:44:05 瀏覽:218
東風本田crv時尚版是什麼配置 發布:2025-01-21 14:20:04 瀏覽:219
安卓如何多開軟體每個機型不一樣 發布:2025-01-21 14:15:29 瀏覽:501
iis配置php5 發布:2025-01-21 14:08:19 瀏覽:274
凱叔講故事為什麼聯系不到伺服器 發布:2025-01-21 13:56:50 瀏覽:387
linux鏡像文件下載 發布:2025-01-21 13:34:36 瀏覽:218