【发布时间】:2014-04-14 07:56:17
【问题描述】:
我们需要创建两个类:GirlScoutCheck 和 CookieAnalyzerCheck。我们需要获得用户输入——询问他们每个女孩的名字,直到他们输入 runaway。然后我们向他们询问每个女孩卖出的饼干盒的数量,然后分别将每个女孩的饼干盒相加并乘以 3.50。这就是总收入。然后我们根据总收入按降序对它们进行排序。我已经做了几乎所有事情。我只需要排序和打印排序信息的帮助。谁能帮我完成这个?谢谢。
package Pcs;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class CookieAnalyzerCheck {
public static void main(String args[]) {
NumberFormat nf = NumberFormat.getCurrencyInstance();
String name = "";
ArrayList al = new ArrayList();
do {
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the name of each"
+ " girl Scout, or \"RUNAWAY\" "
+ "to exit.");
name = kb.nextLine();
if (!name.equalsIgnoreCase("runaway")) {
System.out.println("Enter the number of Thin Mints sold");
int ThinMints = kb.nextInt();
System.out.println("Enter the number of Caramel Delights sold");
int CaramelDelights = kb.nextInt();
System.out.println("Enter the number of Lemonades sold");
int Lemonades = kb.nextInt();
System.out.println("Enter the number of ThanksALot sold");
int ThanksALot = kb.nextInt();
System.out.println("Enter the number of MangoCremes sold");
int MangoCremes = kb.nextInt();
GirlScoutCheck ba = new GirlScoutCheck(name, ThinMints, CaramelDelights, Lemonades, ThanksALot, MangoCremes);
al.add(ba);
System.out.println(""); //makes output easier to read
System.out.println();
}
} while (!name.equalsIgnoreCase("runaway"));
GirlScoutCheck thisBA = (GirlScoutCheck) al.get(0);
double maxBalance = thisBA.totalIncome;
String maxName = thisBA.name;
for (int i = 1; i < al.size(); i++) {
thisBA = (GirlScoutCheck) al.get(i);
if (thisBA.totalIncome > maxBalance) {
//we have a new winner, so save the..
maxBalance = thisBA.totalIncome;
maxName = thisBA.name;
int a= al.size();
}
}
}
}
package Pcs;
import java.util.Collections;
public class GirlScoutCheck implements Comparable {
public String name;
public int ThinMints;
public int CaramelDelights;
public int Lemonades;
public int ThanksALot;
public int MangoCremes;
public int totalCookies;
public double totalIncome;
public GirlScoutCheck(String nm, int tm,int cd, int lem, int tal, int mc){
name = nm;
ThinMints = tm;
CaramelDelights = cd;
Lemonades = lem;
ThanksALot = tal;
MangoCremes = mc;
totalCookies = ThinMints+CaramelDelights+Lemonades+ThanksALot+MangoCremes;
totalIncome = totalCookies*3.50;
}
public int totalCookies(){
return totalCookies;
}
public double totalIncome(){
return totalIncome;
}
public int compareTo(Object done){
GirlScoutCheck b= (GirlScoutCheck)done;
if (totalIncome>b.totalIncome){
return 1;
}
else if(totalIncome<b.totalIncome) {
return -1;
}
else{
return 0;
}
}
public String getName(){
String nameParts[] = name.split(" ");
String first = nameParts[0];
String last = nameParts[1];
return String.format(last + "," + first);
}
@Override
public String toString() {
return "girlScoutCheck [name=" + getName() + ", ThinMints=" + ThinMints
+ ", CaramelDelights=" + CaramelDelights + ", Lemonades="
+ Lemonades + ", ThanksALot=" + ThanksALot + ", MangoCremes="
+ MangoCremes + ", totalCookies=" + totalCookies + "]"+ System.lineSeparator()+ " (total income for Thin Mints is $" +
ThinMints*3.50+ ") ( total income for Caramel Delights is $" +
CaramelDelights*3.50+ ") total income for Lemonades is $" +
Lemonades*3.50+ ")( total income for MangoCremes is $" +
MangoCremes*3.50+ ") (total income for ThanksALot is $" +
ThanksALot*3.50+ ")"+System.lineSeparator() ;
}
}
【问题讨论】:
-
能否请您概述一下您正在处理的问题?目前还不是很清楚,是什么问题。
-
您似乎忘记在尝试排序的地方分享您的代码。通常它只是调用适当的排序方法(在数组或集合上),可能使用自定义比较器(这可能是必要的,因为您想对递减排序)。
-
@Warlord- 我不知道在 CookieAnalyzerCheck 类结束时排序的方法。
标签: java algorithm sorting collections arraylist