1 package codeTask_FangFa; 2 /*5.29 擲骰子游戲。 規(guī)則: 3 扔兩個骰子,計算和。 2.3或12(稱作擲骰子)你就輸了,7或11(稱作自然),你就贏了。如果是其他數(shù)字。 4 繼續(xù)擲,直到出來一個7(你輸了)或者出來一個和剛才一樣的(你贏了)。 5 */ 6 7 import java.util.Random; 8 public class ZhiTouZi { 9 public static void main(String[] args){10 11 System.out.);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("骰子的結(jié)果是:" + a +" and " +b+ " = " +(a+b)+" 所以,你贏了。");21 else if(a+b==2||a+b==3)22 System.out.println("骰子的結(jié)果是:"+(a+b)+"所以,你輸了。");23 else if(a+b==12)24 System.out.println("骰子的結(jié)果是: " +(a+b) +" 所以,你輸了。");25 else { 26 System.out.println("暫時僵持狀態(tài),我們將進行下一次擲骰子。和是:" + (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你的運氣還不賴嘛,結(jié)果和剛才一樣是:%4d 你贏了",sum2); 38 }39 }40 }41 }42
新聞熱點
疑難解答