/** This program allows the user to play the lottery by entering a ticket to com
ID: 3829494 • 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 Lottery
{
public static void main (String[] args) throws IOException
{
int[] l= new int[5];
int[] w= new int[5];
l=lottery();
w=winning();
m=match(l,w);
d=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 pick their own numbers
System.out.println("Would you like to enter your own lottery numbers? Select 1 for yes or select 2 for no");
int user = keyboard.nextInt();
//validate user input
while(user<1 || user>2)
{
System.out.println("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(i<1 || i>20)
{
System.out.println("Invalid number. Please enter a value 1 through 20" + (array+1)); //page439
value=keyboard.nextInt();
}
if(sequentialSearch(array,value,i))
{
//assign value to array
arry[i] = value;
}
else
{
System.out.println("You have already enetered that number, please enter another number");
i--;
}
}
}
//if user chooses no, randomly generate numbers for them
else if(user == 2)
{
for(int i=0; i<array.length; i++)
{
num=randomNumbers.nextInt(20)+1;
array[i] = randomNumbers;
}
}
return array;
}
private static boolean sequentialSearch(int[] aray, int value, int count)
{
for(intj=0; j<count; j++)
{
if(value == aray[j])//checks if value found
{
return false;
}
}
return true; //if not found
}
}
//This method holds the array of the lottery ticket
public static int[] winning()
{
int[] array = new int[5];
//create a Random object
Random randomNumbers = new Random();
for(int i=0; i<array.length; i++)
{
randomNumbers.nextInt(20)+1;
array[i] = randomNumbers;
}
return array;
}
//This method checks the lottery array
//and the winning array to see if they
//are a match
public static int[] match(int[]l, int[] w)
{
//create a nested for loop to compare arrays
for(int i=0; i<l.length; i++)
{
//set accumulator
total=0;
for(int j=0; j<w.length; j++)
{
if( l[i] == w[j])
{
total++;
}
else
{
w[j]=winning();
}
}
}
if(total == 5)
{
return l;
}
}
//This method displays the contents of the tickets
public static int[] display()
Scanner keyboard = new Scanner(System.in);
System.out.println("You Won!")
System.out.println("Your ticket was " + l);
System.out.println("The winning ticket was "+ w);
//create a file
File file = new File ("lottery.txt");
//read the repeated numbers from the file
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext())
{
String lotteryNumbers = inputFile.nextLine();
System.out.println(lotteryNumbers);
}
//create empty array to store the values that get repeated
}
I am getting many error messages when I try to compile this code and I do not know how to fix them. Please use java to make corrections.
Explanation / Answer
modified/debugged code
import java.util.Scanner;
import java.util.Random;
import java.io.*;
import java.util.Arrays;
public class Lottery
{
static int[] l= new int[5];
static int[] w= new int[5];
static boolean m ;
public static void main (String[] args) throws IOException
{
Lottery lobject = new Lottery();
l=Lottery.lottery();
w=Lottery.winning();
m=Lottery.match(l,w);
Lottery.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 pick their own numbers
System.out.println("Would you like to enter your own lottery numbers? Select 1 for yes or select 2 for no");
int user = keyboard.nextInt();
//validate user input
while(user<1 || user>2)
{
System.out.println("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("Invalid number. Please enter a value 1 through 20"); //page439
value=keyboard.nextInt();
}
if(sequentialSearch(array,value,i))
{
//assign value to array
array[i] = value;
}
else
{
System.out.println("You have already enetered that number, please enter another number");
i--;
}
}
}
//if user chooses no, randomly generate numbers 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 found
{
return false;
}
}
return true; //if not found
}
//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 checks the lottery array
//and the winning array to see if they
//are a match
public static boolean match(int[]l, int[] w)
{
int total = 0;
//create a nested for loop to compare arrays
for(int i=0; i<l.length; i++)
{
//set accumulator
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 " + Arrays.toString(l));
System.out.println("The winning ticket was "+ Arrays.toString(w));
//create a file
try{
File file = new File ("lottery.txt");
//read the repeated numbers from the file
Scanner inputFile = new Scanner(file);
while(inputFile.hasNext())
{
String lotteryNumbers = inputFile.nextLine();
System.out.println(lotteryNumbers);
}
}catch(FileNotFoundException e){
}
}
}
output:
run:
Would you like to enter your own lottery numbers? Select 1 for yes or select 2 for no
1
Enter a number 1 through 20
5
Enter a number 1 through 20
4
Enter a number 1 through 20
3
Enter a number 1 through 20
2
Enter a number 1 through 20
1
You Lost! try again!
Your ticket was [5, 4, 3, 2, 1]
The winning ticket was [6, 17, 15, 16, 4]
BUILD SUCCESSFUL (total time: 7 seconds)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.