1. 程式人生 > >【小程式】搖骰子游戲BETA版

【小程式】搖骰子游戲BETA版

import java.util.Scanner;


public class BetGame
{
     public static void main(String[] args)
	{
         int  allmoney = 5000;//開始有5000元
    	 int money = 0;
    	 System.out.println("*********歡迎進入搖骰子游戲******");
    while (true)
    {
		Scanner input = new Scanner(System.in);
		do{
			System.out.println("總金額為"+allmoney);
			System.out.println("請輸入您要押注的金額");
		    Scanner enter = new Scanner(System.in);
		    money = enter.nextInt();
		 if( money>allmoney || money<0)
		  {
		  System.out.println("輸入有誤,請重新輸入");
		  continue;
		  }
		break;
		}while(true);
		System.out.println("是壓大小還是壓數字");
		System.out.println("1.只壓大小");
		System.out.println("2.只壓數字");
		System.out.println("3.壓大小並且壓數字");
		int choose1 = input.nextInt();//選擇押注方式
		int choose2 =0;//選擇大小
		int num1 = 0; //定義3個骰子
		int num2 = 0;
		int num3 = 0;
		num1=(int)(Math.random()*6+1);
		num2=(int)(Math.random()*6+1);
		num3=(int)(Math.random()*6+1);
		int sum = num1+num2+num3;
		System.out.println("開骰:");
	switch(choose1)
	{	 
	  case 3:
	  case 1:	
		    System.out.println("請選擇壓大還是壓小:");
		    System.out.println("1.大");
		    System.out.println("2.小");
		    choose2 = input.nextInt();
			if(sum>9)
			{
				if(choose2==1)
				{
					money*=2;
					allmoney+=money;
					if(choose1==3)
					{
						choose1=2;
						
					}
					else
					{
						System.out.println("BINGO!");
						System.out.println(num1+" "+num2+" "+num3+" "+sum+"點,大");
						break;
					}
				}
				else
				{
					if(choose1==3)
					{
						choose1=2;
						
					}
					else
					{
						System.out.println(num1+" "+num2+" "+num3+" "+sum+"點,大");
						System.out.println("i'm sorry!");
					    allmoney-=money;
						break;
					}

				}
			}
			if(sum<=9)
			{
				if(choose2==2)
				{
					money*=2;
					allmoney+=money;
					if(choose1==3)
					{
						choose1=2;
						
					}
					else
					{
						System.out.println("BINGO!");
						System.out.println(num1+" "+num2+" "+num3+" "+sum+"點,小");
						break;
					}
				}
				else
				{	
					if(choose1==3)
					{
						choose1=2;
					}
					else
					{	
						System.out.println(num1+" "+num2+" "+num3+" "+sum+"點,小");
						System.out.println("i'm sorry!");
			            allmoney-=money;
						break;
					}
				
				}
			}
	  case 2:
		  int choosenum = 0;
		do{
			System.out.println("請選擇數字(3~18)");
			choosenum = input.nextInt();
		  }while(choosenum<3 || choosenum>18);
		   System.out.println(num1+" "+num2+" "+num3+" "+sum+"點");
		  if(sum==choosenum)
			{
				money*=5;
				System.out.println("BINGO!");
				allmoney+=money;
			}
			else
			{
			  System.out.println("i'm sorry!");
			  allmoney-=money;
			}
		  break;
	  }
	    System.out.println("您還剩"+allmoney);
	    System.out.println("(是否繼續y/n)");
	    String iscontinue =input.next();
	    if(iscontinue.equals("n"))
	  {
		System.out.println("歡迎下次光臨");
		System.exit(0);
	  }	
	    if(allmoney==0)
	  {
		System.out.println("你輸光了!!!");
		System.out.println("Game Over!");
		System.exit(0);
	  }
    }
  }
}