Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我是Java新手,正在使用BlueJ。

程序上下文概述。

我正在尝试创建一个俱乐部数据库(阵列列表,而不是实际的数据库),用户可以在其中添加新的登山者(姓名,年龄,性别)以及他们已经爬过的那座山峰(名称,身高)到阵列列表中。

我创建了登山班和登山班。我还为登山者创建了一个ArrayList,可以将其添加到该数组中。我的问题是..如何将一个登山者添加到登山者ArrayList中,并能够同时添加他们已经爬过的那座山及其高度?

添加登山者的方法需要同时进入Climber和Mountain类?我是否需要将山区课程的字段传递给登山者?

尽管可以解决该问题的代码很受赞赏,但是如果以正确的方向向我展示我会有所帮助,那么我可以更好地理解它!

谢谢。

 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;
    }
}

最佳答案

Climber可以包含此List<Mountain>爬过的Mountain a的Climber

您可以将addMountain(Mountain mountain)方法添加到Climber类,这将向该Mountain添加List

您可以添加将返回该List<Mountain> getMountainList()的getter方法List

这样,对于每个Climber,您都可以访问该Mountain已爬升的Climber

请注意,如果多个Climber爬过相同的Mountain,则它们都可以在其Mountain中包含对同一List<Mountain>实例的引用(即,无需为每个Mountain实例创建该Climber实例的副本) )。

07-24 22:29