如何创建,组织塔科价格升序排列,其塔科名一起数组? [英] How do I create an array that organizes the taco prices in ascending order along with its taco name?

查看:137
本文介绍了如何创建,组织塔科价格升序排列,其塔科名一起数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是code,我有这么远。我使用的字符串方法来重新present塔科名称和双重重新present的价格。不过,我不知道如何开始塔科名称和价格的数组。我需要的数组产生一个输出看起来像这样的:

 排序是炸玉米饼
塔克价格脆皮土豆软塔克0.99
塔克价格松脆塔克1.19
塔克价格软塔克1.19
塔克价格多力多滋Locos的塔克(纳乔奶酪)1.49
塔克价格香脆塔克苏$ P $ 1.59 PME
塔克价格软塔克苏$ P $ 1.59 PME
塔克价格鸡软塔克1.79
塔克价格双层塔克1.89
塔克价格多力多滋玉米饼LOCS(炽热)苏$ P $ 1.89 PME
塔克价格双层塔克苏$ P $ 2.29 PME

有人请帮助。

 进口java.util.Scanner中;
公共类TacoSortProgram {    公共静态无效的主要(字串[] args){
        // TODO自动生成方法存根
        //写一个程序,基于价格升序排序的玉米饼10的价格,使用数组        的System.out.println(欢迎到塔科价格分类器输入10塔科名称和价格,我会整理的!);
        扫描仪键盘=新的扫描仪(System.in);           // System.out.printf($%4.2f每个%S,价格,项目);
           // System.out.printf(\\ n此一共是:$%4.2f,总数);            //过程件商品的
            的System.out.println(输入塔科1名);
            字符串塔科= keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双价= Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科2的名称);
            串taco2 = keyboard.nextLine();
            System.out.print(输入塔科价格\\ n);
            双price2 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科3名);
            串taco3 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price3 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科4名);
            串taco4 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price4 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科5名);
            串taco5 = keyboard.nextLine();
            System.out.print(输入塔科价格\\ n);
            双price5 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科6名);
            串taco6 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price6 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科7名);
            串taco7 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price7 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科8名);
            串taco8 = keyboard.nextLine();
            System.out.print(输入塔科价格\\ n);
            双price8 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科9名);
            串taco9 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price9 = Double.parseDouble(keyboard.nextLine());            的System.out.println(输入塔科10名);
            串taco10 = keyboard.nextLine();
            的System.out.println(输入塔科的价格);
            双price10 = Double.parseDouble(keyboard.nextLine());            的System.out.println(排序玉米饼是);


解决方案

您应该创建一个类来重新present你的玉米饼,像这样:

 公共类塔克{
公共字符串名称;
公共双价;公共塔克(){}塔克(字符串名称,双击价格){
    this.name =名称;
        this.price =价格;
    }
}

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

 进口java.util.Scanner中;
进口的java.util.ArrayList;
进口java.util.Collections中;
进口了java.util.Comparator;公共类的测试{
公共静态无效的主要(字串[] args){
    //写一个程序进行升序排序的玉米饼10价格
    //基础上的价格,使用数组    ArrayList的<&塔克GT;玉米饼=新的ArrayList<>();
    扫描仪键盘=新的扫描仪(System.in);    的System.out.println(欢迎到塔科价格分类器输入10塔科名称和价格,我会整理的!);    //循环10次。
    对(INT I = 0; I&小于10;我++){
        //获取名称为每个新项目
        的System.out.println(输入塔科的名+第(i + 1));
        字符串塔科= keyboard.nextLine();
        的System.out.println(输入塔科的价格);
        双价= Double.parseDouble(keyboard.nextLine());
        塔克T =新塔克(塔科,价格);        //将它添加到我们的数组列表。
        tacos.add(T);
    }    //排序列表:
    Collections.sort(玉米饼,新的比较<&塔克GT;(){
         公众诠释比较(塔可O1,O2塔克){
             如果(o1.price == o2.price)
                 返回0;
             返回o1.price< o2.price? -1:1;
         }
    });
    的System.out.println(排序玉米饼是:);
    对于(塔克T:炸玉米饼){
        的System.out.println(塔克价格:+ t.name ++ t.price);
    }    keyboard.close();
}
}

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("\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");

解决方案

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

这篇关于如何创建,组织塔科价格升序排列,其塔科名一起数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆