gpt4 book ai didi

java - 需要一种方法将字符串与单个数组元素相关联

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:19:17 25 4
gpt4 key购买 nike

我完全是编程初学者,我正在为我母亲开发一个程序,通过“赛马”跟踪她员工的收入,每个员工都有一匹马,程序会跟踪他们对 UI 的输入看起来像一个赛道。在我上次查询的帮助下,我已经大大简化了我的代码困惑,但我现在面临一个新问题,在对值从大到小进行排序后,我无法将排序后的值与正确的马相关联.我知道这种解释令人困惑,所以我希望我的代码能帮我解决大部分问题。

老实说,我不知道从哪里开始。正如我在上次询问中所说,我是一个完全的初学者,严重缺乏在这里找到答案的术语或知识。

public class HorseRace {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String horse1 = "#5 Gitty-Up";
String horse2 = "#7 Lady Simmons";
String horse3 = "#6 Burning Peanutbutter";
String horse4 = "#10 White Lightning";
String horse5 = "#3 Bella";
String horse6 = "#1 Meg The Stallion";
float h1val;
float h2val;
float h3val;
float h4val;
float h5val;
float h6val;
System.out.println("Input amount for " + horse1 + ":");
h1val = sc.nextFloat();
System.out.println("Input amount for " + horse2 + ":");
h2val = sc.nextFloat();
System.out.println("Input amount for " + horse3 + ":");
h3val = sc.nextFloat();
System.out.println("Input amount for " + horse4 + ":");
h4val = sc.nextFloat();
System.out.println("Input amount for " + horse5 + ":");
h5val = sc.nextFloat();
System.out.println("Input amount for " + horse6 + ":");
h6val = sc.nextFloat();
Float[] values = new Float[]{h1val, h2val, h3val, h4val, h5val, h6val};
Arrays.sort(values, Collections.reverseOrder());
//currently displays horses with the wrong number. Need a way to tie the horse name strings to their respective float elements
System.out.println("The current race progress is :");
System.out.println(horse1 + " with $" + values[0]);
System.out.println(horse2 + " with $" + values[1]);
System.out.println(horse3 + " with $" + values[2]);
System.out.println(horse4 + " with $" + values[3]);
System.out.println(horse5 + " with $" + values[4]);
System.out.println(horse6 + " with $" + values[5]);
}
}

我想要的结果是用正确的值打印出正确的马。例如,如果我输入#5 带来了 11 美元,#7 带来了 14 美元,程序将打印出#7 以 14 美元领先,#5 以 11 美元位居第二。

目前,该程序始终打印 #5 作为最高值的领先者,#7 为第二高的值,等等。

我明白这是因为我很难调用 horse1-horse6 值,这意味着它们不会改变,但在我弄清楚如何将正确的马与正确的值相关联时,这些值更多地充当占位符

最佳答案

您应该在此处创建一个 Horse 类并将数据存储为 Horse 的实例。

class Horse {
private String name;
private float value;

public String getName() { return name; }
public float getValue() { return value; }

public void setName(String name) { this.name = name; }
public void setValue(float value) { this.value = value; }
}

然后在你的主要方法中:

Horse[] horses = new Horse[6] {
new Horse(), new Horse(), new Horse(), new Horse(), new Horse(), new Horse()
};
horses[0].setName("#5 Gitty-Up");
horses[1].setName("#7 Lady Simmons");
horses[2].setName("#6 Burning Peanutbutter");
// and so on...

// you should use a for loop here instead of writing similar lines over and over again!
for (int i = 0 ; i < 6 ; i++) {
System.out.println("Input amount for " + horses[i].getName() + ":");
horses[i].setValue(sc.nextFloat());
}

Arrays.sort(horses, Comparator.comparingDouble(Horse::getValue).reversed());
System.out.println("The current race progress is :");
for (int i = 0 ; i < 6 ; i++) {
System.out.println(horses[i].getName() + " with $" + horses[i].getValue());
}

通过使用类,您实质上是将属于一起的数据分组在一起。在 Arrays.sort(horses, Comparator.comparingDouble(Horse::getValue).reversed()); 行中,我正在对整个马数组一起进行排序,方法是他们的值(value)观。

如果类和对象的概念对您来说是新的,那就意味着是时候学习一些新概念了。类和对象非常重要。

关于java - 需要一种方法将字符串与单个数组元素相关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57643306/

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