英文:
Correcting incrementally increasing array values within while bounds
问题
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 am trying to write a code resembling the progressing skill of kids in various aspects of basketball: Passing, Shooting, and Dribbling.
I've made an array where each row is supposed to represent a kid, and each column a skill.
Every week (loop) I have it printing the cumulative skill of each player (summing each kid's skills).
Problem:
Each week (loop), every skill should go up by increments relative to the prior skill level. If a skill is <10, it goes up by 3 per loop. Up by 2 if the skill is between 10 and 19, and by 1 if it is between 20 and 30, insinuating that it takes more time to improve skill scores as progress is made.
Skills max out at 30.
The while loops I've used are not effecting the array values, and I am not sure why.
I'd be appreciative if someone could you take a look and mention where I went wrong or explain corrections.
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);
}
}
}
Cheers
答案1
得分: 0
以下是翻译好的内容:
在您的代码中,如果每一行代表一个孩子,那为什么要将其用作星期迭代器呢?
我的意思是,您正在使用以下循环来打印星期进度:
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; // 假设我们有4周
int current_week = 1;
while(current_week <= total_weeks)
{
System.out.println("\n\n第 " + current_week + " 周:\n\n");
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("孩子 " + (i+1) + " 总分:" + sumRow);
}
current_week++; // 移动到下一周
}
}
}
希望能对您有所帮助 :)
英文:
In your code, if each row represents a kid, then why are you using it as a week iterator?
What I mean is that you are using following loop, to print weeks progress:
for(int i = 0; i <= rows; i++)
but instead you are assigning the rows to the total kids:
rows = kids.length;
I think, the following is what you need(after correction of logic & errors) :
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
}
}
}
Hope it helps
专注分享java语言的经验与见解,让所有开发者获益!
评论