Question: Hi, this is done in Javascript. I have an issue with my code. I will post the Lucky for Life code, and the demo. When

Hi, this is done in Javascript.

I have an issue with my code. I will post the "Lucky for Life" code, and the demo.

When this code executes, if the number entered is above 18 or under 0, there is supposed to be an error message that says "please enter a number that is greater than 0, and less than 18."

However, when I run this code and I enter a number that is above 18, it just restarts the entire program and gives no error message.

Lucky for Life code:

import java.util.*; class LuckyForLife { int[] winningNumbers = new int[5]; int powerball; //Print the data to Console public void WriteOutput() { System.out.println("Your powerball numbers are "); for (int i = 0; i<5; i++) { System.out.println(winningNumbers[i]+" "); } System.out.println("and the Powerball number is " + powerball); }

//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 49: "); int n = sc.nextInt(); //if number is invalid if (n<=0 || n>=49) { if (n <= 0) System.out.println("Number " + j + " must be greater than zero"); else System.out.println("Number " + j + " must be less than 49"); } //the number is already repeated else { boolean duplicate = false; for(int k = 0; k < j-1; k++) if(n == winningNumbers[k]) { duplicate = true; break; } if(duplicate) System.out.println("Number " + j + " must be different from previous numbers"); else { //l[n-1] = true; winningNumbers[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 rand variable to access Random class. //rand.nextInt(49) will generate random no. from 0 to 48. Random rand = new Random(); int j1 = 0, temp, k; boolean duplicate; while (true){ temp = rand.nextInt(49); //Generating random nos. if (temp <= 0) //If smaller than equal to 0 then discard. continue; else { duplicate = false; //Checking for duplicates. for (k = 0; k < j1; k++) { if (temp == winningNumbers[k]) duplicate = true; } if (duplicate == false) winningNumbers[j1++] = temp; } //Checking for limit. if (j1 == 5) break; } while (true) { //Generating lucky ball number between 1 and 17. temp = rand.nextInt(18); if (temp > 0) { luckyball = temp; break; } } LuckyForLife powerball = new LuckyForLife(); powerball.ReadInput(); powerball.WriteOutput(); //Display. System.out.println(" The winning numbers are: "); for (k = 0; k < 5; k++) System.out.println(winningNumbers[k]); System.out.println(" The winning lucky ball number is " + luckyball); } } }

Lucky for life Demo:

public class LuckyForLifeDemo { public static void main(String[] args) { // TODO Auto-generated method stub LuckyForLife powerball = new LuckyForLife(); powerball.ReadInput(); powerball.WriteOutput(); } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!