![](/img/trans.png)
[英]Creating arrays with successive numbers of integers for benchmarking purposes using sorting algorithms
[英]Creating & Sorting Arrays CoffeeDriver
我应该创建一个应用程序来跟踪本地咖啡店的物品,另外还提供一种打印物品清单的方法。 我应该创建一个名为Item的类,
然后,我应该使用以下方法创建一个CoffeeDriver:
sortName
–此方法按项目名称对项目数组进行排序,然后在屏幕上显示所有项目的名称和价格 sortPrice
–此方法按商品价格对商品数组进行排序,然后在屏幕上显示所有商品的名称和价格 main
它使用上面的数据创建Item对象的数组,以设置每个Item的信息。 初始化阵列后,提示用户如何查看列表-按名称或价格排序。 然后调用适当的方法。 这是我到目前为止为咖啡驱动程序提供的代码。 这件事对我来说真的很难:(
import javax.swing.*;
import java.text.DecimalFormat;
import java.util.*;
public class CoffeeDriver
{
public static void main(String[] args)
{
Item[] items = new Item[]
{
new Item("Donut", .75),
new Item("Coffee", 1.00),
new Item("Bagel", 1.25),
new Item("Milk", 1.50),
new Item("Water", 2.00)
};
Object[] possibleValues = { "Price", "Name" };
Object selectedValue = JOptionPane.showInputDialog(null,
"How would you like items sorted", "SORTING",
JOptionPane.INFORMATION_MESSAGE, null,
possibleValues, possibleValues[0]);
if(selectedValue == "Price")
{
System.out.println("sorting by price" + items);
}
}
public static void sortPrice(String[] name, double[] price)
{
DecimalFormat money = new DecimalFormat("$0.00");
arrays.sort(price);
JOptionPane.showMessageDialog(null, name[0]+ " " + money.format(price[0]) + "\n" +
name[1] + " " + money.format(price[1]) + "\n" +
name[2] + " " + money.format(price[2]) + "\n" +
name[3] + " " + money.format(price[3]) + "\n" +
name[4] + " " + money.format(price[4]));
}
}
}
if(selectedValue ==“价格”)
不要使用“ ==”来比较对象。 使用equals()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.