package com;
import java.util.Random;
public class Test {
/*
* 玩家掷两个骰子,点数1到6,
* 如果第一次点数和为7或11则胜;
* 若点数和为2,3或12则输;
* 如果为其他点数则记录第一次点数和;
* 如果和,然后继续掷骰,直至点数和等于第一次掷出的和,
* 则玩家胜;
* 如果在这之前掷出点和为7则玩家输;
* 怎么用JAVA实现?
*/
public static void main(String[] args) {
int flag = 0;//玩家胜负标志 0:和 1:玩家胜出 -1:玩家输
int shuaizi1= 1,shuaizi2 =1; //筛子点数
boolean first = true;
int privous = 0;
while(flag==0){
shuaizi1 = (int) (1 + Math.random() * 6);
shuaizi2 = (int) (1 + Math.random() * 6);
if(first == true){
if(shuaizi2+shuaizi1==7||shuaizi2+shuaizi1==11){
flag = 1;
break;
}else if(shuaizi2+shuaizi1==2||shuaizi2+shuaizi1==3||shuaizi2+shuaizi1==12){
flag = -1;
}else{
privous = (shuaizi2+shuaizi1);
flag = 0;
}
first = false;
}else{
if(shuaizi2+shuaizi1 == privous){
flag =1;
break;
}else if(shuaizi2+shuaizi1 == 7){
flag = -1;
break;
}
}
}
if(flag ==1){
System.out.println("玩家胜出");
}else if(flag == -1){
System.out.println("玩家输了");
}
}
}
|