该计划的简介:该计划跟踪拥有航空公司会员卡的人以及他们每周收集的积分数。(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
}
}
}