gpt4 book ai didi

java - 需要比较 ArrayList 中的对象以返回最高值

转载 作者:太空宇宙 更新时间:2023-11-04 10:59:04 24 4
gpt4 key购买 nike

在这个项目中(因为我是初学者,所以使用 BlueJ),我希望将 Climbers 添加到 ArrayList 中,其中包含他们的姓名、性别和年龄。登山者可以添加他们攀登过的山峰。带有山名和高度。

我需要在 Climber 类中添加一个方法来返回某个 Climber 攀登过的最 Alpine 峰。

要定义该方法,我是否使用它来定义它

public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight)

我不确定如何检查山类中的对象进行比较。仅供引用,我还没有学习比较关键字,所以想避免使用它。

俱乐部类别:

import java.util.ArrayList;
import java.util.Scanner;
/**
* Write a description of class Club here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Club
{
// An ArrayList for storing climber details.
private ArrayList<Climber> climbers;

/**
* Constructor for objects of class Club
*/
public Club()
{
// Initialise instance variables.
climbers = new ArrayList<Climber>();
}

public void addClimber(Climber newName)
{
climbers.add(newName);
}


public Climber getClimber(String name)
{
Climber foundClimber = null;
int index = 0;
boolean searching = true;

while(searching && index < climbers.size()) {
Climber climber = climbers.get(index);
if(climber.getName().equals(name)) {
searching = false;
foundClimber = climber;
}
else {
System.out.println(name + " not found");
index++;
}
}
return foundClimber;
}

public void displayClimberList()
{
for (int item = 0; item<climbers.size();
item++) {
Climber climber = climbers.get(item);
System.out.println(climber.getName() + (" ") + climber.getAge() + (" ")
+ climber.getGender());
}
}

}

登山者级别:

import java.util.ArrayList;
/**
* Write a description of class Climber here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Climber
{
// Instance variables.
// The climber name.
private String name;
// The climber age
private int age;
// The climber gender.
private String gender;
private ArrayList<Mountain> mountains;


/**
* Constructor for objects of class Climber
*/
public Climber (String newName, int newAge, String newGender)
{
// Initialise instance variables.
name = newName;
age = newAge;
gender = newGender;
mountains = new ArrayList<Mountain>();

}

/**
* Accessor method for climber's name.
*/
public String getName()
{
return name;
}

/**
* Set the climber's name.
*/
public void setName(String newName)
{
name = newName;
}

/**
* Accessor method for climber's age.
*/
public int getAge()
{
return age;

}

/**
* Set the climber's age.
*/
public void setAge(int newAge)
{
age = newAge;

}

/**
* Set the climer's gender.
*/
public String getGender()
{
return gender;
}

/**
* Accessor method for climber's gender.
*/
public void getGender(String newGender)
{
gender = newGender;

}

public Mountain addMountain(Mountain mountain)
{
return mountain;
}

public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight)
{
double maxHeight = 1;
int index = 1;
for(int i = 0; i < mountainHeight.length; i++) {
if(mountainHeight[i].getHeight()>maxHeight) {
index = i;
}
}




}
}

山地等级:

/**
* Write a description of class Mountain here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Mountain
{
// Instance variables.
private double height;
private String name;

/**
* Constructor for objects of class Mountain
*/
public Mountain(String mountainName, double mountainHeight)
{
// Initialise instance variables
name = mountainName;
height = mountainHeight;
}

/**
* Accessor method for mountain name.
*/
public String getName()
{
return name;
}

/**
* Set the mountain name.
*/
public void setName(String mountainName)
{
name = mountainName;
}

/**
* Accessor method for mountain height.
*/
public double getHeight()
{
// put your code here
return height;
}

/**
* Set the mountain height.
*/
public void setHeight(double newHeight)
{
height = mountainHeight;
}
}

我尝试返回此值,但如您所见,它不完整。

提前致谢!

最佳答案

更改:

//This does absolutely nothing. It takes a parameter and returns it.
public Mountain addMountain(Mountain mountain) {
return mountain;
}

public ArrayList<Mountain> getHighestMountain(Mountain mountainHeight) {
double maxHeight = 1;
int index = 1;
for(int i = 0; i < mountainHeight.length; i++) {
if(mountainHeight[i].getHeight()>maxHeight) {
index = i;
}
}
}

致:

//This adds the parameter mountain to the list of mountains for this climber
public void addMountain(Mountain mountain) {
mountains.add(mountain);
}

//This loops over the mountains for this climber and returns the highest one.
public Mountain getHighestMountain() {
Mountain highestMountain = null;
for(int i = 0; i < mountains.size(); i++) {
if (highestMountain == null) {
highestMountain = mountains.get(i);
}
else if(mountains.get(i).getHeight() > highestMountain.getHeight()) {
highestMountain = mounts.get(i);
}
}
return highestMountain;
}

您的代码示例表明缺乏对字段和变量、参数和返回类型等基本概念的理解。我建议阅读语言基础知识 here

关于java - 需要比较 ArrayList 中的对象以返回最高值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47096522/

24 4 0