/** This program allows the user to play the lottery by entering a ticket to com
ID: 3829602 • Letter: #
Question
/** This program allows the user to play the lottery
by entering a ticket to compare to the computer
generated winning lottery ticket
*/
import java.util.Scanner;
import java.util.Random;
import java.io.*;
public class LotteryTwo
{
public static void main (String[] args) throws IOException
{
int[] l= new int[5];
int[] w= new int[5];
boolean m;
l=lottery();
w=winning();
m=match(l,w);
display();
}
public static int[] lottery()
{
int[]array = new int[5];
int value;
Scanner keyboard = new Scanner(System.in);
Random randomNumbers = new Random();
//Ask the user if they would like to enter their own numbers
System.out.println("Would you like to enter your own lottery numbers? Select 1 for yes or 2 for no");
int user = keyboard.nextInt();
//validate user input
while(user<1 || user>2)
{
System.out.print("Invalid number. Please enter 1 for yes or 2 for no");
user = keyboard.nextInt();
}
//If user selects yes, allow them to enter their own numbers
if(user ==1)
{
//Get the lottery numbers from user
for(int i =0; i<array.length; i++)
{
System.out.println("Enter a number 1 through 20");
value = keyboard.nextInt();
//validate input
while(value <1 || value> 20)
{
System.out.println("Enter a number 1 through 20");
value=keyboard.nextInt();
}
if(sequentialSearch(array, value,i))
{
//assign value to array
array[i]=value;
}
else
{
System.out.println("You have already entered that number, please enter another number");
i--;
}
}
}
//if user choses no, randomly generate number for them
else if(user ==2)
{
int num;
for(int i=0; i<array.length; i++)
{
num=randomNumbers.nextInt(20)+1;
array[i] = num;
}
}
return array;
}
private static boolean sequentialSearch(int[]aray, int value, int count)
{
for(int j=0; j<count; j++)
{
if(value == aray[j]) //checks if value is found
{
return false;
}
}
return true;
}
//This method holds the array of the lottery ticket
public static int[] winning()
{
int rand;
int[] array = new int[5];
//create a Random object
Random randomNumbers = new Random();
for(int i=0; i<array.length; i++)
{
rand = randomNumbers.nextInt(20)+1;
array[i] = rand;
}
return array;
}
//This method cheks to see if they are a match
public static boolean match(int[]l, int[]w)
{
int total =0;
//create nested for loop to compare arrays
for(int i=0; i<l.length; i++)
{
for(int j =0; j<w.length; j++)
{
if(l[i] == w[j])
{
total++;
}
else
{
return false;
}
}
}
if(total ==5)
{
return true;
}
return false;
}
//this method displays the contents of the tickets
public static void display()
{
Scanner keyboard = new Scanner(System.in);
if(m)
{
System.out.println("You Won!");
}
else
{
System.out.println("you lost, try again");
}
System.out.println("Your ticket was" + (lottery(l)));
System.out.println("The winning ticket was" + (winning(w)));
}
}
This program has a few errors that I need help fixing. Also for the match method, I would like to make it that if the elements in the array do not match then I would like to generate a new ticket that the computer makes. I would also like to make a file that keeps track of how many times I have generated a new ticket. Therefore, in the display method I will not need the "you lose" part because the computer will generate a new ticket until the numbers match from each ticket. Such that is the user had 1, 5, 6, 7, 20 the computer can have 20, 7, 6, 1, 5 the numbers do not have to match in order they just have to match somehow.
Please use Java and thank you for your help!
Explanation / Answer
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class LotteryTwo {
static int[] l = new int[5];
static int[] w = new int[5];
static boolean m;
public static void main(String[] args) throws IOException {
l = lottery();
w = winning();
m = match(l, w);
display();
}
public static int[] lottery() {
int[] array = new int[5];
int value;
Scanner keyboard = new Scanner(System.in);
Random randomNumbers = new Random();
// Ask the user if they would like to enter their own numbers
System.out
.println("Would you like to enter your own lottery numbers? Select 1 for yes or 2 for no");
int user = keyboard.nextInt();
// validate user input
while (user < 1 || user > 2) {
System.out
.print("Invalid number. Please enter 1 for yes or 2 for no");
user = keyboard.nextInt();
}
// If user selects yes, allow them to enter their own numbers
if (user == 1) {
// Get the lottery numbers from user
for (int i = 0; i < array.length; i++) {
System.out.println("Enter a number 1 through 20");
value = keyboard.nextInt();
// validate input
while (value < 1 || value > 20) {
System.out.println("Enter a number 1 through 20");
value = keyboard.nextInt();
}
if (sequentialSearch(array, value, i)) {
// assign value to array
array[i] = value;
} else {
System.out
.println("You have already entered that number, please enter another number");
i--;
}
}
}
// if user choses no, randomly generate number for them
else if (user == 2) {
int num;
for (int i = 0; i < array.length; i++) {
num = randomNumbers.nextInt(20) + 1;
array[i] = num;
}
}
return array;
}
private static boolean sequentialSearch(int[] aray, int value, int count) {
for (int j = 0; j < count; j++) {
if (value == aray[j]) // checks if value is found
{
return false;
}
}
return true;
}
// This method holds the array of the lottery ticket
public static int[] winning() {
int rand;
int[] array = new int[5];
// create a Random object
Random randomNumbers = new Random();
for (int i = 0; i < array.length; i++) {
rand = randomNumbers.nextInt(20) + 1;
array[i] = rand;
}
return array;
}
// This method cheks to see if they are a match
public static boolean match(int[] l, int[] w) {
int total = 0;
// create nested for loop to compare arrays
for (int i = 0; i < l.length; i++) {
for (int j = 0; j < w.length; j++) {
if (l[i] == w[j]) {
total++;
} else {
return false;
}
}
}
if (total == 5) {
return true;
}
return false;
}
// this method displays the contents of the tickets
public static void display() {
Scanner keyboard = new Scanner(System.in);
if (m) {
System.out.println("You Won!");
} else {
System.out.println("you lost, try again");
}
System.out.println("Your ticket was" + l.toString());
System.out.println("The winning ticket was" + w);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.