【发布时间】: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 次,对吧?你知道你必须使用一个数组,但我在发布的代码中没有看到任何数组。