功能介紹:java
可隨機生成三個1-6的骰子,玩家可下注猜大小(下注金額不能超過本身的餘額),初始餘額爲1000,若猜中則賺得下注金額,若錢輸完了以後,玩家能夠選擇繼續充錢或者退出遊戲dom
代碼以下:spa
package com.bd22; import java.util.Scanner; public class Game { public static void main(String[] args) { Scanner input = new Scanner(System.in); int money = 1000; //遊戲界面 System.out.println("******歡迎來到皇宮大賭場******"); while(true) { System.out.println("是否開始遊戲:(y/n)"); String key = input.next(); //遊戲開始 if("y".equals(key)) { System.out.println("請下注:"); int pay = input.nextInt(); //下注的錢不能大於餘額 if(pay>money) { System.out.println("餘額不足"); continue; } //隨機生成3個數 int a = (int)(Math.random()*6)+1; int b = (int)(Math.random()*6)+1; int c = (int)(Math.random()*6)+1; String result = (a+b+c)>9?"大":"小"; System.out.println("請壓大小:"); String guess = input.next(); //猜大小以後,遊戲結果 if(result.equals(guess)) { money += pay; System.out.println(a+","+b+","+c); System.out.println("恭喜您壓中了"); System.out.println("帳戶餘額爲:"+money); }else { money -= pay; System.out.println(a+","+b+","+c); System.out.println("很遺憾您未壓中"); System.out.println("帳戶餘額爲:"+money); } }else { System.out.println("退出遊戲"); break; } //錢輸完了以後選擇充值或者離開 if(money<=0) { System.out.println("您沒錢了,是否繼續充值(是或否)"); String whether = input.next(); if("是".equals(whether)) { System.out.println("充值的金額爲:"); money = input.nextInt(); System.out.println("您的餘額爲:" + money); continue; }else { System.out.println("您能夠離開了"); break; } } } } }
運行結果:code
若壓中:blog
若未壓中:遊戲
若輸入金額大於本錢:input
若錢輸光可進行充值:class