Question: ? alter this by comparing each element in the users array against every element in the lottery array. Return the number of digits that match.

? alter this by comparing each element in the users array against every element in the lottery array. Return the number of digits that match. ?

import java.util.Random;

public class LotteryApplication{ private int [] lotteryNumbers; private int [] userLotteryNumbers; private int matches; public void getUserLotteryNumbers( int [] userLotteryNumbersGiven ) { userLotteryNumbers = userLotteryNumbersGiven; } public int [] getLotteryNumbers(){ return lotteryNumbers; } public int [] compareLotteryNumbers() { int [] matchedNumbers = new int [ lotteryNumbers.length ]; // 0 0 0 0 0 for( int currentMatchedNumberIndex = 0; currentMatchedNumberIndex < matchedNumbers.length; currentMatchedNumberIndex++ ){ matchedNumbers[ currentMatchedNumberIndex ] = -1; // -1 -1 -1 -1 -1 } for( int currentUserLotteryNumberIndex = 0; currentUserLotteryNumberIndex < userLotteryNumbers.length; currentUserLotteryNumberIndex++ ){ if( userLotteryNumbers[ currentUserLotteryNumberIndex ] == lotteryNumbers[ currentUserLotteryNumberIndex ] ){ matchedNumbers[ currentUserLotteryNumberIndex ] = userLotteryNumbers[ currentUserLotteryNumberIndex ]; } } return matchedNumbers; } public void printPrize() { for( int index = 0; index < 5; index++ ) System.out.println( lotteryNumbers + "\t"); System.out.println(); for( int index = 0; index < 5; index++ ); System.out.print(lotteryNumbers + "\t"); System.out.println(); if (matches == 5) System.out.println( "Prize $100,000 " ); else if ( matches == 4 ) System.out.println( "Prize $10,000 " ); else if ( matches == 3 ) System.out.println( "Prize $1,000 " ); else if ( matches == 2 ) System.out.println( "Prize $100 " ); else System.out.println( "Sorry, no match " ); }

public LotteryApplication( int numberOfLotteryNumbers ) { Random random = new Random(); lotteryNumbers = new int [ numberOfLotteryNumbers ]; for( int currentLotteryNumberIndex = 0; currentLotteryNumberIndex < lotteryNumbers.length; currentLotteryNumberIndex++ ){ lotteryNumbers[ currentLotteryNumberIndex ] = random.nextInt( 10); } } }

// MAIN Method

import java.util.Scanner;

public class LotteryApplicationTest{ public static void main( String [] args ) { final int NUMBER_OF_LOTTERY_NUMBERS = 1; Scanner keyboard= new Scanner( System.in ); int userLotteryNumber; int [] userLotteryNumbers = new int [ NUMBER_OF_LOTTERY_NUMBERS ]; int [] matchedNumbers = new int [ NUMBER_OF_LOTTERY_NUMBERS ]; int [] lotteryNumbers = new int [ NUMBER_OF_LOTTERY_NUMBERS ]; int numberOfMatchedNumbers = 0; LotteryApplication worldLottery = new LotteryApplication ( NUMBER_OF_LOTTERY_NUMBERS ); for( int currentLotteryNumberIndex = 0; currentLotteryNumberIndex < NUMBER_OF_LOTTERY_NUMBERS; currentLotteryNumberIndex++ ) { System.out.println( "Please enter lottery number " + ( currentLotteryNumberIndex + 1 ) ); userLotteryNumber = keyboard.nextInt(); userLotteryNumbers[ currentLotteryNumberIndex ] = userLotteryNumber; } worldLottery.getUserLotteryNumbers( userLotteryNumbers ); matchedNumbers = worldLottery.compareLotteryNumbers(); lotteryNumbers = worldLottery.getLotteryNumbers(); System.out.println( " Today's lottery Numbers are: " ); for( int currentLotteryNumberIndex = 0; currentLotteryNumberIndex < lotteryNumbers.length; currentLotteryNumberIndex++ ){ System.out.println( lotteryNumbers[ currentLotteryNumberIndex ] + ", " ); } System.out.println(); System.out.println( " Your numbers are: " ); for( int currentUserLotteryNumberIndex = 0; currentUserLotteryNumberIndex < userLotteryNumbers.length; currentUserLotteryNumberIndex++ ){ System.out.println( lotteryNumbers[ currentUserLotteryNumberIndex ] + ", " ); } System.out.println(); System.out.println( " Matched numbers: " ); for( int currentMatchedNumberIndex = 0; currentMatchedNumberIndex < matchedNumbers.length; currentMatchedNumberIndex++ ){ if( matchedNumbers[ currentMatchedNumberIndex ] != -1) { System.out.println( matchedNumbers[ currentMatchedNumberIndex ] + " (Slot " + ( currentMatchedNumberIndex + 1 ) + "), " ); numberOfMatchedNumbers = numberOfMatchedNumbers + 1; } System.out.println(); if( numberOfMatchedNumbers == NUMBER_OF_LOTTERY_NUMBERS ){ System.out.println( "You're a GRAND PRIZE WINNER. You matched all numbers ans won $1 million dollars. Now go and enjoy!" ); } else if( numberOfMatchedNumbers == 1 ){ System.out.println( "You matched " + numberOfMatchedNumbers + " number." ); } else{ System.out.println( "You matched " + numberOfMatchedNumbers + " numbers." ); } } }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!