Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

i am in need of creating a way to generate a new winning ticket after the compar

ID: 3830216 • Letter: I

Question

i am in need of creating a way to generate a new winning ticket after the comparison determines that the arrays are not a match. I also want to accumulate the number of times I have generated a new file and write it to a file. please use java to update existing code thank you!

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class LotteryGameTwo
{
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;

if (SequentialSearch(array, num, i))
{
// assign value to array
array[i] = num;
}
else
{
num = randomNumbers.nextInt(20) + 1;
array[i] = num;
i--;
}
}

}

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;
}

private static boolean Sequentialsearch(int[]aray, int num, int count)
{
for (int j = 0; j < count; j++)
{
if (num == 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;

if (SequentialSearch(array, rand, i))
{
// assign value to array
array[i] = rand;
}
else
{
rand = randomNumbers.nextInt(20) + 1;
array[i] = rand;
i--;
}
}
return array;
}

private static boolean SequentialSearch(int[] aray, int rand, int count)
{
for (int j = 0; j < count; j++)
{
if (rand == aray[j]) // checks if value is found
{
return false;
}
}
return true;
}


// This method cheks to see if they are a match
public static boolean match(int[] l, int[] w)
{
int total = 0;
int generate =0;
boolean matchFound=false;
// create nested for loop to compare arrays
for ( i = 0; i < l.length; i++)
{
for ( j = 0; j < w.length; j++)
{
if (l[i] == w[j])
{
total++;
}
else
{
return false;   
  
}

}
}
if (total == 5) {
return true;
}
//generate a new ticket
while(!matchFound)
{
if(l[i]!=w[j])
{
w[j]=winning();
generate++;

}
}
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!");
}

System.out.println("Your ticket was " + Arrays.toString(l));
System.out.println("The winning ticket was" + Arrays.toString(w));
}
}

Explanation / Answer

NOTE : you can do in this way, whenever the match is not found, generate a winning ticket again...in else case which means match is not found

package philosopher;

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class LotteryGameTwo
{
static int[] l = new int[5];
static int[] w = new int[5];
static boolean m;
static int generate =0;

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;

if (SequentialSearch(array, num, i))
{
// assign value to array
array[i] = num;
}
else
{
num = randomNumbers.nextInt(20) + 1;
array[i] = num;
i--;
}
}
}

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;
}

private static boolean Sequentialsearch(int[]aray, int num, int count)
{
for (int j = 0; j < count; j++)
{
if (num == 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;

if (SequentialSearch(array, rand, i))
{
// assign value to array
array[i] = rand;
}
else
{
rand = randomNumbers.nextInt(20) + 1;
array[i] = rand;
i--;
}
}
return array;
}

private static boolean SequentialSearch(int[] aray, int rand, int count)
{
for (int j = 0; j < count; j++)
{
if (rand == aray[j]) // checks if value is found
{
return false;
}
}
return true;
}

// This method cheks to see if they are a match
public static boolean match(int[] l, int[] w)
{
int total = 0;
boolean matchFound=false;
// 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 {

                  w = winning();
                   generate++;

                   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!");
}

System.out.println("Your ticket was " + Arrays.toString(l));
System.out.println("The winning ticket was" + Arrays.toString(w));
System.out.println("the number of times I have generated a new file and write it to a file: "+generate);
}
}