Write a program that emulates the popular lottery game \"Powerball\". First ask
ID: 3863532 • Letter: W
Question
Write a program that emulates the popular lottery game "Powerball". First ask the user how many times he wants to play. Then ask the user for 5 numbers between 1 and 69, and one additional "Power Ball" between 1 and 26. Then simulate the drawing as many times as indicated initially by creating random numbers in the given ranges, and comparing them to the numbers the user put in. After each drawing, let the user know if he won, and how many numbers he has guessed correctly. Output each drawing's result in the console.Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
import java.util.Random;
public class Powerball {
public static void main (String[] args)
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter number of times you want to play : ");
int numberOfTimes = scanner.nextInt();
for(int i=0;i<numberOfTimes;i++)
{
int numbers[] = new int[5]; //used to store selected numbers
int powerBall; //selected power ball number
System.out.print("Enter 5 distinct numbers between 1 and 69 ");
for(int j=0;j<5;j++)
{
numbers[j] = scanner.nextInt();
}
System.out.print("Enter power ball number between 1 and 26 ");
powerBall = scanner.nextInt();
int randomNumbers[] = new int[5]; // randomly generated lottery numbers
int randomPowerBall; // randomly generated lottery power ball number
System.out.print("White balls randomly selected : ");
for(int j=0;j<5;j++)
{
boolean distinct = false;
int t = 0;
while(!distinct) // numbers need to be distinct in powerball lottery
{
distinct = true;
t = 1 + (int)(Math.random() * ((69 - 1) + 1)); // random number b/w 1 and 69
for(int k=0;k<j;k++) if(randomNumbers[k]==t) distinct = false;
}
randomNumbers[j] = t;
System.out.print(t+" ");
}
System.out.println();
randomPowerBall = 1 + (int)(Math.random() * ((26 - 1) + 1));
System.out.println("PowerBall Number : "+randomPowerBall);
int count = 0;
for(int j=0;j<5;j++) // compare correctly guessed
for(int k=0;k<5;k++)
{
if(numbers[j]==randomNumbers[k]) count++;
}
if(powerBall==randomPowerBall) count++;
System.out.println("You guessed "+count+" balls correctly");
if(count==6) System.out.println("You Won");
else System.out.println("You Lost");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.