[20-04-26][Self-study Notes 12]Java Powerball

Posted mirai3usi9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[20-04-26][Self-study Notes 12]Java Powerball相关的知识,希望对你有一定的参考价值。

 1 package powerball;
 2 
 3 public class Powerball {
 4 
 5     /** 五个白球 */
 6     public int whiteBall1;
 7     public int whiteBall2;
 8     public int whiteBall3;
 9     public int whiteBall4;
10     public int whiteBall5;
11 
12     /** 一个红球 */
13     public int redBall;
14 
15     /** 运行 */
16     public void play() {
17 
18         creatWhiteBall();
19         this.redBall = randomNum(1, 35);
20         
21     }
22     
23     /** 产生白球 */
24     public void creatWhiteBall() {
25         
26         int[] tempArray = new int[5];
27         for (int i = 0; i < tempArray.length; i++) {
28             tempArray[i] = randomNum(1, 59);
29             if (checkRep(i, tempArray[i], tempArray)) {
30                 i--;
31             }
32         }
33         
34         this.whiteBall1 = tempArray[0];
35         this.whiteBall2 = tempArray[1];
36         this.whiteBall3 = tempArray[2];
37         this.whiteBall4 = tempArray[3];
38         this.whiteBall5 = tempArray[4];
39         
40     }
41 
42     /** 检查重复 */
43     private boolean checkRep(int index, int num, int[] array) {
44         
45         for (int i = 0; i < index; i++) {
46             if (array[i] == num) {
47                 return true;
48             }
49         }
50         
51         return false;
52         
53     }
54 
55     /** 产生随机数 */
56     private int randomNum(int min, int max) {
57 
58         int num = (int)(Math.random() * (max - min + 1) + min);
59 
60         return num;
61     }
62 
63     /** 打印结果 */
64     public void displayResults() {
65 
66         System.out.println("白球为:" + this.whiteBall1 + "," + 
67                                        this.whiteBall2 + "," +
68                                        this.whiteBall3 + "," +
69                                        this.whiteBall4 + "," +
70                                        this.whiteBall5 + ";" +
71                            "红球为:" + this.redBall);
72     }
73     
74 }

 

 1 package powerball;
 2 
 3 public class PlayLottery {
 4 
 5     public static void main(String[] args) {
 6         
 7         /** 创建强力球生成器 */
 8         Powerball pb = new Powerball();
 9         
10         pb.play();
11         pb.displayResults();
12         
13         System.out.println("-------------");
14         
15         Powerball pb2 = new Powerball();
16 
17         pb2.play();
18         pb2.displayResults();
19         
20     }
21 
22 }

结果如下:

白球为:40,19,33,45,21;红球为:1
-------------
白球为:27,11,19,50,46;红球为:14

以上是关于[20-04-26][Self-study Notes 12]Java Powerball的主要内容,如果未能解决你的问题,请参考以下文章

[20-04-26][Self-test 6]Java CharType

[20-04-26][Self-test 2]Java Prime Number

Clion编译 报错 “wchar .h not found” “string .h not found”

遇到[0]:使用Not实现Not16时,可能无法使用内部节点的子总线

使用 ":not" 和 ".not()" 选择器之间的性能差异?

vs安装 products not found