Lottery

java

School

Tacoma Community College *

*We aren’t endorsed by this school

Course

142

Subject

Computer Science

Date

Nov 24, 2024

Type

java

Pages

4

Uploaded by HeavyA

Report
/** * Lottery Program * This program is to simulate a lottery * game. First, the user is prompted to enter * how many guesses they want to take. Next, * the user is prompted to enter the list of * guessed numbers. Third, the computer picks * their numbers to guess. Fourth, the grid * replaces zeroes in the number grid. Fifth, * the number of correct guesses is * printed. Finally, the user is prompted to * be asked to play again. * * @author Tanmayi * @version 5/28/2023 */ // Import the scanner and random libraries import java.util.Scanner; import java.util.Random; // Beginning of the Lottery class public class Lottery { // Beginning of the main method public static void main(String[] args) { // Declares and sets the scanner variable Scanner in = new Scanner(System.in); // Declares and sets the random value Random randVal = new Random(); // Declare and sets the 2D array in a grid int[][] numGridAry = new int[8][10]; // Declares and sets the number of guesses int nOfGuesses = 0; // Declares and sets a user guessed number int usrGuessNum=0; // Declares and sets the play again response String playAgResp = "y"; // Beginning of the do loop to run the lotter program while(playAgResp.equals("y")) { // Prints out the lottery rules System.out.println("Welcome to the lottery game!"); System.out.println("You need to guess between 3 and 15 numbers."); System.out.println("The program will generate 20 random numbers between 1-80."); System.out.println("If your numbers match any of the random numbers, you win!"); System.out.println(); // Prints out the grid of numbers 1 to 80 System.out.println("Lottery number grid:"); // Beginning of the outer loop to print the number grid for (int i = 0; i < 8; i++) { // Beginning of the inner loop to print the number grid for (int j = 0; j < 10; j++) { numGridAry[i][j] = i * 10 + j + 1; System.out.printf("%2d ", numGridAry[i][j]); } // Ending of the inner loop to print the number grid
System.out.println(); } // Ending of the outer loop to print the number grid System.out.println(); // Beginning of the while loop foe number of guesses while (nOfGuesses < 3 || nOfGuesses > 15) { // Prompts user to ask for number of guesses System.out.print("Enter the number of guesses you wish " + "would you like to enter(between 3 and 15): "); nOfGuesses = in.nextInt(); if (nOfGuesses < 3 || nOfGuesses > 15) { // Prints a message stating invalid entry System.out.print("The number of guesses entered is " + "not between 3 and 15. Please re-enter.\n"); } } // Ending of the while loop foe number of guesses // Declares and sets the user guesses array int[] userGuessAry = new int[nOfGuesses]; // Prompts user to entered the guess numbers // Brginning of the for loop to build the guessed numbers array for (int i = 0; i < nOfGuesses; i++) { // Beginning of the Do-While to check if number is note // between 1 and 80 do { // Prompts user to enter a number between 1 nad 80 System.out.print("Enter a number between 1 and 80 " + "for the lottery: "); usrGuessNum = in.nextInt(); if(usrGuessNum<1 || usrGuessNum>80) { // Prints a message stating invalid entry System.out.print("The guess number is not between" + " 1 and 80. Please re-enter.\n"); } } while (usrGuessNum<1 || usrGuessNum>80); // Ending of the Do-While to check if number is not between 1 and 80 // Sets each user guessed number into the array userGuessAry[i] = usrGuessNum; } // Ending of the for loop to build the guessed numbers array // Declares and sets a computer randomly number picks array int[] randNumValsAry = new int[20]; // Brginning of the for loop to build the guessed numbers array for (int i = 0; i < 20; i++) { // Add the computer generated random number to the random numbers array randNumValsAry[i] = randVal.nextInt(80) + 1; } // Ending of the for loop to build the guessed numbers array // Beginning of the for outer loop to replace 0s in the number grid for (int i = 0; i < 8; i++) { // Beginning of the inner loop to replace 0s in the number grid for (int j = 0; j < 10; j++) {
int num = numGridAry[i][j]; // Beginning of the for loop to replace 0s to selected numbers for (int k = 0; k < 20; k++) { // Checks if the number is in compter randomly selected // numbers if (num == randNumValsAry[k]) { // Replaces the number grid with 0s in the array numGridAry[i][j] = 0; break; } } // Ending of the for loop to replace 0s to selected numbers // Prints out to the number grid array elements System.out.printf("%2d ", numGridAry[i][j]); } System.out.println(); } // Ending of the outer loop to replace 0s in the number grid // Compare the user's guesses with the random numbers int moOfMatch = 0; // Beginning of the for outer loop to count how many numbers and // guesses matched for (int i = 0; i < nOfGuesses; i++) { // Beginning of the for inner loop to count how many numbers and // guesses matched for (int j = 0; j < 20; j++) { // Checks if user and computer generated arrays match if (userGuessAry[i] == randNumValsAry[j]) { // Adds to number of matches moOfMatch++; break; } } // Ending of the for inner loop to count how many numbers and // guesses matched } // Ending of the for outer loop to count how many numbers and // guesses matched // Print out the results for the lottery game System.out.printf("\nYou guessed %d numbers correctly out of %d.\n", moOfMatch, nOfGuesses); // Beginning of the do-while loop to prompt the user to play again System.out.print("Enter y if you want to play again or n for " + "not to play again (y/n): "); playAgResp = in.next().toLowerCase(); while (!(playAgResp.equals("y") || playAgResp.equals("n"))){ // Prompts the user if they wish to play again System.out.print("Enter y if you want to play again or n " + "for not to play again (y/n): "); playAgResp = in.next().toLowerCase(); // Checks if the play again response is not yes or no if (!(playAgResp.equals("y") || playAgResp.equals("n"))) { // Prints a message stating invalid entry System.out.print("Invalid Entry in playing again. Please re- enter.\n"); } if (playAgResp.equals("n")) {
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
// Comes out of the while loop to play again break; } } System.out.println(); // Ending of the while loop to prompt the user to play again } // Prints a ending message System.out.println("Thanks for playing!"); } // Ending of the main method } // Ending of the Lottery class