【发布时间】:2020-09-29 20:28:00
【问题描述】:
我的程序的目的是输入动物的属性,然后打印具有相应属性的动物。我使用了 setter 和 getter 方法以及其他三种方法来创建动物的正确特征,然后检查输入的属性是否与动物对应。
我的问题是我的 input() 方法应该返回输入的字符串,如果你输入一个对应于第一个动物的属性,但如果你输入一个对应于第二个或第三个动物它会分别重新提示您两次或三次,一旦程序确实找到了正确的动物,它将继续重新提示用户直到输入。像这样:
Enter one attribute you'd like in your pet: green
Enter one attribute you'd like in your pet: green
Enter one attribute you'd like in your pet: green
You can have a Parrot
Enter one attribute you'd like in your pet: green
Enter one attribute you'd like in your pet: green
You can have a Parrot
Enter one attribute you'd like in your pet: green
You can have a Parrot
Process finished with exit code 0
我不确定为什么会发生这种情况或如何解决,我只需要输入提示发生一次,然后将正确的动物打印给用户。任何帮助将不胜感激,谢谢。我目前的代码是这样的:
import java.util.*;
class Main {
private static String name;
private static String colour;
private static String personality;
private static String noise;
public String input(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter one attribute you'd like in your pet: ");
String input = sc.next();
return input;
}
public void setName(String name) {
this.name=name;
}
public void setColour(String colour) {
this.colour = colour;
}
public void setPersonality(String personality) {
this.personality = personality;
}
public void setNoise(String noise) {
this.noise = noise;
}
public String getName(){
return this.name;
}
public String getColour(){
return this.colour;
}
public String getPersonality(){
return this.personality;
}
public String getNoise(){
return this.noise;
}
public void PetOne(){
Main petOne = new Main();
String input = input();
petOne.setName("pug");
petOne.setColour("tan");
petOne.setPersonality("playful");
petOne.setNoise("woof");
if(input.equals(petOne.getName()) || input.equals(petOne.getColour()) || input.equals(petOne.getPersonality()) || input.equals(petOne.getNoise())){
System.out.println("You can have a Pug");
} else PetTwo();
}
public void PetTwo() {
Main petTwo = new Main();
String input = input();
petTwo.setName("cat");
petTwo.setColour("brown");
petTwo.setPersonality("affectionate ");
petTwo.setNoise("meow");
if(input.equals(petTwo.getName()) || input.equals(petTwo.getColour()) || input.equals(petTwo.getPersonality()) || input.equals(petTwo.getNoise())){
System.out.println("You can have a Cat");
} else PetThree();
}
public void PetThree() {
Main petThree = new Main();
String input = input();
petThree.setName("parrot");
petThree.setColour("green");
petThree.setPersonality("intelligent");
petThree.setNoise("squawk");
if(input.equals(petThree.getName()) || input.equals(petThree.getColour()) || input.equals(petThree.getPersonality()) || input.equals(petThree.getNoise())){
System.out.println("You can have a Parrot");
} else throw new RuntimeException
("Attempt to use real attributes n/0");
}
public static void main(String[] args) {
Main m = new Main();
m.PetOne();
m.PetTwo();
m.PetThree();
}
}
【问题讨论】:
-
m.PetOne()输入“green”调用PetTwo(),而后者又使用相同的输入调用PetTree()。但是在所有这些运行之后,您会调用PetTwo(),使用该输入调用PetThree()。然后你又打电话给PetThree()。此外,您永远不会关闭Scanner的实例。考虑到这一切,我不得不说你得到的输入是很正常的。 -
您的每只宠物都调用“下一个”宠物,它要求输入。
标签: java input return getter-setter