【问题标题】:Correcting incrementally increasing array values within while bounds在 while 范围内更正递增的数组值
【发布时间】:2020-10-19 07:03:04
【问题描述】:

我正在尝试编写类似于孩子们在篮球各个方面的进步技能的代码:传球、投篮和盘带。 我制作了一个数组,其中每一行代表一个孩子,每一列代表一个技能。 每周(循环)我让它打印每个玩家的累积技能(总结每个孩子的技能)。 问题: 每周(循环),每项技能都应相对于先前的技能水平增加增量。如果技能

package CumulativePractice;

public class Main {

    public static void main(String[] args)
    {
    int [][] kids =
            {
                    {2, 1, 0},
                    {0, 3, 4},
                    {1, 2, 3}
            };
    int sum = 0;
    int rows, cols, sumRow, sumCols;
    rows = kids.length;
    cols = kids[0].length;


    for(int i = 0; i <= rows; i++)
    {
        sumRow = 0;
        for (int j = 0; j < cols; j++)
        {
            sumRow = sumRow + kids[i][j];

            if (kids[i][j] < 10){
                kids[i][j] += 3;

            else if (kids[i][j] >= 10 && kids[i][j] < 20);
                kids[i][j] += 2;

            else if (kids[i][j] >= 20 && kids[i][j]> 30);
                kids[i][j] += 1;

            }

        }
        System.out.println("For week " + i + ":");

        System.out.println("Kid " + (i+1) + " total score: " + sumRow);

    }

    }
}

干杯

【问题讨论】:

  • i &lt;= rows 因为您使用i 作为索引,那么当它等于rows 时,它会超过元素的数量
  • 另外,请查看 if-else 语句中的大括号。这些也需要修复。您在 if 子句之后打开了大括号,但是您没有在 else if 之前关闭它。此外,您通过将 ; 放在末尾删除了 else if 子句的功能。
  • 嘿,您的if else-if 缩进中有错误。

标签: java arrays loops while-loop


【解决方案1】:

在您的代码中,如果每一行代表一个孩子,那么您为什么将其用作周迭代器? 我的意思是您正在使用以下循环来打印几周的进度:

    for(int i = 0; i <= rows; i++)

但您将行分配给孩子总数:

    rows = kids.length;

我认为,以下是您需要的(在更正逻辑和错误后):

public class Main {

    public static void main(String[] args)
    {
        int [][] kids =
                {
                        {2, 1, 0},
                        {0, 3, 4},
                        {1, 2, 3}
                };
        int sum = 0;
        int rows, cols, sumRow, sumCols;
        rows = kids.length;
        cols = kids[0].length;
        int total_weeks = 4;  // Let say we have 4 weeks
        int current_week = 1;
        
        while(current_week <= total_weeks)
        {
            System.out.println("\n\nFor week " + current_week + ":\n\n");
    
            for(int i = 0; i < rows; i++) // for each kid
            {
                sumRow = 0;
                for (int j = 0; j < cols; j++) // for each skill
                {
                    sumRow = sumRow + kids[i][j]; // get total of current skills
        
                    // Now add the skills for current week
                    if (kids[i][j] < 10)
                        kids[i][j] += 3;
        
                    else if (kids[i][j] >= 10 && kids[i][j] < 20)
                        kids[i][j] += 2;
        
                    else if (kids[i][j] >= 20 && kids[i][j]> 30)
                        kids[i][j] += 1;
        
                }
                   System.out.println("Kid " + (i+1) + " total score: " + sumRow);
            }
            current_week++; // Move to next week
    
        }
    }
}

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 2020-08-06
    • 1970-01-01
    • 2021-11-03
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多