In: Computer Science
rite a Java Program to play your version of the Texas Pick 3 Lottery. You will need to include the provided code given below in your program.
Task:The program will prompt for a number between 0 and 999. Your program will display the number of attempts it took for the entered number to match a randomly generated number.
Enter your Pick 3 choice 0-999: 554
PlayCounter = 1343 ( This result will vary )
Include the code below in your program. The code also requires the following import line:
import java.util.concurrent.ThreadLocalRandom;
/**
* officialPlayPick3()
* Returns a random number from 0 to 999
* @return int
*/
public static int officialPlayPick3() {
final int MIN_RANGE_VALUE = 0;
final int MAX_RANGE_VALUE = 999;
return
ThreadLocalRandom.current().nextInt(MIN_RANGE_VALUE,MAX_RANGE_VALUE
+ 1);
}
Also Create a loop in the main method to allow you to play as often as you wish.
Texas_Pick_3_Lottery.java
import java.util.Scanner;
import java.util.concurrent.ThreadLocalRandom;
class Texas_Pick_3_Lottery {
public static int officialPlayPick3() {
final int MIN_RANGE_VALUE = 0;
final int MAX_RANGE_VALUE = 999;
return ThreadLocalRandom.current().nextInt(MIN_RANGE_VALUE,MAX_RANGE_VALUE + 1);
}
public static void main(String[] args) {
int myPick3choice;
System.out.print("Enter your Pick 3 choice:0-999 : ");
Scanner sc = new Scanner(System.in);
myPick3choice = sc.nextInt();
playPick3(myPick3choice);
}
private static void playPick3(int myPick3) {
int playCounter = 0;
int myPick3Result;
while (true){
myPick3Result = officialPlayPick3();
playCounter ++;
if(myPick3==myPick3Result){
System.out.print("PlayCounter = " + playCounter);
break;
}
}
}
}
Output :
Explanation :
Class Texas_Pick_3_Lottery :
main() :
int officialPlayPick3() :
void playPick3(int myPick3) :