当前位置:网站首页>Bicolor case

Bicolor case

2022-07-04 06:14:00 Zhangmingming

p69 The task is described as follows
It feels better than writing data structures c The code is a lot simpler !!
hope 10 God, get started !
 Insert picture description here

package com.itheima.practice;

import java.util.Random;
import java.util.Scanner;

public class Demo5 {
    
    public static void main(String[] args) {
    
        // Generated random lucky numbers 
        int[] Lucknumber=creatLuckNumber();
        // The selection number entered by the user 
        int[] Usernumber=userInputNumer();
        // Judge the winning situation 
        judge(Lucknumber,Usernumber);


    }
    // How to generate lucky numbers 
    public static int[] creatLuckNumber(){
    
        // Define a dynamic array , Storage 7 A digital 
        int[] numbers=new int[7];
        // Traversal array , Generate 7 A lucky number 
        // The first six are red balls (1-33) Can't repeat , The last blue ball (1-16)
        Random r =new Random();
        for (int i = 0; i < 7; i++) {
    
            while(true){
    
                // Generate a 1-33 The random number 
                int data=r.nextInt(33)+1;
                boolean flag=true;
                // Check for repetition 
                for (int j = 0; j < i; j++) {
    
                    if(numbers[i]==data){
    
                        flag=false;
                        break;
                    }
                }
                if(flag){
    
                    numbers[i]=data;
                    break;
                }
            }
        }
        // Generate for blue ball 1-16 The random number 
        numbers[6]=r.nextInt(16)+1;
        return numbers;
    }
    // User input method of number selection 
    public static int[] userInputNumer(){
    
        int[] numbers=new int[7];

        Scanner sc=new Scanner(System.in);
        for (int i = 0; i < numbers.length-1; i++) {
    
            System.out.println(" Please enter the number "+(i+1)+" Red number (1-33)");
            int data=sc.nextInt();
            numbers[i]=data;
        }
        System.out.println(" Please enter the blue number (1-16)");
        numbers[6]=sc.nextInt();
        return numbers;
    }

    public static void judge(int[] Lucknumber,int[] usernumber) {
    
        int redHit=0;
        int blueHit=0;
        // Judge how many red balls hit 
        for (int i = 0; i < 6; i++) {
    
            for (int j = 0; j < 6; j++) {
    
                if(Lucknumber[j]==usernumber[i]){
    
                    redHit++;
                    break;
                }
            }
        }
        // Judge whether the last one hits 
        if(Lucknumber[6]==usernumber[6]) blueHit++;

        // Show winning details 
        System.out.print(" The lucky number is :");
        printarray(Lucknumber);
        System.out.println(" The number you chose is :");
        printarray(usernumber);
        System.out.println(" The red ball hit "+redHit+" individual , Blue ball hit "+blueHit+" individual ");

        // Judge the winning situation 
        if(blueHit==1&&redHit<3) System.out.println(" Congratulations on your success 5 element ");
        else if(blueHit==1&&redHit==3||blueHit==0&&redHit==4) System.out.println(" Congratulations on your success 10 element ");
        else if(blueHit==1&&redHit==4||blueHit==0&&redHit==5) System.out.println(" Congratulations on your success 200 element ");
        else if(blueHit==1&&redHit==5) System.out.println(" Congratulations on your success 3000 element ");
        else if(blueHit==0&&redHit==6) System.out.println(" Congratulations on your success 500 Ten thousand yuan ");
        else if(blueHit==1&&redHit==6) System.out.println(" Congratulations on your success 1000 Ten thousand yuan ");
        else System.out.println(" I'm sorry I didn't win the prize ");

    }

    public static void printarray(int[] arr) {
    
        System.out.print("[");
        for (int i = 0; i < arr.length; i++) {
    
            System.out.print(i== arr.length?arr[i]:arr[i]+",");
        }
        System.out.println("]");
    }


}

result
Can't , I am the chosen one !!!
 Insert picture description here

原网站

版权声明
本文为[Zhangmingming]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207040610572363.html