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

I\'m trying to make a program loop so I made a test program with lottery! I got

ID: 3920007 • Letter: I

Question

I'm trying to make a program loop so I made a test program with lottery! I got most of the coding down and I have an idea of how it'll go, but I'm having trouble on how to loop it! Can someone help me?

I want the program to output:

Welcome to the Lottery Machine!
Enter your name:
[Name inputted here]
What would you like to do?
0 = Change Name
1 = Regular Lottery
2 = Higher Chance Lottery
3 = Holiday Themed Lottery
4 = End Program
Please enter the number of choice:
[Example] 0
>Returns to “Enter your name:”

[Example] 2
>Randomizes for Regular Lottery
“You got a common ticket of #!”
“[Name] got common item 2!”
>Loops back to “What would you like to do?"

This is what I have for the code:

package test;
import java.util.Scanner;
import java.util.Random;

public class Test {
  
public static void displayOpeningMessage(){
System.out.println("Welcome to the Lottery Machine!");
}   
  
public static int chooseCategory(){
Scanner input = new Scanner(System.in);
int characterName = 0;
String name = new Integer(characterName).toString();
System.out.println("Enter your name:");
name = input.nextLine();
Scanner inputNum = new Scanner(System.in);
int userMove;
System.out.println("What would you like to do?");
System.out.println("0 = Change Name");
System.out.println("1 = Regular Lottery");
System.out.println("2 = Higher Chance Lottery");
System.out.println("3 = Holiday Themed Lottery");
System.out.println("4 = End Program");
System.out.println("Please enter the number of choice:");
if(inputNum.hasNextInt()){
userMove = inputNum.nextInt();
switch(userMove){
case 0:
return characterName;
case 1:
Random random = new Random();
int rarity = random.nextInt(1000)+1;
if(rarity <= 500){
System.out.println("You got a common ticket of " + rarity + "!");
Random randomC = new Random();
int commonItem = randomC.nextInt(2)+1;
if(commonItem == 1){
System.out.println(name + " got common item 1!");
return userMove;
}
else{
System.out.println(name + " got common item 2!");
return userMove;
}
}
else{
System.out.println("You got a rare item of " + rarity + " for " + name);
return userMove;
}

case 2:
Random randomHC = new Random();
int regMin = 901;
int regMax = 1000;
int rarityHC = randomHC.nextInt(regMax-regMin)+regMin;
if(rarityHC <= 500){
System.out.println("You got a rare holiday ticket of " + rarityHC + "!");
Random randomC = new Random();
int uRPItem = randomC.nextInt(2)+1;
if(uRPItem == 1){
System.out.println(name + " got rare Holiday item 1!");
}
else{
System.out.println(name + " got rare Holiday item 2!");
}
}
else{
System.out.println("You got a ultra rare Holiday ticket of " + rarityHC + " for " + name);
}
return userMove;
case 3:
Random randomH = new Random();
int rarityH = randomH.nextInt(1000)+1;
if(rarityH <= 500){
System.out.println("You got a common Holiday ticket of " + rarityH + "!");
Random randomC = new Random();
int holidayItem = randomC.nextInt(2)+1;
if(holidayItem == 1){
System.out.println(name + " got common Holiday 1!");
}
else{
System.out.println(name + " got common Holiday item 2!");
}
}
else{
System.out.println("You got a rare Holiday ticket of " + rarityH + " for " + name);
}
return userMove;
case 4:
System.out.println("Thank you for playing!");
break;
default:
System.out.println("Invalid user input.");
System.out.println("Please enter the number of choice:");
userMove = inputNum.nextInt();
return userMove;
}
}
return characterName;
}
  
public static void main(String[] args) {
displayOpeningMessage();
chooseCategory();
}
  
}

Explanation / Answer

package test; import java.util.Scanner; import java.util.Random; public class Test { public static void displayOpeningMessage(){ System.out.println("Welcome to the Lottery Machine!"); } public static int chooseCategory(){ Scanner input = new Scanner(System.in); int characterName = 0; String name = new Integer(characterName).toString(); System.out.println("Enter your name:"); name = input.nextLine(); Scanner inputNum = new Scanner(System.in); int userMove; System.out.println("What would you like to do?"); System.out.println("0 = Change Name"); System.out.println("1 = Regular Lottery"); System.out.println("2 = Higher Chance Lottery"); System.out.println("3 = Holiday Themed Lottery"); System.out.println("4 = End Program"); System.out.println("Please enter the number of choice:"); while (inputNum.hasNextInt()){ userMove = inputNum.nextInt(); if(userMove == 4) { break; } switch(userMove){ case 0: System.out.println("Enter your name:"); name = inputNum.nextLine(); name = inputNum.nextLine(); break; case 1: Random random = new Random(); int rarity = random.nextInt(1000)+1; if(rarity