0

该计划的简介:该计划跟踪拥有航空公司会员卡的人以及他们每周收集的积分数。(week1, 2, 3, 4) 信息存储在数组中,然后在需要时,可以通过按“listButton”来输出。

我知道如何从数组中获取一个值并简单地输出它,但不确定如何使用循环来做到这一点。请参阅“totalPointsButton”下的问题区域

public class AirlineCardsView extends FrameView {

    class airline {
        String lastName, firstName;
        int week1, week2, week3, week4;

        airline (int _week1, int _week2, int _week3, int _week4, String _lastName, String _firstName) {
            week1 = _week1;
            week2 = _week2;
            week3 = _week3;
            week4 = _week4;
            lastName = _lastName;
            firstName = _firstName;
        }
    }

    /** Define the ArrayList */
    ArrayList <airline> members = new ArrayList <airline>();

    public AirlineCardsView(SingleFrameApplication app) {
        //GUI stuff
    }// </editor-fold>

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          

        String lastName, firstName;
        int week1, week2, week3, week4;

        week1 = Integer.parseInt(weekOneField.getText());
        week2 = Integer.parseInt(weekTwoField.getText());
        week3 = Integer.parseInt(weekThreeField.getText());
        week4 = Integer.parseInt(weekFourField.getText());
        lastName = lastNameField.getText();
        firstName = firstNameField.getText();

        airline c = new airline(week1, week2, week3, week4, firstName, lastName);
        members.add(c);
    }                                         

    private void listButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           

        String temp = "";

        for (int x=0; x<=members.size()-1; x++) {
            temp = temp + members.get(x).firstName + " "
                    + members.get(x).lastName + ": "
                    + members.get(x).week1 + " "
                    + members.get(x).week2 + " "
                    + members.get(x).week3 + " "
                    + members.get(x).week4 + "\n";
        }
        memberListTArea.setText(temp);

    }             

在这里,我不确定如何使用存储在相应成员的数组中的相同值来初始化值 week1、week2、week3、week4(对于 int totalPoints)。

    private void totalPointsButtonActionPerformed(java.awt.event.ActionEvent evt) {

        int week1, week2, week3, week4; 
        String lastName, firstName;

       String points = "";

        for (int j = 0; j < members.size()-1; j++) {
 //this line checks the inputted name to see if it matches any stored in array.
                if (members.get(j).lastName.equals(lastNameField.getText())) {
                    int totalPoints = week1 + week2 + week3 + week4; //then adds total points
                    }
                }

        }
4

2 回答 2

1

我希望我正确理解了这个问题......

  • 首先,这条线有问题:

    for (int j = 0; j < members.size()-1; j++) {
    

您应该删除-1或使用<=. 否则你不会到达最后一个元素。

  • 可能会if (members.get(j).lastName.equals(lastNameField.getText()))抛出 NPE。(members.get(j).lastName可能为空)此方法中定义的变量(week1-4,以及姓氏、名字)也没有多大意义。

  • int totalPoints在 if 块中声明,这意味着它不会在 if 块之外看到。如果你所有的逻辑都在 if 块中,这没什么大不了的。

  • 你可以试试:

    Airline al = null;
    int totalPoints;
    for (int j = 0; j < members.size(); j++) {
        al = members.get(j);
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
    

更好:

    int totalPoints;
    for (Airline al:members) {
        if (al.lastName.equals(lastNameField.getText())) {
            totalPoints = al.week1 + al.week2 + al.week3 + al.week4;
        }
    }
  • 为了使代码看起来更好,您可以考虑在您的Airline课程中添加 getter/setter。还添加了一个方法,int getTotalPoints()返回4周的总和。那么你不必在课堂外总结它。
于 2013-05-02T21:45:12.607 回答
0

您应该将 get 和 set 方法添加到您的航空公司舱位。因此,当您在数组列表中存储航空公司的对象时,您可以使用“members.get(j).week1、members.get(j).week2 等”获取它们的值

例如,像这样创建您的航空公司舱位。

public class airline {
    String lastName; 
    String firstName;
    int week1;
    int week2;
    int week3;
    int week4;

    public airline (int week1, int week2, int week3, int week4, String lastName, String firstName) {
        week1 = this.week1;
        week2 = this.week2;
        week3 = this.week3;
        week4 = this.week4;
        lastName = this.lastName;
        firstName = this.firstName;
    }

    public int getWeek1(){
        return week1;
    }

    public void setWeek1(int newWeek1){
        week1 = newWeek1;
    }

    public int getWeek2(){
        return week2;
    }

    public void setWeek2(int newWeek2){
        week2 = newWeek2;
    }

    public int getWeek3(){
        return week3;
    }

    public void setWeek3(int newWeek3){
        week3 = newWeek3;
    }

    public int getWeek4(){
        return week4;
    }

    public void setWeek4(int newWeek4){
        week4 = newWeek4;
    }

    public String getFirstName(){
        return firstName;
    }

    public void setFirstName(String newFirstName){
        firstName = newFirstName;
    }

    public String getLastName(){
        return lastName;
    }

    public void setLastName(String newLastName){
        lastName = newLastName;
    }

    public String toString(){
        return getWeek1() + " " + getWeek2() + " " + getWeek3() + " " + getWeek4() + getFirstName() + " " + getLastName();
    }
}
于 2013-05-02T21:45:11.640 回答