一、程序规则要求
1、双色球”彩票投注区分为红色球号码区和蓝色球号码区;
2、“双色球”每注投注号码由6个红色球号码和1个蓝色球号码组成;
3、红色球号码从1--33中选择;蓝色球号码从1--16中选择;
4、红色号码不能重复出现。
5、中奖金额规则
二、分步解析
1、利用函数产生随机数获取到我们的双色球幸运奖号码牌,可使用Random获取随机值;
理论上也可使用(int)(Math.random()*100)%33;获取到红色球号码牌;
math.random生成随机数范围为(0,1],乘以100,强转为int范围为(0,100);
再取模可得(0,33)随机数,考虑到概率分布啥的就不使用这个了;
也可math.random()*33+1取到(0,33】,都可以尝试下;
最后会附上整个程序代码;
该部分代码为:
for ( int i = 0 ; i < luckyBall. length - 1 ; i++ ) { int luckyNumber = random. nextInt ( 32 ) + 1 ; while ( isExist ( luckyNumber, luckyBall) ) { luckyNumber = random. nextInt ( 32 ) + 1 ; } luckyBall[ i] = luckyNumber; } luckyBall[ luckyBall. length - 1 ] = random. nextInt ( 15 ) + 1 ;
2、上面看到一个方法isExsit(),该方法用于判断随机得到的数和数组里目前存储的数是否存在相同的;
因为luckyBall数组我直接定义的长度为7,所以不需要额外扩容什么操作;
自己买的号码myBall我使用的是数组扩容调用;
该方法代码如下:
public static boolean isExist ( int luckyNumber, int [ ] luckyBall) { for ( int i = 0 ; i < luckyBall. length; i++ ) { if ( luckyNumber == luckyBall[ i] ) { return true ; } } return false ; }
3、获取了幸运号码,也得完成用户买的号码,用户购买双色球红球范围(0,33】,蓝球(0,16】;
需要判断用户输入是否在这个区间,不在区间则重新输入;
此处由于不确定用户可能输错多少次不在范围内的数;所以使用while循环;
一共需要输入有效的6个红球值,外层循环6次,内层循环直到输入合法才break退出内层while循环;
使用数组进行存储每一次的红球值;
此处需利用数组的一个方法;
Arrays.copyOf(数组名,扩至多少长度);该方法是jdk里带有的可直接扩数组的方法;
此部分代码如下:
while ( myBall. length < 6 ) { while ( true ) { System . out. print ( "请您输入您要购买的第" + ( myBall. length + 1 ) + "个红球号码:" ) ; int number = scan. nextInt ( ) ; if ( number < 1 || number > 33 ) { System . out. println ( "请重新输入1-33的整数:" ) ; } else { if ( isExist ( number, myBall) ) { System . out. println ( "已经选过该数,无法再次选择" ) ; } else { myBall = Arrays . copyOf ( myBall, myBall. length + 1 ) ; myBall[ myBall. length - 1 ] = number; break ; } } } } while ( true ) { System . out. println ( "请您输入您要购买的蓝球号码:" ) ; int number = scan. nextInt ( ) ; if ( number < 1 || number > 16 ) { System . out. println ( "请重新输入1-16的整数:" ) ; } else { myBall = Arrays . copyOf ( myBall, myBall. length+ 1 ) ; myBall[ myBall. length - 1 ] = number; break ; } }
4、有了随机值产生的数组,有了自己选的号码数组;
又在前面控制过,红球中不会出现相同值,那么我们只需要遍历我们的选号每一个;
拿去和幸运号码数组对比,有一个标记一下;
此处需要注意,红球对比的数为6个,即从下标0开始只能到下标5,所以需要length-1即<6进行遍历
int equalRedNumber = 0 ; System . out. println ( myBall. length) ; System . out. println ( Arrays . toString ( luckyBall) ) ; for ( int i = 0 ; i < myBall. length- 1 ; i++ ) { for ( int j = 0 ; j < myBall. length- 1 ; j++ ) { if ( myBall[ i] == luckyBall[ j] ) { equalRedNumber++ ; } } } int equalBuleNumber = 0 ; if ( myBall[ myBall. length - 1 ] == luckyBall[ luckyBall. length - 1 ] ) { equalBuleNumber++ ; }
5、有了红球中奖数和蓝球中奖数,再根据规则编写if else中奖条件便完成了。
三、整个程序代码
package day08. work ; import java. util. Arrays ;
import java. util. Random ;
import java. util. Scanner ; public class TwoColor { public static void main ( String [ ] args) { Scanner scan = new Scanner ( System . in) ; Random random = new Random ( ) ; int [ ] luckyBall = new int [ 7 ] ; int [ ] myBall = { } ;
for ( int i = 0 ; i < luckyBall. length - 1 ; i++ ) { int luckyNumber = random. nextInt ( 32 ) + 1 ; while ( isExist ( luckyNumber, luckyBall) ) { luckyNumber = random. nextInt ( 32 ) + 1 ; } luckyBall[ i] = luckyNumber; } luckyBall[ luckyBall. length - 1 ] = random. nextInt ( 15 ) + 1 ; System . out. println ( "这里输出系统自动生成的随机数作为幸运号码,用于测试。" + Arrays . toString ( luckyBall) ) ;
while ( myBall. length < 6 ) { while ( true ) { System . out. print ( "请您输入您要购买的第" + ( myBall. length + 1 ) + "个红球号码:" ) ; int number = scan. nextInt ( ) ; if ( number < 1 || number > 33 ) { System . out. println ( "请重新输入1-33的整数:" ) ; } else { if ( isExist ( number, myBall) ) { System . out. println ( "已经选过该数,无法再次选择" ) ; } else { myBall = Arrays . copyOf ( myBall, myBall. length + 1 ) ; myBall[ myBall. length - 1 ] = number; break ; } } } } while ( true ) { System . out. println ( "请您输入您要购买的蓝球号码:" ) ; int number = scan. nextInt ( ) ; if ( number < 1 || number > 16 ) { System . out. println ( "请重新输入1-16的整数:" ) ; } else { myBall = Arrays . copyOf ( myBall, myBall. length+ 1 ) ; myBall[ myBall. length - 1 ] = number; break ; } } int equalRedNumber = 0 ; System . out. println ( myBall. length) ; System . out. println ( Arrays . toString ( luckyBall) ) ; for ( int i = 0 ; i < myBall. length- 1 ; i++ ) { for ( int j = 0 ; j < myBall. length- 1 ; j++ ) { if ( myBall[ i] == luckyBall[ j] ) { equalRedNumber++ ; } } } int equalBuleNumber = 0 ; if ( myBall[ myBall. length - 1 ] == luckyBall[ luckyBall. length - 1 ] ) { equalBuleNumber++ ; } System . out. println ( "红中" + equalRedNumber+ "蓝中" + equalBuleNumber) ;
if ( equalRedNumber == 6 && equalBuleNumber == 1 ) { System . out. println ( "恭喜您获得一等奖500w" ) ; } else if ( equalRedNumber == 6 ) { System . out. println ( "恭喜您获得二等奖100w" ) ; } else if ( equalRedNumber == 5 && equalBuleNumber == 1 ) { System . out. println ( "恭喜您获得三等奖3000" ) ; } else if ( equalRedNumber == 5 || ( equalRedNumber == 4 && equalBuleNumber == 1 ) ) { System . out. println ( "恭喜您获得四等奖200" ) ; } else if ( equalRedNumber == 4 || ( equalRedNumber == 3 && equalBuleNumber == 1 ) ) { System . out. println ( "恭喜您获得五等奖10" ) ; } else if ( equalBuleNumber == 1 ) { System . out. println ( "恭喜您获得六等奖5" ) ; } else { System . out. println ( "感谢您为公益事业做出的贡献!" ) ; } } public static boolean isExist ( int luckyNumber, int [ ] luckyBall) { for ( int i = 0 ; i < luckyBall. length; i++ ) { if ( luckyNumber == luckyBall[ i] ) { return true ; } } return false ; }
}