java - 对用户输入的价格进行冒泡排序?

标签 java arrays sorting

我试图对每个炸 Jade 米饼的价格(1-10)进行冒泡排序,同时让炸 Jade 米饼名称遵循其原始价格(炸 Jade 米饼名称不需要排序)。但是,我在排序的 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] + " ");
        }
    }

}

最佳答案

请参阅我的内嵌评论,我认为它们会对您有所帮助。 您的解决方案存在一些不同的问题,这些问题已在我的评论中指出。最大的问题是您没有跟踪与炸 Jade 米饼名称交换的价格,因为您有两个不同的数组。因此,如果您考虑一下,如果交换炸 Jade 米饼名称,它将与不同的炸 Jade 米饼价格相关联,这不是我们想要的。

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] + " ");
    }
    }

}

关于java - 对用户输入的价格进行冒泡排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32751118/

相关文章:

java - 有什么理由在 Web 上使用 Java 而不是 Django/Python、PHP 等?

java - 如何创建一个由用户输入定义长度的静态数组?

arrays - 查找数组中的重复元素

javascript - 通过 Apple Automator 运行 Javascript 排序

java - RMI Server的哪一部分是用多线程实现的

java - 在循环中对可变文本使用 spannable

java - 标记为@NotNull 的最终字段未初始化

php - 为什么我在以下场景中没有获得更新的数组?

algorithm - 选择可以稳定吗?

ios - 对包含 Å、Ä 和 Ö 的字符串数组进行排序 - Swift