【Java自學】擲骰子游戲


 1 package codeTask_FangFa;
 2 /*5.29   擲骰子游戲。 規則:
 3   扔兩個骰子,計算和。 2.3或12(稱作擲骰子)你就輸了,7或11(稱作自然),你就贏了。如果是其他數字。
 4  繼續擲,直到出來一個7(你輸了)或者出來一個和剛才一樣的(你贏了)。
 5  */
 6 
 7 import java.util.Random;
 8 public class ZhiTouZi {
 9    public static void main(String[] args){
10       
11        System.out.println("程序將會扮演一個玩家,和你玩。現在,開始吧。");
12        first();
13    }
14    
15    public static void first(){
16        Random random = new Random();
17        int a = random.nextInt(6)+1;
18        int b = random.nextInt(6)+1;
19        if(a+b==7||a+b==11)
20            System.out.println("骰子的結果是:" + a +" and " +b+ " = " +(a+b)+"  所以,你贏了。");
21        else if(a+b==2||a+b==3)
22            System.out.println("骰子的結果是:"+(a+b)+"所以,你輸了。");
23        else if(a+b==12)
24            System.out.println("骰子的結果是: " +(a+b) +" 所以,你輸了。");
25        else { 
26           System.out.println("暫時僵持狀態,我們將進行下一次擲骰子。和是:" + (a+b));
27           int sum = a+b;
28           int sum2 = -1;
29           while(sum2!=7&sum!=sum2){
30           Random  random1 = new Random();
31              int a1 =  random1.nextInt(6)+1;
32           int b1 =  random1.nextInt(6)+1;
33              sum2 = a1+b1;
34              if(sum2==7)
35                 System.out.println("好吧,你還是輸了,剛篩出來的和是7。");
36            else if (sum2 ==sum)
37                   System.out.printf("\n你的運氣還不賴嘛,結果和剛才一樣是:%4d 你贏了",sum2);        
38        }
39    }
40   }
41 }
42       

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM