关闭。这个问题需要细节或清晰。它目前不接受答案。
**想改进这个问题吗?**通过编辑这个帖子来添加细节并澄清问题。
三年前关门了。
改进这个问题
我是java新手,正在使用bluej。
课程背景概述。
我正在尝试创建一个俱乐部数据库(arraylist,而不是实际的数据库),用户可以在其中添加一个新的登山者(姓名、年龄、性别)以及他们爬过的山(姓名、高度)到arraylist。
我创建了攀岩类和山地类。我还为登山者创建了一个arraylist,可以添加到这个数组中。我的问题是。。如何将登山者添加到登山者阵列列表中,同时添加他们攀登过的山峰及其高度?
添加登山者的方法需要同时访问登山者和登山者类?我需要把山地班的田野变成登山者吗?
虽然解决问题的代码是赞赏的,如果我是在正确的方向,使我可以理解它更多的帮助!
谢谢。
import java.util.ArrayList;
import java.util.Scanner;
/**
* Write a description of class ClubStats here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ClubStats
{
// An ArrayList for storing climber details.
private ArrayList<Climber> climbers;
/**
* Constructor for objects of class ClubStats
*/
public ClubStats()
{
// 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 displayList()
{
for (int item = 0; item<climbers.size();
item++) {
Climber climber = climbers.get(item);
System.out.println(climber.getName() + (" ") + climber.getAge() + (" ")
+ climber.getGender());
}
}
}
public class Climber
{
// Instance variables.
// The climber name.
private String name;
// The climber age
private int age;
// The climber gender.
private String gender;
/**
* Constructor for objects of class Climber
*/
public Climber (String newName, int newAge, String newGender)
{
// Initialise instance variables.
name = newName;
age = newAge;
gender = newGender;
}
/**
* 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 class Mountain
{
// Instance variables.
private double height;
private String name;
/**
* Constructor for objects of class Mountain
*/
public Mountain(String mName, double mHeight)
{
// Initialise instance variables
name = mName;
height = mHeight;
}
/**
* Accessor method for mountain name.
*/
public String getName()
{
return name;
}
/**
* Set the mountain name.
*/
public void setName(String newName)
{
name = newName;
}
/**
* Accessor method for mountain height.
*/
public double getHeight()
{
// put your code here
return height;
}
/**
* Set the mountain height.
*/
public void setHeight(double newHeight)
{
height = newHeight;
}
}
1条答案
按热度按时间bweufnob1#
一
Climber
可以包含List<Mountain>
的Mountain
a这是什么Climber
攀爬。您可以添加
addMountain(Mountain mountain)
方法Climber
类,该类将添加Mountain
为了这个List
.可以添加getter方法
List<Mountain> getMountainList()
那会回报你的List
.这样,每个
Climber
您可以访问Mountain
这是什么Climber
已经攀升了。注意,如果多个
Climber
我们也爬上了同样的台阶Mountain
,它们都可以包含对同一对象的引用Mountain
示例List<Mountain>
(即无需创建Mountain
每个示例Climber
).