Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

java - How do I create an array that organizes the taco prices in ascending order along with its taco name?

This is the code that I have so far. I am using string methods to represent the taco names and double to represent the price. However, I have no clue how to start the array of taco names and prices. I need the array to produce an output that looks like this:

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

Someone please help.

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("
The 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
");
            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
");
            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
");
            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");
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should create a class to represent your tacos, like so:

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

public Taco() {}

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

You can then sort them using Collections.sort(), passing it a comparator:

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();
}
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...