Java “问题”;“获取最低值”;二维数组中的方法
我写的这个程序有点问题。我必须做一个总的,平均的,最高的和最低的。问题是,除了最后一个方法getLowest之外,其他方法都在工作,它给了我错误“Exception in thread”main java.lang.ArrayIndexOutOfBoundsException:索引4超出长度4的界限 在TwoDim.getlower(TwoDim.java:66) 在TwoDim.main(TwoDim.java:15) " 此外,它在Eclipse中表示“不可访问的代码”,并告诉我去掉末尾的“return low;”行。我真的不知道我做错了什么,它应该工作得很好,但它不是。任何帮助都将不胜感激Java “问题”;“获取最低值”;二维数组中的方法,java,Java,我写的这个程序有点问题。我必须做一个总的,平均的,最高的和最低的。问题是,除了最后一个方法getLowest之外,其他方法都在工作,它给了我错误“Exception in thread”main java.lang.ArrayIndexOutOfBoundsException:索引4超出长度4的界限 在TwoDim.getlower(TwoDim.java:66) 在TwoDim.main(TwoDim.java:15) " 此外,它在Eclipse中表示“不可访问的代码”,并告诉我去掉末尾的“
import java.io.*;
class TwoDim
{
//main function
public static void main (String []arg)
{
int [][]list= {{1,2,3,4},{5,6,7,8},{9,10,11,12}};
//Function call and display data returned
System.out.println("Total:"+getTotal(list));
System.out.println("Average:"+getAverage(list));
System.out.println("Row 2 value:"+getRowTotal(list,2));
System.out.println("Column 3 Total :"+getColumnTotal(list,3));
System.out.println("Highest value in row 1 is :"+getHighest(list,1));
System.out.println("Lowest value in row 2 is :"+getLowest(list,2));
//Exit program
System.exit(0);
}
public static int getTotal(int [][]numbers)
{
int tot=0;
for(int row=0;row<numbers.length;row++)
for(int col=0;col<numbers[row].length;col++)
tot+=numbers[row][col];
return tot;
}
public static double getAverage(int [][]numbers)
{
double avg;
avg= (double)(getTotal(numbers)/(12));
return avg;
}
public static int getRowTotal(int [][]numbers, int index)
{
int tot=0;
for(int col=0;col<4;col++)
tot+=numbers[index][col];
return tot;
}
public static int getColumnTotal (int [][]numbers, int index)
{
int tot=0;
for(int row=0;row<numbers.length;row++)
tot+=numbers[row][index];
return tot;
}
public static int getHighest(int [][]numbers,int row)
{
int high=numbers[row][0];
for(int i=1;i<4;i++)
if(numbers[row][i]>high)
high=numbers[row][i];
return high;
}
public static int getLowest(int [][]numbers,int row)
{
int low=numbers[row][0];
for(int i=1;1<4;i++)
if(numbers[row][i]<low)
low=numbers[row][i];
return low;
}
}
import java.io.*;
二等舱
{
//主要功能
公共静态void main(字符串[]arg)
{
int[]list={{1,2,3,4},{5,6,7,8},{9,10,11,12};
//返回的函数调用和显示数据
System.out.println(“总计:+getTotal(列表));
System.out.println(“平均值:+getAverage(列表));
System.out.println(“第2行值:”+getRowTotal(列表,2));
System.out.println(“第3列总计:+getColumnTotal(列表,3));
System.out.println(“第1行中的最高值为:”+getHighest(list,1));
System.out.println(“第2行中的最小值为:”+GetLost(列表,2));
//退出程序
系统出口(0);
}
公共静态int getTotal(int[][]个数字)
{
int-tot=0;
for(int row=0;row在getloost
函数中,for循环仅在1>=4
时存在,但这永远不会是真的。您应该使用i<4
而不是1<4
在for循环的每次迭代中,它都会检查:
- 数字[row][1]在
getloost
函数中,for循环仅在1>=4
时存在,但这永远不会是真的。您应该使用i<4
而不是1<4
在for循环的每次迭代中,它都会检查:
- 数字[行][1]看看这个:
for(int i=1;1<4;i++)
看看这个:
for(int i=1;1<4;i++)
旁注:不要使用像4
这样的幻数。请使用length
成员。例如:for(int i=1;i
由于这种情况1的循环是无限的,因此,您在for中有一个输入错误(int i=1;1侧注:不要使用像4
这样的幻数。请使用length
成员。例如:for(int i=1;i
由于这种情况下的循环是无限的1无论如何,您在处有一个输入错误(int i=1;1ohhh我一定是不小心输入了1而不是i,这是一个多么愚蠢的错误,哈哈。现在效果很好,谢谢。@user13932723没问题!还有-一些人提到了上面提到的更好的编码实践,如果你想使用你的代码样式,你可以尝试一下。哦,我一定是不小心输入了1而不是i,这是多么愚蠢的错误哈哈。效果很好现在,谢谢。@user13932723没问题!还有-一些人提到了上面提到的更好的编码实践,如果您想使用您的代码样式,可以尝试一下。