所以,我是编程新手,我有这个练习,我必须阅读 int[][]
根据试图成为俱乐部成员(member)的人的年龄和残障程度进行排列,分为高级和公开两个类别。
我的工作是读取 example [[45, 12],[55,21],[19, -2]]
的数组其中第一个整数是年龄,第二个整数是障碍级别。如果年龄至少为55
并且差点级别高于7
那么该人将获得高级成员(member)资格,否则他将获得开放成员(member)资格。我的想法是查看int[][]
作为矩阵并将两个数字(年龄和级别)相加,如果数字高于 62,我会将其分类为高级,否则分类为开放。
我的方法如下所示:
public class montecarlo {
static String[] openOrSenior(int[][] a) {
int i, j, sum;
String[] abo = new String[a[0].length];
for (i = 0; i < a.length; i++)
for (j = 0; j < a[0].length; j++ ) {
sum = 0;
int x = a[i][j];
sum = sum + x;
if (sum > 62)
abo[i] = "Senior";
else
abo[i] = "Open"; //this would be line 12
}
return abo;
}
public static void main(String[] args) {
int [][] a = {{42, 12},{55, 21},{19,-2}};
String[] x = openOrSenior(a); //this would be line 20
Out.print(x); //here was to see what i'd get if i let it run
}
}
这是我得到的错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at montecarlo.openOrSenior(montecarlo.java:12) at montecarlo.main(montecarlo.java:20)
我真的很感激一些帮助。
最佳答案
这是一个更简单的版本,修复了语法错误。
public class montecarlo {
static String[] openOrSenior(int[][] a) {
String[] abo = new String[a.length]; // you had a[0] here instead of a
for (int i = 0; i < a.length; i++) {
if (a[i][0] >= 55 && a[i][1] > 7) {
abo[i] = "Senior";
} else {
abo[i] = "Open";
}
}
return abo;
}
}
关于java - java中多个int[][]数组转为single[]字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47265690/