Question: How would I make a loop in java that will ask the user to choose an option from an array and if they do not
How would I make a loop in java that will ask the user to choose an option from an array and if they do not choose one of the options to ask them to try again and to repeat the loop?
My code:
import java.util.Scanner; public class PartyTest { public static void main(String[] args) { Party party = new Party(); String[] classNames = {"theif","warrior","wizard","steve","bard"}; Hero Carmilla = new Hero("Carmilla"); Hero Alucard = new Hero("Alucard"); Hero Steve = new Hero("steve"); Hero Sypha = new Hero("sypha"); System.out.println("The avaliable classes are: " ); for(int i = 0; i < classNames.length; i++) { System.out.println(classNames[i]); } Scanner ask = new Scanner(System.in); System.out.println("Enter the class for Carmilla "); String nameC = ask.next(); Boolean temp; I was attempting to do a while loop but I was not sure what conditions to use
while(temp = true) { if(nameC.equalsIgnoreCase("theif")) { Carmilla.sethClass(nameC); temp = false;; break; } else { System.out.println("Invalid class try again"); } } System.out.println(Carmilla); Hero class just sets the values for everything, I would use (depending on the name of the person I'm calling) `Carmilla.sethClass(nameC)', which just sets the name of the chosen class to the hero class. I want to ask the user what class they would like to set for each person(they are the names stated with Hero in front of them) and if the user does not type one of the classNames values then they are told that its an invalid statement and to try again, which will then ask again what class they want for (in this example) Carmilla.
Example of the output:
The avaliable classes are:
theif warrior wizard steve bard
Enter the class for Carmilla
wizard
Enter the class for Alucard
theif
Enter the class for Steve
steve
Enter the class for Sypha
warrior
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
