标签:
1 public class Solution 2 { 3 public static void main(String[] args) 4 { 5 int n1 = (int)(Math.random() * 5 + 1); 6 int n2 = (int)(Math.random() * 5 + 1); 7 int winCount = 0; 8 9 for(int i = 0; i < 10000; i++) 10 { 11 if(diceGame(n1, n2)) 12 winCount++; 13 n1 = (int)(Math.random() * 5 + 1); 14 n2 = (int)(Math.random() * 5 + 1); 15 } 16 System.out.println(winCount); 17 } 18 19 public static boolean diceGame(int n1, int n2) 20 { 21 if(n1 + n2 == 2 || n1 + n2 == 3 || n1 + n2 == 12) 22 return true; 23 else if(n1 + n2 == 7 || n1 + n2 == 11) 24 return false; 25 else 26 { 27 int sum = n1 + n2; 28 int num1, num2; 29 while(true) 30 { 31 num1 = (int)(Math.random() * 5 + 1); 32 num2 = (int)(Math.random() * 5 + 1); 33 34 if(num1 + num2 == 7) 35 return false; 36 else if(num1 + num2 == sum) 37 return true; 38 sum = num1 + num2; 39 } 40 } 41 } 42 }
标签:
原文地址:http://www.cnblogs.com/wood-python/p/5798397.html