最近做了一道关于双色球中奖的题目。
题目比较简单,写的也比较乱,写之前也没有很清晰的思考到全部细节,总是写一会儿想一会儿。
希望经过练习,可以积累更多的经验,让下一次代码变得更好
import java.util.Random;
import java.util.Scanner;
public class HelloJava {
public static void main(String[] args) {
int arr[] = creatNumber();
int userNumber[] = scNumber();
int redCounts = containsRed(arr,userNumber);
int blueCounts = containsBlue(arr,userNumber);
judgePrize(redCounts,blueCounts);
}
public static int[] creatNumber(){
int arr[] = new int[33];
int number[] = new int[7];
for (int i = 0; i < 33; i++) {
arr[i] = i + 1;
}
Random r = new Random();
for (int i = 0; i < number.length-1; ) {
int index = r.nextInt(33);
for (int j = 0; j <= number.length-1; j++) {
if(number[j] == arr[index]){
break;
}else if(j == number.length-1){
number[i] = arr[index];
i++;
}
}
}
number[6] = r.nextInt(16) + 1;
return number;
}
public static int[] scNumber(){
Scanner sc = new Scanner(System.in);
int arr[] = new int[7];
int count = 1;
while(count<7){
int flag = 1;
System.out.println("请输入第"+count+"个红球号码");
int number = sc.nextInt();
for (int i = 0; i < arr.length-1; i++) {
if(number == arr[i]){
flag = 0;
}
}
if(number>=1&&number<=33&&flag==1){
arr[count-1] = number;
count++;
}else{
System.out.println("输入不合法,请输入1-33的数字,请勿重复输入同一数字");
}
}
System.out.println("请输入蓝球号码");
boolean i = true;
while(i){
int number2 = sc.nextInt();
if(number2>=1&&number2<=16){
arr[arr.length-1] = number2;
i = false;
}else{
System.out.println("输入不合法,请输入1-16的数字");
}
}
return arr;
}
public static int containsRed(int[]arr,int[]arr2){
int redCount = 0;
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr2.length-1; j++) {
if(arr[i] == arr2[j]){
redCount++;
}
}
}
return redCount;
}
public static int containsBlue(int[]arr,int[]arr2){
int blueCount = 0;
if(arr[arr.length-1] == arr2[arr2.length-1]){
blueCount++;
}
return blueCount;
}
public static void judgePrize(int redNumber,int blueNumber){
if(redNumber == 6 && blueNumber == 1){
System.out.println("恭喜你中了1000万");
}else if(redNumber == 6 && blueNumber == 0){
System.out.println("恭喜你中了50万");
}else if(redNumber == 5 && blueNumber == 1){
System.out.println("恭喜你中了3000元");
}else if((redNumber == 5 && blueNumber ==0)||(redNumber==4&&blueNumber==1)){
System.out.println("恭喜你中了200元");
}else if((redNumber==4 && blueNumber==0)||(redNumber==3&&blueNumber==1)||(redNumber==2&&blueNumber==1)){
System.out.println("恭喜你中了10元");
}else if((redNumber==1&&blueNumber==1)||(redNumber==0)&&blueNumber==1){
System.out.println("恭喜你中了5元");
}else{
System.out.println("感谢参与");
}
}
}