-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
196 lines (173 loc) · 45.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!doctype html>
<html lang="zh"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta><title>纪东东-博客</title><link rel="manifest" href="/manifest.json"><meta name="application-name" content="纪东东"><meta name="msapplication-TileImage" content="/img/favion.jpeg"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-title" content="纪东东"><meta name="apple-mobile-web-app-status-bar-style" content="default"><meta property="og:type" content="blog"><meta property="og:title" content="纪东东-博客"><meta property="og:url" content="http://example.com/"><meta property="og:site_name" content="纪东东-博客"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="http://example.com/img/og_image.png"><meta property="article:author" content="纪东东"><meta property="twitter:card" content="summary"><meta property="twitter:image:src" content="http://example.com/img/og_image.png"><script type="application/ld+json">{"@context":"https://schema.org","@type":"BlogPosting","mainEntityOfPage":{"@type":"WebPage","@id":"http://example.com"},"headline":"纪东东-博客","image":["http://example.com/img/og_image.png"],"author":{"@type":"Person","name":"纪东东"},"publisher":{"@type":"Organization","name":"纪东东-博客","logo":{"@type":"ImageObject","url":"http://example.com/img/logo.jpeg"}},"description":""}</script><link rel="icon" href="/img/favion.jpeg"><link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.0.0/css/all.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/styles/atom-one-light.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;600&family=Source+Code+Pro"><link rel="stylesheet" href="/css/default.css"><style>body>.footer,body>.navbar,body>.section{opacity:0}</style><!--!--><script>var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "//hm.baidu.com/hm.js?23d4db333099ff1dc8cc9ec5b1ef94cb";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();</script><!--!--><!--!--><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/build/cookieconsent.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/lightgallery.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/justifiedGallery.min.css"><!--!--><!--!--><style>.pace{-webkit-pointer-events:none;pointer-events:none;-webkit-user-select:none;-moz-user-select:none;user-select:none}.pace-inactive{display:none}.pace .pace-progress{background:#3273dc;position:fixed;z-index:2000;top:0;right:100%;width:100%;height:2px}</style><script src="https://cdn.jsdelivr.net/npm/[email protected]/pace.min.js"></script><!--!--><!--!--><!-- hexo injector head_end start --><script>
(function () {
function switchTab() {
if (!location.hash) {
return;
}
const id = '#' + CSS.escape(location.hash.substring(1));
const $tabMenu = document.querySelector(`.tabs a[href="${id}"]`);
if (!$tabMenu) {
return;
}
const $tabMenuContainer = $tabMenu.parentElement.parentElement;
Array.from($tabMenuContainer.children).forEach($menu => $menu.classList.remove('is-active'));
Array.from($tabMenuContainer.querySelectorAll('a'))
.map($menu => document.getElementById($menu.getAttribute("href").substring(1)))
.forEach($content => $content.classList.add('is-hidden'));
if ($tabMenu) {
$tabMenu.parentElement.classList.add('is-active');
}
const $activeTab = document.querySelector(id);
if ($activeTab) {
$activeTab.classList.remove('is-hidden');
}
}
switchTab();
window.addEventListener('hashchange', switchTab, false);
})();
</script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 6.3.0"><style>mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container[display="true"] {
overflow: auto hidden;
}
mjx-container[display="true"] + br {
display: none;
}
</style></head><body class="is-2-column"><nav class="navbar navbar-main"><div class="container navbar-container"><div class="navbar-brand justify-content-center"><a class="navbar-item navbar-logo" href="/"><img src="/img/logo.jpeg" alt="纪东东-博客" height="28"></a></div><div class="navbar-menu"><div class="navbar-start"><a class="navbar-item is-active" href="/">主页</a><a class="navbar-item" href="/archives">归档</a><a class="navbar-item" href="/categories">分类</a><a class="navbar-item" href="/tags">标签</a><a class="navbar-item" href="/about">About</a></div><div class="navbar-end"><a class="navbar-item search" title="搜索" href="javascript:;"><i class="fas fa-search"></i></a></div></div></div></nav><section class="section"><div class="container"><div class="columns"><div class="column order-2 column-main is-8-tablet is-8-desktop is-8-widescreen"><div class="card"><article class="card-content article" role="article"><div class="article-meta is-size-7 is-uppercase level is-mobile"><div class="level-left"><span class="level-item"><time dateTime="2023-11-17T06:56:06.000Z" title="2023/11/17 下午2:56:06">2023-11-17</time>发表</span><span class="level-item"><time dateTime="2023-11-17T07:03:47.746Z" title="2023/11/17 下午3:03:47">2023-11-17</time>更新</span><span class="level-item"><a class="link-muted" href="/categories/Java/">Java</a></span><span class="level-item">6 分钟读完 (大约923个字)</span></div></div><p class="title is-3 is-size-4-mobile"><a class="link-muted" href="/2023/11/17/Java%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E5%8F%8C%E8%89%B2%E7%90%83%E6%B8%B8%E6%88%8F%EF%BC%8C%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%B7%B1%E4%B9%B0%E4%B8%AA%E5%BD%A9%E7%A5%A8%E7%8E%A9/">Java手动实现一个双色球游戏,每天自己买个彩票玩</a></p><div class="content"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>之前购买彩票时很少中奖,实际上彩票的中奖概率非常低。在多次尝试后,只中过一次。考虑到中奖机会有限,我决定尝试实现一个彩票自动购买程序,类似于机选功能。这样,我可以更加灵活地进行尝试,而不受实际彩票中奖概率的限制。</p>
<h1 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h1><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> demo.game;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> cn.hutool.core.util.RandomUtil;</span><br><span class="line"><span class="keyword">import</span> com.alibaba.fastjson.JSON;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> jisl on 2023/7/31 15:52</span></span><br><span class="line"><span class="comment"> * 双色球彩票</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DoubleColorBallLottery</span> {</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 彩票期数</span></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="variable">lotteryNumber</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 开奖号码</span></span><br><span class="line"> <span class="keyword">private</span> Set<Integer> winRedNum = <span class="keyword">new</span> <span class="title class_">HashSet</span><>();</span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> winBlueNum;</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 用户购买的彩票</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> List<List<Integer>> userLottery = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>();</span><br><span class="line"></span><br><span class="line"> <span class="comment">// 用户中奖的彩票</span></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">final</span> Map<Integer, List<String>> userPrizeLottery = <span class="keyword">new</span> <span class="title class_">HashMap</span><>();</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span>[] lotteryAmount = <span class="keyword">new</span> <span class="title class_">int</span>[]{<span class="number">0</span>, <span class="number">5000000</span>, <span class="number">1250000</span>, <span class="number">3000</span>, <span class="number">200</span>, <span class="number">10</span>, <span class="number">5</span>};</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">MAX_PRIZE_LEVEL</span> <span class="operator">=</span> <span class="number">6</span>;</span><br><span class="line"></span><br><span class="line"> <span class="type">Random</span> <span class="variable">random</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 彩票开奖</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> jisl on 2023/7/31 16:06</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">lotteryDrawing</span><span class="params">()</span> {</span><br><span class="line"> winRedNum = buildRedNum();</span><br><span class="line"> winBlueNum = buildBlueNum();</span><br><span class="line"> System.out.printf(<span class="string">"[第%s期双色球]开奖结果:红球:%s,蓝球:%s%n"</span>, lotteryNumber, JSON.toJSONString(winRedNum), winBlueNum);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="title function_">buildBlueNum</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">return</span> random.nextInt(<span class="number">16</span>) + <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> HashSet<Integer> <span class="title function_">buildRedNum</span><span class="params">()</span> {</span><br><span class="line"> <span class="keyword">final</span> HashSet<Integer> set = <span class="keyword">new</span> <span class="title class_">HashSet</span><>();</span><br><span class="line"> <span class="keyword">while</span> (set.size() < <span class="number">6</span>) {</span><br><span class="line"> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">randomNum</span> <span class="operator">=</span> random.nextInt(<span class="number">33</span>) + <span class="number">1</span>;</span><br><span class="line"> set.add(randomNum);</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> set;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="comment">/**</span></span><br><span class="line"><span class="comment"> * 买彩票</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> jisl on 2023/7/31 16:07</span></span><br><span class="line"><span class="comment"> **/</span></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">buy</span><span class="params">(<span class="type">int</span> count)</span> {</span><br><span class="line"> lotteryNumber = RandomUtil.randomInt(<span class="number">999</span>);</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < count; i++) {</span><br><span class="line"> <span class="keyword">final</span> ArrayList<Integer> lottery = <span class="keyword">new</span> <span class="title class_">ArrayList</span><>(buildRedNum());</span><br><span class="line"> lottery.add(buildBlueNum());</span><br><span class="line"> userLottery.add(lottery);</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">redemption</span><span class="params">()</span> {</span><br><span class="line"><span class="comment">// 一到六等奖</span></span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i <= MAX_PRIZE_LEVEL; i++) {</span><br><span class="line"> userPrizeLottery.put(i, <span class="keyword">new</span> <span class="title class_">ArrayList</span><>());</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">for</span> (List<Integer> lottery : userLottery) {</span><br><span class="line"> <span class="type">int</span> <span class="variable">readBallMatches</span> <span class="operator">=</span> countMatchingNumbers(lottery);</span><br><span class="line"> <span class="type">boolean</span> <span class="variable">blueBallMatch</span> <span class="operator">=</span> lottery.get(<span class="number">6</span>) == winBlueNum;</span><br><span class="line"> <span class="type">int</span> <span class="variable">prizeItem</span> <span class="operator">=</span> calculatePrize(readBallMatches, blueBallMatch);</span><br><span class="line"> <span class="keyword">if</span> (prizeItem != <span class="number">0</span>) {</span><br><span class="line"><span class="comment">// 中奖了</span></span><br><span class="line"> <span class="keyword">final</span> List<String> numbers = userPrizeLottery.get(prizeItem);</span><br><span class="line"> numbers.add(JSON.toJSONString(lottery));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> System.out.println(String.format(<span class="string">"[第%s期双色球]中奖结果:"</span>, lotteryNumber));</span><br><span class="line"> <span class="type">int</span> <span class="variable">userPrizeCount</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="type">int</span> <span class="variable">userPrizeAmount</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">1</span>; i <= MAX_PRIZE_LEVEL; i++) {</span><br><span class="line"> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">userPrizeItemCount</span> <span class="operator">=</span> userPrizeLottery.get(i).size();</span><br><span class="line"> userPrizeCount += userPrizeItemCount;</span><br><span class="line"> userPrizeAmount += lotteryAmount[i] * userPrizeItemCount;</span><br><span class="line"> <span class="keyword">if</span> (i <= <span class="number">3</span>) {</span><br><span class="line"> System.out.println(String.format(<span class="string">"%s等奖中奖个数:%s,中奖号码:%s"</span>, i, userPrizeItemCount, userPrizeLottery.get(i)));</span><br><span class="line"> } <span class="keyword">else</span> {</span><br><span class="line"> System.out.println(String.format(<span class="string">"%s等奖中奖个数:%s,中奖号码:-"</span>, i, userPrizeItemCount));</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"><span class="comment">// 共计中奖率:6.71%</span></span><br><span class="line"> System.out.println(String.format(<span class="string">"一共买了[%s张]彩票,中奖个数=%s,中奖概率=%s;购买彩票费用=%s,奖金=%s,盈利=%s"</span></span><br><span class="line"> , userLottery.size(), userPrizeCount, <span class="number">1.0</span> * userPrizeCount / userLottery.size(), userLottery.size() * <span class="number">2</span>, userPrizeAmount, userPrizeAmount - userLottery.size() * <span class="number">2</span>));</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="title function_">calculatePrize</span><span class="params">(<span class="type">int</span> readBallMatches, <span class="type">boolean</span> blueBallMatch)</span> {</span><br><span class="line"> <span class="keyword">if</span> (readBallMatches == <span class="number">6</span> && blueBallMatch) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (readBallMatches == <span class="number">6</span>) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (readBallMatches == <span class="number">5</span> && blueBallMatch) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (readBallMatches == <span class="number">5</span> || (readBallMatches == <span class="number">4</span> && blueBallMatch)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">4</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (readBallMatches == <span class="number">4</span> || (readBallMatches == <span class="number">3</span> && blueBallMatch)) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">5</span>;</span><br><span class="line"> } <span class="keyword">else</span> <span class="keyword">if</span> (blueBallMatch) {</span><br><span class="line"> <span class="keyword">return</span> <span class="number">6</span>;</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">private</span> <span class="type">int</span> <span class="title function_">countMatchingNumbers</span><span class="params">(List<Integer> lottery)</span> {</span><br><span class="line"> <span class="type">int</span> <span class="variable">count</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i < <span class="number">6</span>; i++) {</span><br><span class="line"> <span class="keyword">if</span> (winRedNum.contains(lottery.get(i))) {</span><br><span class="line"> count += <span class="number">1</span>;</span><br><span class="line"> }</span><br><span class="line"> }</span><br><span class="line"> <span class="keyword">return</span> count;</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">game</span><span class="params">(<span class="type">int</span> n)</span> {</span><br><span class="line"> buy(n);</span><br><span class="line"> lotteryDrawing();</span><br><span class="line"> redemption();</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"> <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> {</span><br><span class="line"> <span class="keyword">final</span> <span class="type">DoubleColorBallLottery</span> <span class="variable">lottery</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">DoubleColorBallLottery</span>();</span><br><span class="line"> lottery.game(<span class="number">100000</span>);</span><br><span class="line"> }</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">}</span><br><span class="line"></span><br></pre></td></tr></table></figure></div><a class="article-more button is-small is-size-7" href="/2023/11/17/Java%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E5%8F%8C%E8%89%B2%E7%90%83%E6%B8%B8%E6%88%8F%EF%BC%8C%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%B7%B1%E4%B9%B0%E4%B8%AA%E5%BD%A9%E7%A5%A8%E7%8E%A9/#more">阅读更多</a></article></div><div class="card"><article class="card-content article" role="article"><div class="article-meta is-size-7 is-uppercase level is-mobile"><div class="level-left"><span class="level-item"><time dateTime="2023-11-16T07:12:22.000Z" title="2023/11/16 下午3:12:22">2023-11-16</time>发表</span><span class="level-item"><time dateTime="2023-11-16T08:43:43.764Z" title="2023/11/16 下午4:43:43">2023-11-16</time>更新</span><span class="level-item"><a class="link-muted" href="/categories/Java/">Java</a></span><span class="level-item">13 分钟读完 (大约1990个字)</span></div></div><p class="title is-3 is-size-4-mobile"><a class="link-muted" href="/2023/11/16/%E7%BB%9F%E4%B8%80%E6%94%AF%E4%BB%98%E6%8E%A5%E5%8F%A3%EF%BC%8C%E6%97%A0%E9%99%90%E6%89%A9%E5%B1%95%E4%B8%8D%E5%90%8C%E6%94%AF%E4%BB%98%E6%9C%8D%E5%8A%A1%E5%B7%A5%E5%85%B7%E5%8C%85%E5%AE%9E%E7%8E%B0%E3%80%82%E3%80%90%E5%BC%80%E7%AE%B1%E5%8D%B3%E7%94%A8%E3%80%91%EF%BC%88%E5%8F%AF%E7%94%A8%E4%BA%8E%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%EF%BC%89/">统一支付接口,无限扩展不同支付服务工具包实现。【开箱即用】(可用于生产环境中)</a></p><div class="content"><h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>在实际生产环境中,我们经常面临多个支付厂商的选择和变更,这可能导致支付代码的不断修改和维护。为了更好地应对产品需求的变化,我们采用了策略模式,通过统一的支付接口将各个支付服务进行实现。这样一来,支付厂商的变化对业务代码没有任何影响,使得我们能够更灵活地应对支付服务的变化和新增,而无需频繁修改核心业务逻辑。这种设计不仅提高了代码的可维护性,还为未来的扩展和变更提供了便利。</p></div><a class="article-more button is-small is-size-7" href="/2023/11/16/%E7%BB%9F%E4%B8%80%E6%94%AF%E4%BB%98%E6%8E%A5%E5%8F%A3%EF%BC%8C%E6%97%A0%E9%99%90%E6%89%A9%E5%B1%95%E4%B8%8D%E5%90%8C%E6%94%AF%E4%BB%98%E6%9C%8D%E5%8A%A1%E5%B7%A5%E5%85%B7%E5%8C%85%E5%AE%9E%E7%8E%B0%E3%80%82%E3%80%90%E5%BC%80%E7%AE%B1%E5%8D%B3%E7%94%A8%E3%80%91%EF%BC%88%E5%8F%AF%E7%94%A8%E4%BA%8E%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%EF%BC%89/#more">阅读更多</a></article></div><div class="card"><article class="card-content article" role="article"><div class="article-meta is-size-7 is-uppercase level is-mobile"><div class="level-left"><span class="level-item"><time dateTime="2023-11-14T09:16:50.000Z" title="2023/11/14 下午5:16:50">2023-11-14</time>发表</span><span class="level-item"><time dateTime="2023-11-16T08:45:55.635Z" title="2023/11/16 下午4:45:55">2023-11-16</time>更新</span><span class="level-item"><a class="link-muted" href="/categories/%E7%AE%97%E6%B3%95/">算法</a></span><span class="level-item">3 分钟读完 (大约400个字)</span></div></div><p class="title is-3 is-size-4-mobile"><a class="link-muted" href="/2023/11/14/%E3%80%90%E7%89%88%E6%9C%AC%E5%8F%B7%E6%AF%94%E8%BE%83%E5%B7%A5%E5%85%B7-%EF%BC%88%E7%AE%97%E6%B3%95%E7%89%88%EF%BC%89%E3%80%91/">【版本号比较工具-(算法版)】</a></p><div class="content"><h1 id="1-版本号工具"><a href="#1-版本号工具" class="headerlink" title="1.版本号工具"></a>1.版本号工具</h1><p>之前在网上看到版本号工具,觉得还行,就一直用这个。代码如下,写的也挺简洁的</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="type">int</span> <span class="title function_">compareAppVersion</span><span class="params">(String version1, String version2)</span> {</span><br><span class="line"> <span class="keyword">if</span> (version1 == <span class="literal">null</span> || version2 == <span class="literal">null</span>) {</span><br><span class="line"> <span class="keyword">throw</span> <span class="keyword">new</span> <span class="title class_">RuntimeException</span>(<span class="string">"版本号不能为空"</span>);</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 注意此处为正则匹配,不能用.</span></span><br><span class="line"> String[] versionArray1 = version1.split(<span class="string">"\\."</span>);</span><br><span class="line"> String[] versionArray2 = version2.split(<span class="string">"\\."</span>);</span><br><span class="line"> <span class="type">int</span> <span class="variable">idx</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 取数组最小长度值</span></span><br><span class="line"> <span class="type">int</span> <span class="variable">minLength</span> <span class="operator">=</span> Math.min(versionArray1.length, versionArray2.length);</span><br><span class="line"> <span class="type">int</span> <span class="variable">diff</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line"> <span class="comment">// 先比较长度,再比较字符</span></span><br><span class="line"> <span class="keyword">while</span> (idx < minLength</span><br><span class="line"> && (diff = versionArray1[idx].length() - versionArray2[idx].length()) == <span class="number">0</span></span><br><span class="line"> && (diff = versionArray1[idx].compareTo(versionArray2[idx])) == <span class="number">0</span>) {</span><br><span class="line"> ++idx;</span><br><span class="line"> }</span><br><span class="line"> <span class="comment">// 如果已经分出大小,则直接返回,如果未分出大小,则再比较位数,有子版本的为大</span></span><br><span class="line"> diff = (diff != <span class="number">0</span>) ? diff : versionArray1.length - versionArray2.length;</span><br><span class="line"> <span class="keyword">return</span> diff;</span><br><span class="line">}</span><br></pre></td></tr></table></figure></div><a class="article-more button is-small is-size-7" href="/2023/11/14/%E3%80%90%E7%89%88%E6%9C%AC%E5%8F%B7%E6%AF%94%E8%BE%83%E5%B7%A5%E5%85%B7-%EF%BC%88%E7%AE%97%E6%B3%95%E7%89%88%EF%BC%89%E3%80%91/#more">阅读更多</a></article></div></div><div class="column column-left is-4-tablet is-4-desktop is-4-widescreen order-1"><div class="card widget" data-type="profile"><div class="card-content"><nav class="level"><div class="level-item has-text-centered flex-shrink-1"><div><figure class="image is-128x128 mx-auto mb-2"><img class="avatar is-rounded" src="https://assets.leetcode.cn/aliyun-lc-upload/users/jishenglong92/avatar_1642208937.png?x-oss-process=image%2Fformat%2Cwebp" alt="纪东东"></figure><p class="title is-size-4 is-block" style="line-height:inherit;">纪东东</p><p class="is-size-6 is-block">All is Well</p><p class="is-size-6 is-flex justify-content-center"><i class="fas fa-map-marker-alt mr-1"></i><span>长安</span></p></div></div></nav><nav class="level is-mobile"><div class="level-item has-text-centered is-marginless"><div><p class="heading">文章</p><a href="/archives"><p class="title">3</p></a></div></div><div class="level-item has-text-centered is-marginless"><div><p class="heading">分类</p><a href="/categories"><p class="title">2</p></a></div></div><div class="level-item has-text-centered is-marginless"><div><p class="heading">标签</p><a href="/tags"><p class="title">2</p></a></div></div></nav><div class="level"><a class="level-item button is-primary is-rounded" href="https://github.com/jsl1992" target="_blank" rel="noopener">关注我</a></div><div class="level is-mobile is-multiline"><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="Github" href="https://github.com/jsl1992"><i class="fab fa-github"></i></a><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="Email" href="mailto:[email protected]"><i class="fa fa-envelope"></i></a><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="Weibo" href="/"><i class="fab fa-weibo"></i></a><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="WeiXin" href="/"><i class="fab fa-weixin"></i></a><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="Next" href="https://blog.cnhuashao.com/remove.io"><i class="fab fa-dribbble"></i></a><a class="level-item button is-transparent is-marginless" target="_blank" rel="noopener" title="RSS" href="/"><i class="fas fa-rss"></i></a></div></div></div><!--!--><div class="card widget" data-type="categories"><div class="card-content"><div class="menu"><h3 class="menu-label">分类</h3><ul class="menu-list"><li><a class="level is-mobile" href="/categories/Java/"><span class="level-start"><span class="level-item">Java</span></span><span class="level-end"><span class="level-item tag">2</span></span></a></li><li><a class="level is-mobile" href="/categories/%E7%AE%97%E6%B3%95/"><span class="level-start"><span class="level-item">算法</span></span><span class="level-end"><span class="level-item tag">1</span></span></a></li></ul></div></div></div><div class="card widget" data-type="recent-posts"><div class="card-content"><h3 class="menu-label">最新文章</h3><article class="media"><div class="media-content"><p class="date"><time dateTime="2023-11-17T06:56:06.000Z">2023-11-17</time></p><p class="title"><a href="/2023/11/17/Java%E6%89%8B%E5%8A%A8%E5%AE%9E%E7%8E%B0%E4%B8%80%E4%B8%AA%E5%8F%8C%E8%89%B2%E7%90%83%E6%B8%B8%E6%88%8F%EF%BC%8C%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%B7%B1%E4%B9%B0%E4%B8%AA%E5%BD%A9%E7%A5%A8%E7%8E%A9/">Java手动实现一个双色球游戏,每天自己买个彩票玩</a></p><p class="categories"><a href="/categories/Java/">Java</a></p></div></article><article class="media"><div class="media-content"><p class="date"><time dateTime="2023-11-16T07:12:22.000Z">2023-11-16</time></p><p class="title"><a href="/2023/11/16/%E7%BB%9F%E4%B8%80%E6%94%AF%E4%BB%98%E6%8E%A5%E5%8F%A3%EF%BC%8C%E6%97%A0%E9%99%90%E6%89%A9%E5%B1%95%E4%B8%8D%E5%90%8C%E6%94%AF%E4%BB%98%E6%9C%8D%E5%8A%A1%E5%B7%A5%E5%85%B7%E5%8C%85%E5%AE%9E%E7%8E%B0%E3%80%82%E3%80%90%E5%BC%80%E7%AE%B1%E5%8D%B3%E7%94%A8%E3%80%91%EF%BC%88%E5%8F%AF%E7%94%A8%E4%BA%8E%E7%94%9F%E4%BA%A7%E7%8E%AF%E5%A2%83%E4%B8%AD%EF%BC%89/">统一支付接口,无限扩展不同支付服务工具包实现。【开箱即用】(可用于生产环境中)</a></p><p class="categories"><a href="/categories/Java/">Java</a></p></div></article><article class="media"><div class="media-content"><p class="date"><time dateTime="2023-11-14T09:16:50.000Z">2023-11-14</time></p><p class="title"><a href="/2023/11/14/%E3%80%90%E7%89%88%E6%9C%AC%E5%8F%B7%E6%AF%94%E8%BE%83%E5%B7%A5%E5%85%B7-%EF%BC%88%E7%AE%97%E6%B3%95%E7%89%88%EF%BC%89%E3%80%91/">【版本号比较工具-(算法版)】</a></p><p class="categories"><a href="/categories/%E7%AE%97%E6%B3%95/">算法</a></p></div></article></div></div><div class="card widget" data-type="archives"><div class="card-content"><div class="menu"><h3 class="menu-label">归档</h3><ul class="menu-list"><li><a class="level is-mobile" href="/archives/2023/"><span class="level-start"><span class="level-item">2023</span></span><span class="level-end"><span class="level-item tag">3</span></span></a></li></ul></div></div></div><div class="card widget" data-type="tags"><div class="card-content"><div class="menu"><h3 class="menu-label">标签</h3><div class="field is-grouped is-grouped-multiline"><div class="control"><a class="tags has-addons" href="/tags/Java/"><span class="tag">Java</span><span class="tag">3</span></a></div><div class="control"><a class="tags has-addons" href="/tags/%E7%AE%97%E6%B3%95/"><span class="tag">算法</span><span class="tag">1</span></a></div></div></div></div></div></div><!--!--></div></div></section><footer class="footer"><div class="container"><div class="level"><div class="level-start"><a class="footer-logo is-block mb-2" href="/"><img src="/img/logo.jpeg" alt="纪东东-博客" height="28"></a><p class="is-size-7"><span>© 2023 纪东东</span> Powered by <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a> & <a href="https://github.com/ppoffice/hexo-theme-icarus" target="_blank" rel="noopener">Icarus</a></p><p class="is-size-7">© 2019</p></div><div class="level-end"><div class="field has-addons"><p class="control"><a class="button is-transparent is-large" target="_blank" rel="noopener" title="Creative Commons" href="https://creativecommons.org/"><i class="fab fa-creative-commons"></i></a></p><p class="control"><a class="button is-transparent is-large" target="_blank" rel="noopener" title="Attribution 4.0 International" href="https://creativecommons.org/licenses/by/4.0/"><i class="fab fa-creative-commons-by"></i></a></p><p class="control"><a class="button is-transparent is-large" target="_blank" rel="noopener" title="GitHub" href="https://github.com/jsl1992"><i class="fab fa-github"></i></a></p></div></div></div></div></footer><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/min/moment-with-locales.min.js"></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/clipboard.min.js" defer></script><script>moment.locale("zh-cn");</script><script>var IcarusThemeSettings = {
article: {
highlight: {
clipboard: true,
fold: 'unfolded'
}
}
};</script><script src="/js/column.js"></script><script src="/js/animation.js"></script><a id="back-to-top" title="回到顶端" href="javascript:;"><i class="fas fa-chevron-up"></i></a><script src="/js/back_to_top.js" defer></script><!--!--><!--!--><!--!--><script src="https://cdn.jsdelivr.net/npm/[email protected]/build/cookieconsent.min.js" defer></script><script>window.addEventListener("load", () => {
window.cookieconsent.initialise({
type: "info",
theme: "edgeless",
static: false,
position: "bottom-left",
content: {
message: "此网站使用Cookie来改善您的体验。",
dismiss: "知道了!",
allow: "允许使用Cookie",
deny: "拒绝",
link: "了解更多",
policy: "Cookie政策",
href: "https://www.cookiesandyou.com/",
},
palette: {
popup: {
background: "#edeff5",
text: "#838391"
},
button: {
background: "#4b81e8"
},
},
});
});</script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/lightgallery.min.js" defer></script><script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/jquery.justifiedGallery.min.js" defer></script><script>window.addEventListener("load", () => {
if (typeof $.fn.lightGallery === 'function') {
$('.article').lightGallery({ selector: '.gallery-item' });
}
if (typeof $.fn.justifiedGallery === 'function') {
if ($('.justified-gallery > p > .gallery-item').length) {
$('.justified-gallery > p > .gallery-item').unwrap();
}
$('.justified-gallery').justifiedGallery();
}
});</script><!--!--><!--!--><!--!--><!--!--><!--!--><script src="/js/main.js" defer></script><div class="searchbox"><div class="searchbox-container"><div class="searchbox-header"><div class="searchbox-input-container"><input class="searchbox-input" type="text" placeholder="想要查找什么..."></div><a class="searchbox-close" href="javascript:;">×</a></div><div class="searchbox-body"></div></div></div><script src="/js/insight.js" defer></script><script>document.addEventListener('DOMContentLoaded', function () {
loadInsight({"contentUrl":"/content.json"}, {"hint":"想要查找什么...","untitled":"(无标题)","posts":"文章","pages":"页面","categories":"分类","tags":"标签"});
});</script></body></html>