Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Return语句继续提示用户输入字符串_Java_Input_Return_Getter Setter - Fatal编程技术网

Java Return语句继续提示用户输入字符串

Java Return语句继续提示用户输入字符串,java,input,return,getter-setter,Java,Input,Return,Getter Setter,我的程序的目的是获取动物属性的输入,然后打印具有相应属性的动物。我使用了setter和getter方法以及其他三种方法来创建动物的正确特征,然后检查输入的属性是否与动物对应 我的问题是,如果您输入与第一只动物对应的属性,我的input()方法应该返回输入的字符串将运行一次,但如果您输入与第二只或第三只动物对应的属性,它将分别重新提示您两到三次,一旦程序找到了正确的动物,它将继续重新提示用户,直到输入。像这样: 我不知道为什么会发生这种情况,也不知道如何修复,我所需要的只是输入提示发生一次,然后将

我的程序的目的是获取动物属性的输入,然后打印具有相应属性的动物。我使用了setter和getter方法以及其他三种方法来创建动物的正确特征,然后检查输入的属性是否与动物对应

我的问题是,如果您输入与第一只动物对应的属性,我的input()方法应该返回输入的字符串将运行一次,但如果您输入与第二只或第三只动物对应的属性,它将分别重新提示您两到三次,一旦程序找到了正确的动物,它将继续重新提示用户,直到输入。像这样:

我不知道为什么会发生这种情况,也不知道如何修复,我所需要的只是输入提示发生一次,然后将正确的动物打印给用户。任何帮助都将不胜感激,谢谢。我目前的代码是:

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();
    }

}
尝试从main()调用input()方法,并将结果保存为在Pet()方法中引用的字符串。您的输入可以保存为全局变量或作为参数传递。这将避免多次要求相同的输入

另外,在调试模式下运行您的程序,并逐行检查,以确保您理解为什么它当前如此频繁地向用户请求输入。调试有助于学习和修复:)

m.PetOne()
使用输入“绿色”调用
PetTwo()
,然后使用相同的输入调用
PetTree()
。但是在所有这些运行之后,您需要调用
PetTwo()
,该输入调用
PetTwo()
。然后,您有另一个调用
PetThree()
。此外,您从不关闭
扫描仪的实例。考虑到所有这些,我不得不说你得到的输入是很正常的。你的每只宠物都会叫“下一只”宠物,它要求输入。
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();
    }

}