我用Java制作了一个狗kennel/pound
,它正在工作(在eclipse中)。但是当我测试它时,我得到了这个InputMismatchException
。
我执行 switch-statement
和案例的方式有问题吗?或者我的扫描仪
有什么用?该程序正在编译,所以我不明白。
作业中的要求之一是程序接受以下命令:
- 注册新狗
- 增加年龄
- 列出狗
- 移除狗
- 退出
另一个要求是测试程序必须识别单词“Error”。 请帮助我!!!
这是 Dog 类:
import java.util.*;
public class Dog {
private String dogName;
private String dogRace;
private int dogAge;
private double dogWeight;
public Dog(String name, String race, int age, double weight){
this.dogName = name;
this.dogRace = race;
this.dogAge = age;
this.dogWeight = weight;
}
public String getDogName (){
return this.dogName;
}
public String getDogRace (){
return this.dogRace;
}
public void birthday(){
dogAge = dogAge+1;
}
public int getDogAge(){
return this.dogAge;
}
public double getDogWeight(){
return this.dogWeight;
}
public double getDogTailLength(){
if (!getDogRace().equalsIgnoreCase("Tax"))
if(!getDogRace().equalsIgnoreCase("Dachshund")){
return ((dogAge * dogWeight)/ 10);
}
return 3.7;
}
public String toString(){
return "Name: " + getDogName() + ", Race: " + getDogRace() + ", Age: " + getDogAge() + ", Weight: " +
getDogWeight() + ", Tail: " + getDogTailLength();
}
}
这是主类:
import java.util.*;
public class Main {
public static Scanner scan = new Scanner(System.in);
private ArrayList <Dog> dogList = new ArrayList<Dog>();
public ArrayList<Dog> getDogList(){
return this.dogList;
}
public String dogMenu(){
return "Choose a commande below: \n1) Register new dog \n2) Increase age \n3) List dogs \n4)"
+ " Remove dog \n5) Exit ";
}
public String error(){
return "ERROR: please choose a number between 1-5";
}
public void addDog(){
System.out.println("1- Enter the dogs name: ");
String name = scan.next();
System.out.println("2- Enter the dogs race: ");
String race = scan.next();
System.out.println("3- Enter the dogs age: ");
int age = scan.nextInt();
System.out.println("4- Enter the dogs weight: ");
double weight = scan.nextDouble();
Dog newDog = new Dog(name, race, age, weight);
getDogList().add(newDog);
System.out.println(newDog);
}
public void ageDog(){
System.out.println("Which dog would you like to age? (State the name): ");
String write = scan.next();
for(Dog d: getDogList()){
if(d.getDogName().equalsIgnoreCase(write)){
d.birthday();
}else{
System.out.println("ERROR: The dog doesn't exist in the kennel");
}
}
}
public void dogTail(){
System.out.println("State the tail length: ");
double dogTailLengthIn = scan.nextDouble();
for(Dog d1: getDogList()){
if(d1.getDogTailLength()>= dogTailLengthIn){
System.out.println(d1);
}
}
if(getDogList().isEmpty()){
System.out.println("ERROR: There are no dogs!!!");
}
}
public void deleteDog(){
System.out.println("Which dog do you want to delete? (State the name): ");
String nameIn = scan.next();
boolean delete = false;
for(int a = 0; a <getDogList().size();a++){
if(getDogList().get(a).getDogName().equalsIgnoreCase(nameIn)){
getDogList().remove(a);
delete = true;
}
}
if(delete){
System.out.println("The dog " + nameIn + " is deleted from the register.");
}else{
System.out.println("The dog " + nameIn + " doesn't exist in the register.");
}
}
public void exit(){
System.out.println("Thank you. Hope to see you soon!");
scan.close();
System.exit(0);
}
public static void main(String[] args){
/* Dog betty = new Dog("pudel", "Betty", 4, 22, 1.5);
Dog jonte = new Dog("rotweiler", "Jontis", 8, 60, 2);
Dog jonas = new Dog("chihuahua", "Jonis", 2, 5, 0.3);*/
Main kennel = new Main();
while (true){
System.out.print(kennel.dogMenu());
int write = scan.nextInt();
switch(write){
case 1:
kennel.addDog();
break;
case 2:
kennel.ageDog();
break;
case 3:
kennel.dogTail();
break;
case 4:
kennel.deleteDog();
break;
case 5:
kennel.exit();
default:
System.out.println(kennel.error());
}
}
}
}
问题是这样的:
Choose a commande below:
1) Register new dog
2) Increase age
3) List dogs
4) Remove dog
5) Exit
[IN:wrong command]
java.util.concurrent.TimeoutException [LOOKING FOR: Any of [[fel case insensitive], [error case insensitive]]]
[ERR: Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:96)]
在每种情况下我都明白这一点!!
最佳答案
您收到 InputMismatchException
是因为您正在输入 String
作为输入,但 int write = scan.nextInt();
需要来自 user
的 int
值,因此存在 datatype
的 mismatch
。如果您输入选项的编号而不是字符串
,您的代码将正常工作,但您仍然需要更改输入Error
关于java - 为什么我的 Java 程序中会出现 InputMismatchException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47639198/