/* 博弈游戏 */ import java.util.*; class AgainstGame{ static int totalMoney;//全局 static int betNum;//全局 public static void main(String [] args){ Scanner input = new Scanner(System.in); totalMoney = 5000;//全局变量方法内初始化 System.out.println("初始余额"+totalMoney); while(true){ System.out.println("当前余额"+totalMoney); System.out.println("欢迎进入博弈游戏 1 进入游戏 2 退出游戏"); int choose = input.nextInt(); if(choose == 1){ System.out.println("进入游戏"); System.out.println("客官您好 请押大小 1 大 2 小"); int choose1 = input.nextInt(); int record = choiceRecord(choose1);//记录庄家押数 if(record == -1){ System.out.println("输入有误 请重新输入"); continue; } //System.out.println("庄家选择的记录1大2小:"+record); moneyRecord();//庄家押钱 rechargeChoice(); //充值前的选择 int num = RandomNum();//电脑产生随机数 System.out.println("Computer摇出来的骰子数为:"+num); isRight(num,record);//判读庄家输赢 System.out.println("****************************************"); }else if(choose == 2){ System.out.println("退出游戏"); break; }else{ System.out.println("输入有误 请重新输入"); } } } //记录庄家押的数 public static int choiceRecord(int choose){ int record = -1; if(choose == 1){ System.out.println("你押的-->大"); record = 1; }else if(choose == 2){ System.out.println("你押的-->小"); record = 2; } return record; } //记录庄家押的钱 public static void moneyRecord(){ System.out.println("未来尽在手中,请下注,买定离手(50的倍数并且不能超过1000)"); Scanner input = new Scanner(System.in); while(true){ betNum = input.nextInt(); if(betNum%50!=0 || betNum>1000){ System.out.println("你下注的金额不符合规定,重新输入"); continue; }else{ break; } } } //系统产生1~18的随机数 //static double random() 返回带正号的double值,该值大于等于0.0且小于1.0。 public static int RandomNum(){ int num = (int)(Math.random()*18)+1; return num; } //充值前的选择 public static void rechargeChoice(){ Scanner input = new Scanner(System.in); while(true){ if(betNum > totalMoney){ System.out.println("客官,您的荷包瘪了,充点呗?1 前往饿狼充值 2 退出游戏"); int choose2 = input.nextInt(); if(choose2 == 1){ System.out.println("客官,您太敞亮了,请输入您的充值金额"); int money = 0; money = input.nextInt(); recharge(money); }else if(choose2 == 2){ System.out.println("退出游戏"); break; }else{ System.out.println("你的输入有误 请重新输入"); continue; } }else{ break; } } } //充值 public static void recharge(int money){ System.out.print("您充了 "+money); totalMoney += money; System.out.println(" 余额为 "+totalMoney); } //判断大小 public static void isRight(int RandomNum, int record){ int flag = 0; if(RandomNum>9 && RandomNum<=18){ flag = 1;//大 }else{ flag = 2;//小 } if(flag == record){ System.out.println("庄家是大"); System.out.println("真棒,您赢啦 余额为"+(totalMoney+betNum)); totalMoney = totalMoney+betNum; }else{ System.out.println("庄家是小"); System.out.println("抱歉,您输啦 余额为"+(totalMoney-betNum)); totalMoney = totalMoney-betNum; } } }