Write a Java program to simulate a pick 3, 4 or 5 lottery drawing. A lottery gam
ID: 3762526 • Letter: W
Question
Write a Java program to simulate a pick 3, 4 or 5 lottery drawing. A lottery game of this type will draw values between 0 and 9. You should use the Math.Random() or the java.util.Random() class to generate the values in the lottery drawing. The program should prompt the user for which lottery game they want to play. The program should also ask how many times they want to play the game. The output should show the numbers picked for each game and the sum of the numbers of all games. Here is a sample run:
Which lottery game do you want to play (Enter 3 for pick-3, 4 for pick-4 or 5 for pick-5? 3 How many games would you like to play? 10
Thank you! The numbers selected were:
349
247
331
032
931
924
452
110
017
863
The sum for all numbers picked was: 107
I have the code but I want to make the code end if the user enters a wrong integer for what game they would like to play and output that the game entedered is incorrect. I think it should be a break statement but i havent been able to figure it out.
Also please let me know if my comments are correct. :)
Thank you!
import java.util.Scanner;
import java.util.Random;
public class Lottery{
public static void main(String[] args){
//Declare variables
int game;
int rounds;
int lottoNumber;
int numGenerator;
int sum = 0;
//Welcome message
System.out.println("Welcome to the Lottery Game!");
//Scanner
Scanner userInput = new Scanner(System.in);
//Lottery game selection
System.out.println("Which lottery game do you want to play (Enter 3 for pick-3, 4 for pick-4 or 5 for pick-5)? ");
game = userInput.nextInt();
//Games selection
System.out.println("How many games would you like to play? ");
rounds = userInput.nextInt();
//Output message
System.out.println("Thank you! The numbers selected were: ");
//For loop
for(int i = 0; i < rounds; i++){
lottoNumber = 0;
//Nested for loop
for(int g = 0; g < game; g++){
//Random number generator with numbers 0-9 only
numGenerator = (new Random()).nextInt(10);
lottoNumber = (lottoNumber * 10) + numGenerator;
//Output of random numbers
System.out.print(numGenerator + " ");
//Equation for finding the sum of random numbers
sum += numGenerator;
}//End of nested for loop
System.out.println();
}//End of for loop
//Output of the sum of numbers
System.out.println("The sum for all numbers picked was: " + sum);
}//End main method
}//End Lottery class
Explanation / Answer
Use this in your code to display a comment and close the program when game code entered is wrong:
//Lottery game selection
System.out.println("Which lottery game do you want to play (Enter 3 for pick-3, 4 for pick-4 or 5 for pick-5)? ");
game = userInput.nextInt();
if (game=3 || game=4 || game=5){
//leave blank here since the game code matched
}
else{
//system.exit code will close the program
System.out.println("Invalid game code entered ");
System.exit(0);
}
//Games selection
All the given comments are correct.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.