Return语句继续提示用户输入String

问题描述

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

我的问题是,如果您输入与第一个动物对应的属性,但是如果您输入与对应于第一个动物的属性,则本应返回输入的String的 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();
    }

}

解决方法

尝试从main()调用input()方法,然后将结果另存为Pet()方法中引用的String。您的输入可以另存为全局变量或作为参数传递。这样可以避免多次要求相同的输入。

还要在调试模式下运行程序,并逐行进行检查,以确保您了解为什么当前如此频繁地向用户请求输入。调试不仅可以学习,而且可以修复:)