“Get Lowest Value”方法在二维数组中出现问题。

huangapple go评论71阅读模式
英文:

Problem with "Get Lowest Value" Method in 2 Dimensional Array

问题

我正在编写的一个程序中遇到了一个问题。我需要实现一个 getTotal、Average、highest 和 lowest 方法。问题是,除了最后一个 getLowest 方法之外,其他都正常运行。getLowest 方法给了我一个错误,内容为:"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at TwoDim.getLowest(TwoDim.java:66)
at TwoDim.main(TwoDim.java:15)"
此外,Eclipse 还显示 "unreachable code",并建议我删除最后的 "return low;" 行。我真的不知道我在这里做错了什么,理应可以正常工作但实际上并没有。任何帮助将不胜感激。

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;
        
    }
}
英文:

I'm having an issue with this one program I'm writing. I have to do a getTotal, Average, highest and lowest. The thing is, everything is working except the last method, getLowest, which gives me the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4
at TwoDim.getLowest(TwoDim.java:66)
at TwoDim.main(TwoDim.java:15)
"
Additionally it says "unreachable code" in Eclipse and tells me to get rid of the "return low;" line at the end. I really don't know what I'm doing wrong here, it should work totally fine but it isn't. Any help would be appreciated.

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(&quot;Total:&quot;+getTotal(list));
System.out.println(&quot;Average:&quot;+getAverage(list));
System.out.println(&quot;Row 2 value:&quot;+getRowTotal(list,2));
System.out.println(&quot;Column 3 Total :&quot;+getColumnTotal(list,3));
System.out.println(&quot;Highest value in row 1 is :&quot;+getHighest(list,1));
System.out.println(&quot;Lowest value in row 2 is :&quot;+getLowest(list,2));
//Exit program
System.exit(0);
}
public static int getTotal(int [][]numbers)
{
int tot=0;
for(int row=0;row&lt;numbers.length;row++)
for(int col=0;col&lt;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&lt;4;col++)
tot+=numbers[index][col];
return tot;
}
public static int getColumnTotal (int [][]numbers, int index)
{
int tot=0;
for(int row=0;row&lt;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&lt;4;i++)
if(numbers[row][i]&gt;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&lt;4;i++) 
if(numbers[row][i]&lt;low)
low=numbers[row][i];
return low;
}
}

答案1

得分: 1

在你的 getLowest 函数中,你的 for 循环只有在 1 >= 4 时才会退出,但这永远不会成立。你应该使用 i < 4 代替 1 < 4

在 for 循环的每次迭代中,它在检查:

  • numbers[row][1] < low
  • numbers[row][2] < low
  • numbers[row][3] < low
  • numbers[row][4] < low
  • numbers[row][5] < low
  • numbers[row][6] < low
  • numbers[row][7] < low
  • numbers[row][8] < low
  • numbers[row][9] < low
  • ...
英文:

in your getLowest function, your for loop only exits when 1 &gt;= 4, but this will never be true. You should have i &lt; 4 instead of 1 &lt; 4

In every iteration of your for loop, it was checking:

  • numbers[row][1]<low
  • numbers[row][2]<low
  • numbers[row][3]<low
  • numbers[row][4]<low
  • numbers[row][5]<low
  • numbers[row][6]<low
  • numbers[row][7]<low
  • numbers[row][8]<low
  • numbers[row][9]<low
  • ...

答案2

得分: 1

看这个:

for(int i=1;1&lt;4;i++)

1 &lt; 4 总是为 true,所以你在这里有一个无限循环,直到 i == list[row].length,然后会得到 ArrayIndexOutOfBoundsException。你应该使用像 IntelliJ IDEA 这样的 IDE,这种问题会被编辑器标记出来。

class TwoDim {

    public static void main(String... arg) {
        int[][] list = {
            { 1, 2, 3, 4 },
            { 5, 6, 7, 8 },
            { 9, 10, 11, 12 }
        };

        System.out.println("Total: " + getTotal(list));
        System.out.format(Locale.ENGLISH, "Average: %.2f\n", 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));
    }

    public static int getTotal(int[][] list) {
        int sum = 0;

        for (int row = 0; row &lt; list.length; row++)
            for (int col = 0; col &lt; list[row].length; col++)
                sum += list[row][col];

        return sum;
    }

    public static double getAverage(int[][] list) {
        int sum = 0;
        int total = 0;

        for (int row = 0; row &lt; list.length; row++)
            for (int col = 0; col &lt; list[row].length; col++, total++)
                sum += list[row][col];

        return (double)sum / total;
    }

    public static int getRowTotal(int[][] list, int row) {
        int sum = 0;

        for (int col = 0; col &lt; list[row].length; col++)
            sum += list[row][col];

        return sum;
    }

    public static int getColumnTotal(int[][] list, int col) {
        int sum = 0;

        for (int row = 0; row &lt; list.length; row++)
            sum += list[row][col];

        return sum;
    }

    public static int getHighest(int[][] list, int row) {
        int high = Integer.MIN_VALUE;

        for (int col = 0; col &lt; list[row].length; col++)
            high = Math.max(high, list[row][col]);

        return high;
    }

    public static int getLowest(int[][] list, int row) {
        int low = Integer.MAX_VALUE;

        for (int col = 0; col &lt; list[row].length; col++)
            low = Math.min(low, list[row][col]);

        return low;
    }

}
英文:

Look at this:

for(int i=1;1&lt;4;i++)

1 &lt; 4 is always true, so you have infinite loop here until i == list[row].length and you get ArrayIndexOutOfBoundsException. You should use an IDE like IntelliJ IDEA where such problems are highlited by the editor.

class TwoDim {
public static void main(String... arg) {
int[][] list = {
{ 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
System.out.println(&quot;Total: &quot; + getTotal(list));
System.out.format(Locale.ENGLISH, &quot;Average: %.2f\n&quot;, getAverage(list));
System.out.println(&quot;Row 2 value: &quot; + getRowTotal(list, 2));
System.out.println(&quot;Column 3 Total: &quot; + getColumnTotal(list, 3));
System.out.println(&quot;Highest value in row 1 is: &quot; + getHighest(list, 1));
System.out.println(&quot;Lowest value in row 2 is: &quot; + getLowest(list, 2));
}
public static int getTotal(int[][] list) {
int sum = 0;
for (int row = 0; row &lt; list.length; row++)
for (int col = 0; col &lt; list[row].length; col++)
sum += list[row][col];
return sum;
}
public static double getAverage(int[][] list) {
int sum = 0;
int total = 0;
for (int row = 0; row &lt; list.length; row++)
for (int col = 0; col &lt; list[row].length; col++, total++)
sum += list[row][col];
return (double)sum / total;
}
public static int getRowTotal(int[][] list, int row) {
int sum = 0;
for (int col = 0; col &lt; list[row].length; col++)
sum += list[row][col];
return sum;
}
public static int getColumnTotal(int[][] list, int col) {
int sum = 0;
for (int row = 0; row &lt; list.length; row++)
sum += list[row][col];
return sum;
}
public static int getHighest(int[][] list, int row) {
int high = Integer.MIN_VALUE;
for (int col = 0; col &lt; list[row].length; col++)
high = Math.max(high, list[row][col]);
return high;
}
public static int getLowest(int[][] list, int row) {
int low = Integer.MAX_VALUE;
for (int col = 0; col &lt; list[row].length; col++)
low = Math.min(low, list[row][col]);
return low;
}
}

huangapple
  • 本文由 发表于 2020年10月24日 01:20:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64504612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定