【问题标题】:Bubble Search guided by user input?由用户输入引导的冒泡搜索?
【发布时间】:2015-12-01 15:42:13
【问题描述】:

我正在编写一个程序来帮助用户创建 Top X 或热门收藏列表。

用户将首先输入列表中的项目数,然后用于创建相同大小的数组。然后用户填充数组。最后,用户会被问到一系列问题,程序一次将列表中的每个项目与列表中的另一个项目进行比较,并在两者之间询问您更喜欢哪一个。根据他们的选择,他们的分数保存在另一个存储整数值的数组中。然后按降序排序。

当我运行程序时,它不会将所有元素与每个元素进行比较。

例如,假设我有一个长度为三个元素的数组,其中项目是“香草”、“巧克力”和“草莓”。它会要求我将香草与巧克力进行比较,然后将香草与草莓进行比较。但是,它不会将 Chocolate 与 Strawberry 进行比较,然后打印结果。我想知道我遇到了什么逻辑错误。我从来没有使用气泡搜索字符串。

这是我的参考代码:

 import java.util.Scanner;

public class FavoriteListMaker {

public static void main(String[] args)
{

    Scanner inputDevice = new Scanner(System.in);
    System.out.println("This program is to help you order a favorites list. Please enter the amount of items on the list.");
    int ListSize = inputDevice.nextInt();
    inputDevice.nextLine();
    String [] TopXA = new String [ListSize];
    int [] TopXB = new int[ListSize];

    for (int x = 0; x < TopXA.length; ++ x)
    {
        System.out.println("Please enter an item to be organized on the list");
        TopXA[x] = inputDevice.nextLine();
        System.out.println("You have " + (ListSize - x - 1) + " items left to fill on the list.");
    }

    System.out.println("Now we will compare each item on the list with every item on the list one at a time.");
    System.out.println("We will ask you a series a question on whether you like item A better then item B and tally the score.");
    System.out.println("At the end the item with the most points wins.");

    int comparisonsToMake = TopXA.length - 1;
    for (int y = 0; y < TopXA.length - 1; ++ y) 
    {   
        for (int z = 0; z < comparisonsToMake; ++ z)
        {
            if(TopXA[y] != TopXA[z + 1])
            {
                String compareA = TopXA[y];
                String compareB = TopXA[z + 1];

                System.out.println("Do you prefer "  + compareA + " or " + compareB + " .");
                System.out.println("If you prefer " + compareA + " Please press 1. If you prefer " + compareB + " please press 2.");
                int choice = inputDevice.nextInt();
                inputDevice.nextLine();
                switch(choice)
                {
                case 1:
                    TopXB[y] =+ 1;
                    break;
                case 2:
                    TopXB[z + 1] =+ 1;
                    break;
                default:
                    System.out.print("I'm sorry but that is not a valid input.");
                }
            }
        }
        --comparisonsToMake;
    }

    int comparisonsToMakeB = TopXB.length - 1;
    for(int a = 0; a < TopXB.length - 1; ++ a)
    {
        for(int b = 0; b < comparisonsToMakeB; ++b)
        {
            if(TopXB[b] < TopXB[b + 1])
            {
                String temp = TopXA[b];
                TopXA[b] = TopXA[b + 1];
                TopXA[b + 1] = temp;
            }
        }
        --comparisonsToMakeB;
    }
    for(int q = 0; q < TopXA.length; ++ q)
    {
        System.out.print("Your number " + (q + 1) + " pick is "  + TopXA[q] + ".");
    }

    }
 }

【问题讨论】:

  • 我刚刚意识到,对我正在做的事情使用气泡搜索可能是一个固有缺陷的前提。因此,我想我真正要问的是,将数组中的给定数量的项目与每个可能的配对组合进行一次比较的替代方法是什么。此外,我还了解到使用并行数组显然是一种笨拙的技术,所以如果有替代方法,我会采用它。

标签: java arrays for-loop bubble-sort string-search


【解决方案1】:

没关系,我找到了答案....其实很简单。我只是将 z 的所有特定实例更改为 y。

int comparisonsToMake = TopXA.length - 1;
for (int y = 0; y < TopXA.length - 1; ++ y) 
{   
    for (int z = 0; z < comparisonsToMake; ++ z)
    {
        if(TopXA[y] != TopXA[z + 1]) // changed TopXA[z + 1] to [y + 1]
        {
            String compareA = TopXA[y];
            String compareB = TopXA[y + 1]; //instance of change

            System.out.println("Do you prefer "  + compareA + " or " + compareB + " .");
            System.out.println("If you prefer " + compareA + " Please press 1. If you prefer " + compareB + " please press 2.");
            int choice = inputDevice.nextInt();
            inputDevice.nextLine();
            switch(choice)
            {
            case 1:
                TopXB[y] =+ 1; 
                break;
            case 2:
                TopXB[y + 1] =+ 1; //instance of change.
                break;
            default:
                System.out.print("I'm sorry but that is not a valid input.");
            }
        }
    }
    --comparisonsToMake;
}

老实说,我不知道我为什么要做出这样的改变。可能是因为我累了,昨晚正在研究它,并且在思考一些疯狂的巨魔逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 2019-01-19
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多