码迷,mamicode.com
首页 > 其他好文 > 详细

HW5.32

时间:2016-08-23 11:23:26      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

 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 }

 

HW5.32

标签:

原文地址:http://www.cnblogs.com/wood-python/p/5798397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!