I have a lottery program that compares two arrays of five elements (the numbers
ID: 3828403 • Letter: I
Question
I have a lottery program that compares two arrays of five elements (the numbers can only go up to 20). First I want to compare the two arrays to see if they have any matching numbers. If they do not match I want to generate a whole new ticket. Then I want to create a file that stores all the numbers and how many times they are generated before the winning ticket is generated. For instance I would want "the number 1 was generated 50 times" or something like that.
I am trying to compare them with for loops, is that a good approach?
please use java
Explanation / Answer
Hi,
Please see below the class and output file.
Please comment any queries/feedbacks
Thanks,
Lottery.java
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class Lottery {
public static void main(String [] args){
int [] intArray1 = new int[5];
int [] intArray2 = new int[5];
int max =20;
int min=1;
int randomNum=0;
boolean matchFound =false;
Random rn = new Random();
int totalCount = 0;
String content ="";
//Populating first array with randomnumbers
for(int i=0;i<intArray1.length;i++){
randomNum = rn.nextInt((max - min) + 1) + min;
intArray1[i] = randomNum;
}
//Populating Second array with randomnumbers
for(int i=0;i<intArray2.length;i++){
randomNum = rn.nextInt((max - min) + 1) + min;
intArray2[i] = randomNum;
}
System.out.println("Elements in Array Are:");
printAray(intArray1);
System.out.println();
printAray(intArray2);
//Comparing each elemnt in 2 ararys{
for(int k=0;k<intArray1.length;k++){
totalCount =0;
matchFound =false;
while(!matchFound){
if(intArray1[k]!= intArray2[k]){
intArray2[k]= createNewTicket();
totalCount = totalCount+1;
}
else{
content = content+" the number "+intArray2[k]+" was generated "+totalCount+" times";
matchFound =true;
}
}
}
System.out.println("Winning Ticket is :");
printAray(intArray2);
System.out.println(content);
writeToFile(content); //Writing to File
}
public static int createNewTicket(){
int [] intArrayNew= new int[5];
int max =20;
int min=1;
int randomNum=0;
Random rn = new Random();
//Populating Second array with randomnumbers
for(int i=0;i<intArrayNew.length;i++){
randomNum = rn.nextInt((max - min) + 1) + min;
}
return randomNum;
}
public static void printAray(int[] intarray){
for(int i=0;i<intarray.length;i++){
System.out.print(intarray[i]+" ");
}
}
/**
* To write the output to a file
* @param args
*/
public static void writeToFile(String content) {
BufferedWriter bw = null;
FileWriter fw = null;
try {
fw = new FileWriter("output.txt");
bw = new BufferedWriter(fw);
bw.write(content);
bw.flush();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
output.txt
the number 12 was generated 24 times
the number 1 was generated 41 times
the number 10 was generated 12 times
the number 9 was generated 23 times
the number 15 was generated 29 times
Sample output:
Elements in Array Are:
12 1 10 9 15
7 17 7 12 17 Winning Ticket is :
12 1 10 9 15
the number 12 was generated 24 times
the number 1 was generated 41 times
the number 10 was generated 12 times
the number 9 was generated 23 times
the number 15 was generated 29 times
Done
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.