【问题标题】:Bubble sorting of the users entered prices?用户输入价格的冒泡排序?
【发布时间】:2015-09-23 23:32:29
【问题描述】:

我正在尝试对每个 taco (1-10) 的价格进行冒泡排序,同时让 taco 名称遵循其原始价格(不需要对 taco 名称进行排序)。但是,我在排序的 if 语句中收到错误消息。

Exception in thread "main" java.lang.Error:
Unresolved compilation problems: The type of the expression must be an array type but it resolved to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
Type mismatch: cannot convert from double to int
    at TacoSort.main(TacoSort.java:36)

我可能是冒泡排序不准确,还是我没有适当地合并字符串?

import java.util.Scanner;

class TacoSort 
{
    //Create a constant amount of temperatures
    public static int NUMBER_OF_TACOS = 10;
    public static int NUMBER_OF_PRICES = 10;
    public static void main(String[] args) 
    {
        // TODO Auto-generated method stub

        Scanner keyboard = new Scanner(System.in);
        System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");

        //Populates array of 10 tacos
        //Prompts user to enter name of each taco

        String[] tacos = new String[NUMBER_OF_TACOS];
        for (int i = 0; i < NUMBER_OF_TACOS; i++) 
        {
            System.out.print("Enter the name of taco " + (i+1) + "\n");
            tacos[i] = keyboard.next();

            //Populates array of 10 prices
            //Prompts user to enter price of each taco
            double[] prices = new double[NUMBER_OF_PRICES];
            //for (int j = 0; j < NUMBER_OF_PRICES; j++) //

            System.out.print("Enter taco's price " + (i+1) + "\n");
            prices[i] = keyboard.nextDouble();
        }

        for(double i = 0; i < NUMBER_OF_PRICES; i++)
        {
            for(double j = i + 1; j < tacos.length; j++)
            {
                if(NUMBER_OF_PRICES[i] > tacos[(int) j])
                {
                    String temp = tacos[i];
                    tacos[i] = tacos[j];
                    tacos[i] = temp;
                }
            }
        }
        for(int i = 0; i < tacos.length; i++)
        {
            System.out.print(tacos[i] + " ");
        }
    }

}

【问题讨论】:

  • 能否打印错误信息?
  • 线程“main” java.lang.Error 中的异常:未解决的编译问题:表达式的类型必须是数组类型,但它解析为 int 类型不匹配:无法从 double 转换为 int 类型不匹配: 无法从 double 转换为 int 类型不匹配:无法从 double 转换为 int 类型不匹配:无法从 double 转换为 int 类型不匹配:无法在 TacoSort.main(TacoSort.java:36) 中从 double 转换为 int
  • 这篇文章让我饿了
  • 每个 Taco 可以有 10 个价格?在第一个循环中检查新的。这个声明if(NUMBER_OF_PRICES[i] &gt; tacos[(int) j]) - 也必须重写。 NUMBER_OF_PRICES 设置为 10。您不应该对其进行索引。我建议您从一个数组开始,该数组使用 Taco 名称和价格(即没有用户输入)进行初始化。让冒泡排序工作,然后在您希望用户输入数据的地方实现代码。
  • 我需要它大于或等于炸玉米饼的数量。

标签: java arrays sorting


【解决方案1】:

请查看我的内联 cmets,我认为它们会对您有所帮助。 您的解决方案存在一些不同的问题,这些问题已在我的 cmets 中记录。最大的问题是您没有跟踪与 tacos 名称交换的价格,因为两者都有两个不同的数组。因此,如果您考虑一下,如果您交换 taco 名称,它将与不同的 tacos 价格相关联,这不是我们想要的。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class TacoSort {
    //Create a constant amount of temperatures
    public static int NUMBER_OF_TACOS = 5; //<-- you should consider only using one constant value since they will both always be the same
    public static int NUMBER_OF_PRICES = 5;

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub

        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); //<-- notice i changed replace the scanner with this line to allow spaces in between the name of the tacos
        System.out.println("Welcome to the taco price sorter! Enter 10 taco names and prices and I'll sort it!");

        //Populates array of 10 tacos
        //Prompts user to enter name of each taco

        String[] tacos = new String[NUMBER_OF_TACOS]; //<-- it would be a better practice to create a taco object which would have two values name and price.
        //Populates array of 10 prices
        //Prompts user to enter price of each taco
        double[] prices = new double[NUMBER_OF_PRICES]; //<-- error should initialize this array outside the loop or you will create a new one on every iteration
        for (int i = 0; i < NUMBER_OF_TACOS; i++) {
            System.out.print("Enter the name of taco " + (i + 1) + "\n");
            tacos[i] = keyboard.readLine(); //<-- you want to read in next line so you can have spaces in your taco names

            System.out.print("Enter taco's price " + (i + 1) + "\n");
            prices[i] = Double.parseDouble(keyboard.readLine());
        }

        for (int i = 0; i < NUMBER_OF_PRICES; i++) {
            for (int j = 1; j < NUMBER_OF_PRICES; j++) {
                if (prices[j] < prices[j - 1]) //<-- error here you need to compare prices not what you were doing before by comparing the max size of your array
                {
                    String temp = tacos[j];
                    tacos[j] = tacos[j - 1];
                    tacos[j - 1] = temp; //<-- error here should be swapping element j here

                    /* NOTE YOU NEED TO KEEP TRACK OF THE PRICES SWAP AS WELL */
                    double tempDouble = prices[j];
                    prices[j] = prices[j - 1];
                    prices[j - 1] = tempDouble;
                }
            }
        }
        System.out.println("Sorted Tacos are");
        for (int i = 0; i < tacos.length; i++) {
        System.out.println("Taco Prices " + tacos[i] + " " + prices[i] + " ");
    }
    }

}

【讨论】:

  • 它仍然无法识别价格。由于价格双倍 tempDouble = 价格 [j] 的错误,我无法运行该程序;价格[j] = 价格[j - 1];价格[j - 1] = tempDouble;
  • 您的原始代码中有很多错误。我假设您试图仅将我的部分代码复制并粘贴到您的解决方案中?发生此错误是因为在您的原始代码中您将价格放入 for 循环中。如果您看到我的代码,我将其从 for 循环中取出并放在 tacos 数组之后
  • 我已经在本地运行了代码,并且可以正常工作。我对您的代码进行了多次修改,请查看我添加到代码中的 cmets 以了解差异
  • 好的。我运行了您的代码,但输出未显示按名称组织的价格。
  • @PaulL 我逐行打印它们,不确定这是您想要的还是全部在一行上。如果您正在寻找不同的东西,您可以更改打印。如果此答案解决了您的问题,请不要忘记标记已接受的答案
猜你喜欢
  • 1970-01-01
  • 2012-10-20
  • 2018-03-29
  • 2019-01-19
  • 2016-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
相关资源
最近更新 更多