Java编程——赌数游戏
/**
- 赌数游戏
- 提供三个1-6的随机数,作为掷骰子得到的点数。如果各个点数相同,则为豹子。
- 如果三个骰子点数和,小于或等于9,则为“小”。
- 如果三个骰子点数和,大于9,则为“大”。
- 用户随机选择:押大、押小或者押豹子。通过判断,输出客户是否押正确。
- 如果正确三次,结束游戏。(循环)while\for\do-while
- */
```java
package com.demo2;
public class Banker {
public String name;
public int age;
public int seat_number;
public int a;
public Banker(String name,int age,int seat_number){
this.name = name;
this.age = age;
this.seat_number = seat_number;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public int getSeat_number(){
return seat_number;
}
public int getA(){
int a = (int) (Math.random()*6+1); //产生一个[1-6]的数
return a;
}
}
```sql
package com.demo2;
public class Player {
public String name;
public int age;
public int seat_number;
public int d;
public String guess;
public Player(String name,int age,int seat_number){
this.name = name;
this.age = age;
this.seat_number = seat_number;
}
}
package com.demo2;
import javax.swing.*;
public class GuessNumber2 {
public static void main(String[] args) {
int cnt1 = 0;
int cnt2 = 0;
//new 庄家对象
Banker b1 = new Banker("金牌庄家1号", 25, 2);
int a ;
int b ;
int c;
String result;
//2、new 玩家对象
Player s1 = new Player("勇敢的玩家1号",30,2);
while (cnt1<=3) {
//庄家摇骰子结果(取三个随机数)
a = b1.getA();
b = b1.getA();
c = b1.getA();
//判定结果
if (a==b && b==c){
result="豹子";
} else if (a+b+c<=9) {
result = "小";
}else {
result = "大";
}
//玩家输入进行猜宝
String guess= JOptionPane.showInputDialog("请押宝,买定离手:(选择:大、小、豹子)");
if(result.equals(guess)){
cnt1 = cnt1+1;
System.out.println("恭喜你这是猜对的第" + cnt1 + "次了!");
System.out.println("庄家开盘结果:"+result);
System.out.println("玩家押宝结果:"+guess);
System.out.println("庄家出数情况 "+"数字a:"+a+" 数字b:"+b+" 数字c:"+c);
System.out.println("--------------------------------");
continue;
}else {
cnt2 = cnt2+1;
System.out.println("很遗憾,这是你猜错的第"+cnt2+"次了!");
System.out.println("庄家开盘结果:"+result);
System.out.println("玩家押宝结果:"+guess);
System.out.println("庄家出数情况 "+"数字a:"+a+" 数字b:"+b+" 数字c:"+c);
System.out.println("--------------------------------");
}
}
}
}