Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that simulates a lottery. The program should have an array of 5

ID: 3774563 • Letter: W

Question

Write a program that simulates a lottery. The program should have an array of 5 integers named winning Digits, with a randomly generated number in the range of 0 through 9 for each element in the array. The program should ask the user to enter 5 digits and should store them in a second integer array named player. The program must compare the corresponding elements in the two arrays and count how many digits match. For example, the following shows the winning Digits array and the Player array with sample numbers stored in each. There are two matching digits, elements 2 and 4. Once the user has entered a set of numbers, the program should display the winning digits and the player's digits and tell how many digits matched.

Explanation / Answer

package chegg2;

import java.util.Scanner;

public class RandomNum {

   public static void main(String args[]){
      
       int WinningDigits[]=new int[5];
       int player[]=new int[5];
       int count=0;
       Scanner scanner = new Scanner(System.in);
       System.out.println("Enter 5 digits");
  
       for(int i=0;i<5;i++){
           WinningDigits[i]=(int)(Math.random() * 10);
           player[i]=Integer.parseInt(scanner.nextLine());
       }
       scanner.close();
       System.out.print("Winning Digits");
       for(int j=0;j<5;j++){
           System.out.print(" "+ WinningDigits[j]);  
       }
       System.out.println();

       System.out.print("player ");

       for(int j=0;j<5;j++){
           System.out.print(" "+ player[j]);  
       }
       System.out.println();
      
       for(int j=0;j<5;j++){
           if(WinningDigits[j]==player[j]){
               count++;
           }
      
       }
       System.out.println("Number of matching digits: "+count);
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote