【问题标题】:Why is the String color's name that is outputted to the user does not match the color name that is in the array?为什么输出给用户的字符串颜色名称与数组中的颜色名称不匹配?
【发布时间】:2020-04-04 10:28:05
【问题描述】:

我正在尝试编写一个递归算法,该算法允许用户根据用户输入程序的数字来选择颜色。颜色的名称存储在 String[] 数组中。我无法弄清楚为什么输出给用户的颜色与数组中的颜色与与用户输入的数字匹配的索引不匹配。这是我的代码:

/*
The class allows the user to search through a list a colors to pick a new
favorite color
*/
class PickAColor
{
    //Global Variables
   public int maxSize;
    public int numOfColors;
    static public String[] colors;

    //Scanner
    static Scanner myScan = new Scanner(System.in);

    //Constructor
    public PickAColor()
    {

    }

    public PickAColor(int maxSize)
    {
        this.maxSize = maxSize;
        this.numOfColors = 0;
        colors = new String[maxSize];
        populateColors();
    }

    //Functions
    //Function to populate colors
    public void populateColors()
    {
        //Add colors to array
        push(0, "red");
        push(1, "orange");
        push(2, "yellow");
        push(3, "green");
        push(4, "blue");
        push(5, "purple");
        push(6, "violet");
        push(7, "teal");
        push(8, "magenta");
        push(9, "brown");
        push(10, "black");
        push(11, "white");
        push(12, "periwinkle");
    }

    //Function to push new color
    public String push(int cntr, String color)
    {
       colors[cntr] = color;

       return colors[cntr];
    }

    //Function to see if stack is empty
    public boolean isEmpty()
    {
        return numOfColors == 0;
    }

    //Function to see if stack is full
    public boolean isFull()
    {
        return numOfColors == maxSize;
    }

    //Function to loop through array
    public static String Search(int num)
    {
        //Variables
        int cntr = 0;
        String color = null;

        //While loop iterates through array based on user's input
        while(num > cntr)
        {
            color = colors[cntr];
            cntr++;
            num--;
            Search(num);
        }
        return color;
    }

    //Function to randomly organize an array
    //Function to ask user for favorite color
    public static void askUser()
    {
        //Variables
        int response;
        String color;

        //Message user
        System.out.println("Do you like your favorite color?"
                + " \nIs it still your favorite color today?"
                + " \nHow about you spin the wheel of colors to randomly"
                + " pick a new favorite color?"
                + " \nEnter a number. ");

        response = Integer.parseInt(myScan.nextLine());

        System.out.println("...\nGood luck! ");

        color = Search(response);

       //Message
        System.out.println("Your new favorite color is: " + color);
    }

    //Function to ask if user wants to repeat this
    public static void repeat()
    {
        //Variables
        String response;
        String color;

        int slots;

        System.out.println("Would you like to try again? (y/n) ");

        response = ScottN_RecursionExcerise.myScanner.nextLine();

        switch(response)
        {
            case "y":
            {
                System.out.println("Enter a number. ");

                slots = Integer.parseInt(myScan.nextLine());

                color = Search(slots);

                System.out.println("Your new favorite color is: " + color);

                System.out.println();
                repeat();

            }
            case "n":
            {
                System.out.println();
                menu();
            }
            default:
            {
                ScottN_RecursionExcerise.menuRedirect();
            }
        }

    }
}

我创建这个类的一个新实例,并在主类的菜单中开始算法。

//Create a new color instance
                PickAColor pc = new PickAColor(13);

                //Get user's resposne
                pc.askUser();

                System.out.println();

                pc.repeat();

                menu();

你知道我做错了什么吗?

【问题讨论】:

    标签: java arrays recursion search


    【解决方案1】:

    关于这段代码可以说很多,但你的具体问题的答案是 Search 方法应该是这样的:

    public static String Search(int num)
    {
        // Basic validation of 'num' omitted...
        return colors[num];
    }
    

    您不需要循环数组,因为您知道要查找的颜色位于数组中的索引 num 处。去拿吧:)

    【讨论】:

      猜你喜欢
      • 2019-07-28
      • 2012-10-13
      • 2018-11-14
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      • 2016-04-10
      相关资源
      最近更新 更多