Need help to add main class to this program so i can run it to work import java.
ID: 3866100 • Letter: N
Question
Need help to add main class to this program so i can run it to work
import java.util.Random;
import java.util.Scanner;
public class LuckyForLife {
int[] luckyballnumbers = new int[5];
int[] winningNumbers = new int[5];
int powerball;
int luckyball;
//Print the data to Console
public void WriteOutput() {
System.out.println("Your lucky ball numbers are ");
for (int i = 0; i < 5; i++) {
System.out.println(luckyballnumbers[i]);
}
System.out.println("The lucky ball pick is " + powerball);
System.out.println(" The winning numbers are ");
for (int k = 0; k < 5; k++)
System.out.println(winningNumbers[k]);
System.out.println(" The winning lucky ball number is " + luckyball);
}
//Taking Input from Console
public void ReadInput() {
Scanner sc = new Scanner(System.in);
int j = 1;
while (true) {
System.out.print("Please enter number " + j + " which should be greater than 0 and less than 75: ");
int n = sc.nextInt();
//if number is invalid
if (n <= 0 || n >= 75) {
if (n <= 0)
System.out.println("Number " + j + " must be greater than zero");
else
System.out.println("Number " + j + " must be less than 75");
}
//the number is already repeated
else {
boolean duplicate = false;
for (int k = 0; k < j - 1; k++)
if (n == luckyballnumbers[k]) {
duplicate = true;
break;
}
if (duplicate)
System.out.println("Number " + j + " must be different from previous numbers");
else {
//[n-1] = true;
luckyballnumbers[j - 1] = n;
j += 1;
}
}
//once we have entered five numbers
if (j == 6)
break;
}
while (true) {
System.out.println("Please enter a number that is greater than 0 and less than 18 for your lucky Powerball number");
//read a value between 0 and 18
int p_ball = sc.nextInt();
if (p_ball <= 0 || p_ball >= 18) {
if (p_ball <= 0)
System.out.println("The Powerball must be greater than zero");
else
System.out.println("The Powerball must be less than 18");
} else {
powerball = p_ball;
break;
}
}
// int [] winningNumbers = new int[5];
// int luckyball;
//Creating random variable to access the Random class
//Random number generator
System.out.println("In here ");
Random rand = new Random();
int j1 = 1, temp, k;
boolean duplicate;
while (true) {
temp = rand.nextInt(75); //Generating random numbers
if (temp <= 0) //If smaller than 0 then discard
continue;
else {
duplicate = false;
//Checking for duplicates
for (k = 0; k < j1-1; k++) {
if (temp == winningNumbers[k])
duplicate = true;
}
if (duplicate == false)
winningNumbers[j1-1] = temp;
j1 += 1;
}
//Checking for limit
if (j1 == 6)
break;
}
while (true) {
//Generating lucky ball number between 1 and 17
temp = rand.nextInt(18);
if (temp > 0) {
luckyball = temp;
break;
}
}
sc.close();
//Display
}
}
Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class LuckyForLife {
int[] luckyballnumbers = new int[5];
int[] winningNumbers = new int[5];
int powerball;
int luckyball;
// Print the data to Console
public void WriteOutput() {
System.out.println("Your lucky ball numbers are ");
for (int i = 0; i < 5; i++) {
System.out.println(luckyballnumbers[i]);
}
System.out.println("The lucky ball pick is " + powerball);
System.out.println(" The winning numbers are ");
for (int k = 0; k < 5; k++)
System.out.println(winningNumbers[k]);
System.out.println(" The winning lucky ball number is " + luckyball);
}
// Taking Input from Console
public void ReadInput() {
Scanner sc = new Scanner(System.in);
int j = 1;
while (true) {
System.out.print("Please enter number " + j
+ " which should be greater than 0 and less than 75: ");
int n = sc.nextInt();
// if number is invalid
if (n <= 0 || n >= 75) {
if (n <= 0)
System.out.println("Number " + j
+ " must be greater than zero");
else
System.out.println("Number " + j + " must be less than 75");
}
// the number is already repeated
else {
boolean duplicate = false;
for (int k = 0; k < j - 1; k++)
if (n == luckyballnumbers[k]) {
duplicate = true;
break;
}
if (duplicate)
System.out.println("Number " + j
+ " must be different from previous numbers");
else {
// [n-1] = true;
luckyballnumbers[j - 1] = n;
j += 1;
}
}
// once we have entered five numbers
if (j == 6)
break;
}
while (true) {
System.out
.println("Please enter a number that is greater than 0 and less than 18 for your lucky Powerball number");
// read a value between 0 and 18
int p_ball = sc.nextInt();
if (p_ball <= 0 || p_ball >= 18) {
if (p_ball <= 0)
System.out
.println("The Powerball must be greater than zero");
else
System.out.println("The Powerball must be less than 18");
} else {
powerball = p_ball;
break;
}
}
// int [] winningNumbers = new int[5];
// int luckyball;
// Creating random variable to access the Random class
// Random number generator
System.out.println("In here ");
Random rand = new Random();
int j1 = 1, temp, k;
boolean duplicate;
while (true) {
temp = rand.nextInt(75); // Generating random numbers
if (temp <= 0) // If smaller than 0 then discard
continue;
else {
duplicate = false;
// Checking for duplicates
for (k = 0; k < j1 - 1; k++) {
if (temp == winningNumbers[k])
duplicate = true;
}
if (duplicate == false)
winningNumbers[j1 - 1] = temp;
j1 += 1;
}
// Checking for limit
if (j1 == 6)
break;
}
while (true) {
// Generating lucky ball number between 1 and 17
temp = rand.nextInt(18);
if (temp > 0) {
luckyball = temp;
break;
}
}
sc.close();
// Display
}
}
public class LuckyForLifeTest {
/**
* @param args
*/
public static void main(String[] args) {
try {
// creating object
LuckyForLife luckyForLife = new LuckyForLife();
// calling method to read input to console
luckyForLife.ReadInput();
// calling method to read output to console
luckyForLife.WriteOutput();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
OUTPUT:
Please enter number 1 which should be greater than 0 and less than 75: 34
Please enter number 2 which should be greater than 0 and less than 75: 22
Please enter number 3 which should be greater than 0 and less than 75: 54
Please enter number 4 which should be greater than 0 and less than 75: -9
Number 4 must be greater than zero
Please enter number 4 which should be greater than 0 and less than 75: 21
Please enter number 5 which should be greater than 0 and less than 75: 54
Number 5 must be different from previous numbers
Please enter number 5 which should be greater than 0 and less than 75: 87
Number 5 must be less than 75
Please enter number 5 which should be greater than 0 and less than 75: 74
Please enter a number that is greater than 0 and less than 18 for your lucky Powerball number
4
In here
Your lucky ball numbers are
34
22
54
21
74
The lucky ball pick is 4
The winning numbers are
31
50
52
67
34
The winning lucky ball number is 13