Reputation: 122
Quick overview:
Im doing a basic program that asks the user how many doors they want their "dream car" to have.
public static void main(String [ ] args){
CarSim carSimUser1 = new CarSim();
System.out.println("Hello, and welcome to Car Matcher!");
Scanner scanName = new Scanner(System.in); // user input for name
System.out.println("What's your name?");
String userName = scanName.nextLine();
System.out.println("Hello " + userName);
System.out.println("So you are looking for a new Car?");
System.out.println("Would you like 4 door sedan");
System.out.println("Or a two door coupe?");
System.out.println("Enter a number only!");
Scanner scanDoor = new Scanner(System.in); // user input for number of doors
int numbOfDoor = scanDoor.nextInt();
while(numbOfDoor <= 4){
if(numbOfDoor == 4){
System.out.println("Okay, so you want " + numbOfDoor);
}
else if(numbOfDoor == 2){
System.out.println("Okay, so you want " + numbOfDoor);
break;
}
else{
System.out.println("Select either 2 or 4 doors!");
break;
}
enter code here
When I prompt the user how many doors they want their car to have, they only have two choices, to enter the number 2 or 4. But if they enter any other number they cannot go back and enter either 2 or 4. How can I get my program to allow the user to enter the correct number if their input wasnt 2 or 4 in the first place?
Sorry if vague, i dont have a text book because I am not a CS student currently, learning on my own.
Upvotes: 1
Views: 54
Reputation: 1
A possible solution to the question can as follows. It is not the most elegant solution but it is nearly word for word from your code. A good trick to learn from this example would be to use while loops around your inputs so that you can increase the robustness of your code.
int numbOfDoor = scanDoor.nextInt();
while (numbOfDoor != 2 || numbOfDoor != 4)
{
System.out.print("Invalid choice, try again: ");
numbOfDoor = scanDoor.nextInt();
if (numbOfDoor == 2 || numbOfDoor == 4)
break;
}
if(numbOfDoor == 4)
{
System.out.println("Okay, so you want " + numbOfDoor);
}
else if(numbOfDoor == 2)
{
System.out.println("Okay, so you want " + numbOfDoor);
}
else
{
System.out.println("Select either 2 or 4 doors!");
}
scanDoor.close()
Upvotes: 0
Reputation: 31300
This is one possibility. Note that numbOfDoor is set to 0, the test checking that it isn't one of the acceptable values, and that the nextInt is inside the loop. It's not necessary to have break statements.
int numbOfDoor = 0;
while(numbOfDoor != 2 && numbOfDoor != 4){
numbOfDoor = scanDoor.nextInt();
if(numbOfDoor == 4){
System.out.println("Okay, so you want " + numbOfDoor);
} else if(numbOfDoor == 2){
System.out.println("Okay, so you want " + numbOfDoor);
} else{
System.out.println("Select either 2 or 4 doors!");
}
}
You might also use
int numbOfDoor = 0;
while( true ){
numbOfDoor = scanDoor.nextInt();
if(numbOfDoor == 4){
System.out.println("Okay, so you want " + numbOfDoor);
break;
}
// More if + println + break
}
Here you need break statements.
Upvotes: 1