風行草偃

陪伴,是最长情的告白。

java模拟福利彩双色球游戏(代码)

chakoe:

下面为代码:


====================


//开始代码


import java.util.Random;


import java.util.Scanner;




//福利彩双色球游戏


public class Test1 {


/*


 * 1.生成随机号码  6红,1蓝


 * 2.接收用户输入  6红,1蓝


 * 3.验证:随机号码,用户号码,是否中奖


 * 4.公布中奖号码结果


 * */


public static void main(String[] args)


{


int[] sysRedBall = new int[6];  //存储系统生成的红球


int sysBlueBall = 0;  //存储系统生成的蓝球


int[] userRedBall = new int[6];  //存储用户选择的红球


int userBlueBall = 0;  //存储用户选择的蓝球


int redConnt = 0;  //正确的红球


int blueConnt = 0;  //正确的蓝球


Random r = new Random();  //初始化随机数


Scanner in = new Scanner(System.in);


//定义一个数组,解决系统随机生成的重复数


int[] redBall = {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};


int[] redBalla = {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};


//游戏开始


System.out.println("双色球,开始游戏");


int index = 0; //定义系统随机下标


//生成随机号码 


for (int i = 0; i < sysRedBall.length; i++)


{


while (true)


{


index = r.nextInt(33);


if (redBall[index]!=-1)


{


sysRedBall[i] = redBall[index];


redBall[index] =-1;


break;


}


}


}


sysBlueBall = r.nextInt(16);


//接收用户选号


System.out.println("请选择红球号码(1-33):");


int indexa = 0;  //定义用户输入的下标


for (int i = 0; i < userRedBall.length; i++)


{


while (true)


{


System.out.print("输入第"+(i+1)+"个红球:");


indexa = in.nextInt();


if (indexa>33||indexa<=0)  //判断用户输入的整数是否为(1-33)以内


{


System.out.print("输入超出(1-33)范围,请重新,");


//System.exit(0);


for (int j = 0; j < 0; j++)


{


indexa = in.nextInt();


}


}


else if(redBalla[indexa-1]!=-1)  //解决用户输入重复整数问题


{


userRedBall[i] = redBalla[indexa-1];


redBalla[indexa-1] =-1;


break;


}


}


}


System.out.println("请选择蓝球号码(1-16):");


userBlueBall = in.nextInt();


//验证用户是否中奖


for (int i = 0; i < sysRedBall.length; i++)


{


for (int j = 0; j < userRedBall.length; j++)


{


if (sysRedBall[i]==userRedBall[j])


{


redConnt++;


break;


}


}


}


if (sysBlueBall==userBlueBall)


{


blueConnt++;


}


//判断是否中奖


if (redConnt==6&&blueConnt==1)


{


System.out.println("恭喜你,中一等奖,500万!");


}


else if (redConnt==6)


{


System.out.println("恭喜你,中二等奖,12万!");



else if(redConnt==5&&blueConnt==1)


{


System.out.println("恭喜你,中三等奖,3000块!");


}


else if(redConnt==5||(redConnt==4&&blueConnt==1))


{


System.out.println("恭喜你,中四等奖,200块!");


}


else if(redConnt==4||(redConnt==3&&blueConnt==1))


{


System.out.println("恭喜你,中五等奖,10块!");


}


else if(blueConnt==1)


{


System.out.println("恭喜你,中六等奖,5块!");


}


else


{


System.out.println("很遗憾,你没中奖,谢谢支持!");


}


System.out.print("用户红球号码:");


for (int i = 0; i < userRedBall.length-1; i++)


{


for (int j = 0; j < userRedBall.length-1; j++)


{


if (userRedBall[j]>userRedBall[j+1])


{


int temp = userRedBall[j];


userRedBall[j] = userRedBall[j+1];


userRedBall[j+1] = temp;


}


}


}


for (int i = 0; i < userRedBall.length; i++)


{


System.out.print(userRedBall[i] + " ");


}


System.out.print("蓝色球:" + userBlueBall);




System.out.println("\n=====================");


//输出结果


System.out.print("本期红球号码:");


for (int i = 0; i < sysRedBall.length-1; i++)


{


for (int j = 0; j < sysRedBall.length-1; j++)


{


if (sysRedBall[j]>sysRedBall[j+1])


{


int temp = sysRedBall[j];


sysRedBall[j] = sysRedBall[j+1];


sysRedBall[j+1] = temp;


}


}


}


for (int i = 0; i < sysRedBall.length; i++)


{


System.out.print(sysRedBall[i] + " ");


}


System.out.print("蓝色球:" + sysBlueBall);


}




}




//结束代码


=================



评论
热度 ( 1 )
  1. 風行草偃chakoe 转载了此文字

© 風行草偃 | Powered by LOFTER