用于循环的Java 2d数组

时间:2014-05-05 21:43:58

标签: java if-statement for-loop multidimensional-array

我正在试图弄清楚如何让用户拥有超过5000分 他们将获得额外的1000分。实际上,它为每个人打印相同数量的积分。 User1值:100,1000,1000,5000。 User2值:1,1,1,1。

当前价值:

用户1的总分为7100 用户2的总分为7100 预期价值:

用户1的总积分是7100奖励:1000。 用户2的总积分为4。

`public class AirlinePointsView extends FrameView {
    String[][] name = new String[10][2];
    Integer[][] points = new Integer[10][4];
    int rowNumber = 0;
    int userNumber = 0;
    public AirlinePointsView(SingleFrameApplication app) {
    }

    private void btnAddMouseClicked(java.awt.event.MouseEvent evt) {                                    
        name[rowNumber][0] = firstName.getText();
        name[rowNumber][1] = lastName.getText();

        points[rowNumber][0] = Integer.parseInt(week1.getText());
        points[rowNumber][1] = Integer.parseInt(week2.getText());
        points[rowNumber][2] = Integer.parseInt(week3.getText());
        points[rowNumber][3] = Integer.parseInt(week4.getText());
        rowNumber ++;
    }                                   

    private void btnListMouseClicked(java.awt.event.MouseEvent evt) {                                     
        String outputStr = "";
        for(int i=0; i < rowNumber; i++){
            outputStr += name[i][0] + " " + name[i][1] + " " +  points[i][0] + " " + points[i][1] + " " + points[i][2] + " " + points[i][3] + "" + "\n";
        }outPut.setText(outputStr);
    }`                                   

The Total Button

`private void btnTotalMouseClicked(java.awt.event.MouseEvent evt) {                                      
        String userTotal = "";
        String userBonus = "";
        int total = (points[0][0] + points[0][1] + points[0][2] + points[0][3]);
        int bonus = 1000;
        for(int i=0; i < rowNumber; i++){
            userTotal += name[i][0] + "'s Total Points are: " + total + "\n";
            userBonus += name[i][0] + "'s Total Points are: " + total + " Bonus: " + bonus + "\n";
            if(total > 5000){
                outPut.setText(userTotal);
            }else{
                outPut.setText(userBonus);
            }
        }
    }                                     


}`

2 个答案:

答案 0 :(得分:1)

您只是计算第0行的总数。

试试这个:

private void btnTotalMouseClicked(java.awt.event.MouseEvent evt) {                                      
        String userTotal = "";
        String userBonus = "";

        int bonus = 1000;
        for(int i=0; i < rowNumber; i++){
            int total = (points[i][0] + points[i][1] + points[i][2] + points[i][3]);
            userTotal += name[i][0] + "'s Total Points are: " + total + "\n";
            userBonus += name[i][0] + "'s Total Points are: " + total + " Bonus: " + bonus + "\n";
                if(total > 5000){
                    outPut.setText(userBonus);                        
                }else{
                    outPut.setText(userTotal);
                }
        }
    }                                     

}

答案 1 :(得分:0)

Put&#34; total&#34;在for循环内部,以便为每个用户更新:

total  = (points[i][0] + points[i][1] + points[i][2] + points[i][3]);