我试图让用户输入yes
,如果他们想重新输入,而no
,如果他们不想输入
我已经声明了reinput
,username
,Scanner
,array
,answer1
和answer2
用户已经为2
输入了answer1
,并假设他们将输入3
或4
或5
import java.util.Scanner; //importing Scanner to get user input
public class IndoorPets {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Declaring and initializing a Scanner
int choices[] = new int[5]; //Declaring and initializing an Array to store a sequence based on the user's input
int i = 0;
int answer1 = 0;
int answer2 = 0;
int answer3 = 0;
int answer4 = 0;
int answer5 = 0;
String username;
String reinput = "yes";
System.out.println("This program is intended to recommend to you a selection of indoor pets based on your answers to the following questions");
System.out.println("Please answer your name/nickname to begin the program");
username = input.nextLine();
System.out.println("Wow! That is a fantastic name!");
do{
System.out.println("Question 1");
System.out.println(username + ", do you want a big or small pet? Type 1 for big and 2 for small");
System.out.println();
answer1 = input.nextInt();
if (answer1 == 1 || answer1 == 2){
if (answer1 == 1){
choices[0] = 1;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
reinput = input.next();
input.nextLine();
if (answer1 == 2){
choices[0] = 2;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
} while(reinput.equals("yes"));
do{
if (answer1 == 1){
choices[1] = 3;
break;
}
if (answer1 == 2){
reinput = "yes";
System.out.println("Question 2");
System.out.println(username + ", do you want a pet that lives on land, in water or in air? Type 3 for land, 4 for water, 5 for air");
answer2 = input.nextInt();
if (answer2 == 3 || answer2 == 4 || answer2 == 5){
if (answer2 == 3){
choices[1] = 3;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer2 == 4){
choices[1] = 4;
}
if (answer2 == 5){
choices[1] = 5;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
}
} while(reinput.equals("yes"));
do{
if (answer1 == 1){
System.out.println("Question 2");
System.out.println(username + ", do you have a large living space or a tiny living space? Type 6 for large and 7 for tiny");
}
reinput = "yes";
answer3 = input.nextInt();
if (answer3 == 6 || answer3 == 7){
if (answer3 == 6){
choices[2] = 6;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer3 == 7){
choices[2] = 7;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
if (answer1 == 2){
reinput = "yes";
System.out.println("Question 3");
System.out.println(username + ", do you have a large living space or a tiny living space? Type 6 for large and 7 for tiny");
answer3 = input.nextInt();
if (answer3 == 6 || answer3 == 7){
if (answer3 == 6){
choices[2] = 6;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer3 == 7){
choices[2] = 7;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
}
} while(reinput.equals("yes"));
do{
if (answer1 == 1){
System.out.println("Question 3");
System.out.println(username + ", do you want a pet that is easy or hard to take care of? Type 8 for easy and 9 for hard");
}
reinput = "yes";
answer4 = input.nextInt();
if (answer4 == 8 || answer4 == 9){
if (answer4 == 8){
choices[3] = 8;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer4 == 9){
choices[3] = 9;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
if (answer1 == 2){
reinput = "yes";
System.out.println("Question 4");
System.out.println(username + ", do you want a pet that is easy or hard to take care of? Type 8 for easy and 9 for hard");
answer4 = input.nextInt();
if (answer4 == 8 || answer4 == 9){
if (answer4 == 8){
choices[3] = 8;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer4 == 9){
choices[3] = 9;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
}
} while(reinput.equals("yes"));
do{
if (answer1 == 1){
System.out.println("Question 4");
System.out.println(username + ", do you want a cheap or expensive pet? Type 10 for cheap and 11 for expensive");
}
reinput = "yes";
answer5 = input.nextInt();
if (answer5 == 10 || answer5 == 11){
if (answer5 == 10){
choices[4] = 10;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer5 == 11){
choices[4] = 11;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
if (answer1 == 2){
System.out.println("Question 5");
System.out.println(username + ", do you want a cheap or expensive pet? Type 10 for cheap and 11 for expensive");
answer5 = input.nextInt();
if (answer5 == 10 || answer5 == 11){
if (answer5 == 10){
choices[4] = 10;
}
System.out.println(username + ", did you make a mistake and want to choose again? Type yes or no");
input.nextLine();
reinput = input.nextLine();
if (answer5 == 11){
choices[4] = 11;
}
}else{
System.out.println(username + ", you have entered the wrong number, please read the question and try again");
}
}
} while(reinput.equals("yes"));
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 6 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Belgian Tervuren");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 6 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Tibetan Mastiff");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 6 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Green monkey");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 6 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Alaskan Malamute");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 7 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Maine Coon");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 7 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Lavender Albino Ball Python");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 7 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Vizla");
}
if (choices[0] == 1 && choices[1] == 3 && choices[2] == 7 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Ayam Cemani Chicken");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 6 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend Ants");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 6 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Pomeranian Spitz");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 6 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Skinny Pig");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 6 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Pharaoh Hound");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 7 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Guinea Pig");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 7 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a British Shorthair");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 7 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Hedgehog");
}
if (choices[0] == 2 && choices[1] == 3 && choices[2] == 7 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Peterbald");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 6 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Goldfish");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 6 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Axolotl");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 6 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Hermit Crab");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 6 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend Sea Monkeys");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 7 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Fire-Bellied Toad");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 7 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Aquatic turtle");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 7 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend Guppies");
}
if (choices[0] == 2 && choices[1] == 4 && choices[2] == 7 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Xingu River ray");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 6 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Canary");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 6 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a American Goldfinch");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 6 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Budgies");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 6 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Hyacinth Macaw");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 7 && choices[3] == 8 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Cockatiel");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 7 && choices[3] == 8 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I would recommend a Palm Cockatoo");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 7 && choices[3] == 9 && choices[4] == 10){
System.out.println("Based on your answers to the above questions, I would recommend a Peach-Faced Lovebird");
}
if (choices[0] == 2 && choices[1] == 5 && choices[2] == 7 && choices[3] == 9 && choices[4] == 11){
System.out.println("Based on your answers to the above questions, I
would recommend a Toucan");
}
}
}
我得到的错误是:
java.util.Scanner.throwFor处的java.util.InputMismatchException(未知 源)位于java.util.Scanner.next(未知源)位于 java.util.Scanner.nextInt(未知源),位于 java.util.Scanner.nextInt(未知源),位于 IndoorPets.main(IndoorPets.java:146)位于 sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)位于 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源)位于 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)位于 java.lang.reflect.Method.invoke(未知来源)
我希望能够在no
输入reinput = input.nextLine();
退出循环,但我只能输入yes