gpt4 book ai didi

java - 我如何按降序完成排序?

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:27:50 25 4
gpt4 key购买 nike

我们需要创建两个类: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() ;

}


}

最佳答案

我认为您的 main 中遗漏了一些内容方法:

  • 你的变量nf 从未使用过
  • 因为您想对 GirlScoutCheck 进行排序的,我建议您使用 TreeSet存储它们,而不是 ArrayList . TreeSet是始终排序的元素集合 ( here is its javadoc entry )。

理想情况下,您应该按如下方式声明和初始化该容器:

final Set<GirlScoutCheck> al = new TreeSet<>();
  • 一般情况下,您应该始终关闭流和任何外部资源。 id est:关闭你的 Scanner在离开你的 main 之前方法。

您可以简单地使用 Try-With-Resource 来做到这一点语法如下:

try (final Scanner kb = new Scanner(System.in)) {
do {
// your stuff
kb.nextLine();
} while (!name.equalsIgnoreCase("runaway"));
}

顺便说一句,您可能需要在阅读另一个女孩的名字之前消耗最后一个换行符:我建议您直接调用kb.nextLine();在重新输入您的 do/while 之前循环(正如我在上面的 Try-With-Resource 示例中所做的那样)。

基本上,您的主要方法可以是:

final Set<GirlScoutCheck> al = new TreeSet<>();

try (final Scanner kb = new Scanner(System.in)) {
do {
// your stuff
kb.nextLine();
} while (!name.equalsIgnoreCase("runaway"));
}

// from now on, al will contain all the GirlScoutCheck's ordered "as you want"

for (final GirlScoutCheck gsc : al) {
System.out.println(gsc);
}

然后...就是这样!


现在,实际上,要实现此“自动排序”,我们需要声明如何比较您的 GirlScoutCheck的。你做的是对的 GirlScoutCheck实现 Comparable :)

但是,您应该指定您正在实现 Comparable<GirlScoutCheck> .因此,compareTo 的签名您覆盖的方法变为:

public int compareTo(final GirlScoutCheck other)

代替:

public int compareTo(final Object other);

你也可以这样简化它:

@Override
public int compareTo(final GirlScoutCheck o) {
return (int) (o.totalIncome - this.totalIncome);
}

最后一点:equals 一致是确保已排序集合(例如 TreeSet)行为良好所必需的。
来源:Implementing compareTo - JavaPractices

因此,您还应该实现 equalshashcode为你的 GirlScoutCheck的,仅使用 totalIncome字段(与您的 compareTo 实现保持一致)。别担心,您的 IDE 可能会为您做这件事 :)无论如何,我也可以。所以这里是:

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
long temp;
temp = Double.doubleToLongBits(this.totalIncome);
result = (prime * result) + (int) (temp ^ (temp >>> 32));
return result;
}

@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final GirlScoutCheck other = (GirlScoutCheck) obj;
if (Double.doubleToLongBits(this.totalIncome) != Double.doubleToLongBits(other.totalIncome)) {
return false;
}
return true;
}

TL;恢复

只是...

  • 将该段代码推送到您的 GirlScoutCheck 中类(class),
  • 替换它的compareTo按照我给你的方法,
  • 一定要让它实现Comparable<GirlScoutCheck>而不仅仅是 Comparable ,
  • 修改main我推荐的方法...

一切都应该运行良好;)

关于java - 我如何按降序完成排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22313288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com