【问题标题】:How do I create an array that organizes the taco prices in ascending order along with its taco name?如何创建一个数组,以升序组织 taco 价格及其 taco 名称?
【发布时间】:2015-09-20 13:48:07
【问题描述】:

这是我到目前为止的代码。我使用字符串方法来表示 taco 名称并使用 double 来表示价格。但是,我不知道如何启动一系列 taco 名称和价格。我需要该数组来生成如下所示的输出:

Sorted Tacos are
Taco Prices Crispy Potato Soft Taco 0.99
Taco Prices Crunchy Taco 1.19
Taco Prices Soft Taco 1.19
Taco Prices Doritos Locos Taco (Nacho Cheese) 1.49
Taco Prices Crunchy Taco Supreme 1.59
Taco Prices Soft Taco Supreme 1.59
Taco Prices Chicken Soft Taco 1.79
Taco Prices Double Decker Taco 1.89
Taco Prices Doritos Locs Tacos(Fiery) Supreme 1.89
Taco Prices Double Decker Taco Supreme 2.29

请有人帮忙。

import java.util.Scanner;
public class TacoSortProgram {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Write a program that sorts prices of 10 tacos in ascending order based on the price, using arrays

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

           //       System.out.printf("$%4.2f for each %s ", price, item);
           //       System.out.printf("\nThe total is: $%4.2f ", total);

            //process for item one
            System.out.println("Enter the name of taco 1");
            String taco = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 2");
            String taco2 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price2 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 3");
            String taco3 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price3 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 4");
            String taco4 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price4 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 5");
            String taco5 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price5 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 6");
            String taco6 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price6 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 7");
            String taco7 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price7 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 8");
            String taco8 = keyboard.nextLine();
            System.out.print("Enter taco's price\n");
            double price8 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 9");
            String taco9 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price9 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Enter the name of taco 10");
            String taco10 = keyboard.nextLine();
            System.out.println("Enter taco's price");
            double price10 = Double.parseDouble(keyboard.nextLine());

            System.out.println("Sorted tacos are");

【问题讨论】:

  • 使用包含 Taco 类实例的数组或列表。创建 Taco 类,其中包含 2 个字段:名称和价格。谷歌关于如何对对象列表/数组进行排序。每当您像上面一样多次重复相同的代码时,您必须认为:“这不可能。让我们使用循环。”。老师很客气地要了10个炸玉米饼。如果他要 1000 个炸玉米饼怎么办。您不会创建 1000 个变量并重复相同的 4 行代码 1000 次,对吧?你知道你必须使用一个数组,但我在发布的代码中没有看到任何数组。

标签: java arrays


【解决方案1】:

您应该创建一个类来表示您的炸玉米饼,如下所示:

public class Taco {
public String name;
public double price;

public Taco() {}

Taco(String name, double price){
    this.name  = name;
        this.price = price;
    }
}

然后您可以使用 Collections.sort() 对它们进行排序,并将比较器传递给它:

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test {
public static void main(String[] args) {
    // Write a program that sorts prices of 10 tacos in ascending order
    // based on the price, using arrays

    ArrayList<Taco> tacos = new ArrayList<>();
    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!");

    // Loop 10 times.
    for( int i = 0; i < 10; i++ ){
        // Get the name for each new item
        System.out.println("Enter the name of taco " + (i + 1) );
        String taco = keyboard.nextLine();
        System.out.println("Enter taco's price");
        double price = Double.parseDouble(keyboard.nextLine());
        Taco t = new Taco(taco, price);

        // Add it to our array list.
        tacos.add(t);
    }

    //Sort the list:
    Collections.sort(tacos, new Comparator<Taco>(){
         public int compare(Taco o1, Taco o2){
             if(o1.price == o2.price)
                 return 0;
             return o1.price < o2.price ? -1 : 1;
         }
    });


    System.out.println("Sorted tacos are:");
    for( Taco t : tacos ){
        System.out.println("Taco Prices: " + t.name + " " + t.price );
    }

    keyboard.close();
}
}

【讨论】:

  • 比较器不应该是 Taco。你不应该创建一个 Taco 来对其他 tacos 进行排序。
  • 同意,我现在更新了我的代码以提供更好的答案。
  • 好多了。您现在可以删除 Taco 的无参数构造函数。您也可以使用 return Double.compare(o1.getPrice(), o2.getPrice()); 而不是重新实现它。您绝对应该将这两个字段设为私有,而不是公开。
【解决方案2】:

您可以创建一个名为 Taco 的类。

一旦用户输入了价格和名称,您就可以启动 Taco 类的对象并分配值。

将所有对象保存在一个数组中。

并使用排序算法。

【讨论】:

    【解决方案3】:

    为 taco 创建一个单独的类。然后创建nameprice 作为其成员变量。现在将 taco 的实例创建为数组。喜欢taco tacoInst[10]。现在您可以参考每个 taco 的价格为tacoInst[0].price。现在,当您对价格进行排序时,名称会自动排序。反之亦然。

    【讨论】:

      【解决方案4】:

      要添加到上面人们的答案,是的 - 你应该开发一个 Taco 类,为什么不尝试实现 Comparable?我不确定你的 java 有多先进,以及你是否已经完成了接口,但看看你已经设置的问题的级别,它似乎会给你一些可靠的额外信用/炫耀信用。

      您可以在 Enno Shioji 的回答 here(第一个)中找到一个很好的例子。基本上你要做的是声明 Taco implements Comparable&lt;Taco&gt; 然后覆盖 compareTo 方法。这将允许您使用 Collections.sort(tacos); 自动对 tacos 进行排序(这里数组的名称是 tacos,如果您调用它,例如 tacoArray,则命令将是 Collections.sort(tacoArray);

      编辑:除了 JB Nizet 在此答案下方的评论之外,我在这里尝试做的是标记您可能不熟悉的新 Java 技术。他完全正确,因为它不是世界上见过的最伟大的实现,我的逻辑是,如果你不知道这些东西,那么它可能会带来一个有趣的挑战。

      【讨论】:

      • 不让 Taco 具有可比性有两个很好的理由。第一个原因:Comparable 应该与 equals() 一致。这意味着如果 Comparable 也应该实现 equals() 和 hashCode(),这意味着价格相同但名称不同的两个 taco 会变得相等,这不太现实。第二个原因:Comparable 定义了一个类的自然顺序。为什么价格会定义一个自然顺序?为什么没有名字?按价格排序是一个特定的用例,因此应该由特定的比较器来实现,而不是让价格定义自然顺序。
      • 完全同意,我掩饰了很多。我的回答(是的,我应该更清楚地说明这一点 - 我将在此评论之后进行编辑)更多地旨在将新概念和技术引入看起来非常像家庭作业的内容。但是,我不确定一般意义上的 equals() 和 hashcode() 点,没有什么可以阻止他的 compareTo 方法考虑到在早餐 Tac 1.99 之后对大型 Taco 1.99 进行排序的名称,它只是更高级一点。但是,正如我所说,您是对的,我应该更清楚我的答案的意图。
      • 让 Taco 本身具有可比性没有问题,只要它与 equals 一致,并且不只是按价格比较。但既然这是用户想要的,那么 Comparator 更合适。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2016-01-06
      • 2013-05-10
      • 2013-06-13
      • 1970-01-01
      • 2021-10-25
      相关资源
      最近更新 更多