Problem mit der Methode "Get Lowest Value" im zweidimensionalen Array [geschlossen]
Ich habe ein Problem mit diesem einen Programm, das ich schreibe. Ich muss ein GetTotal, Average, Highest und Lowest machen. Die Sache ist, dass alles funktioniert, außer der letzten Methode, getLowest, die mir den Fehler "Ausnahme im Thread" main "gibt. Java.lang.ArrayIndexOutOfBoundsException: Index 4 außerhalb der Grenzen für Länge 4 bei TwoDim.getLowest (TwoDim.java:66) ) at TwoDim.main (TwoDim.java:15) "Zusätzlich steht in Eclipse" nicht erreichbarer Code "und ich soll das" return low "loswerden. Linie am Ende. Ich weiß wirklich nicht, was ich hier falsch mache, es sollte völlig gut funktionieren, ist es aber nicht. Jede Hilfe wäre dankbar.
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;
}
}
Antworten
In Ihrer getLowestFunktion wird Ihre for-Schleife nur dann beendet 1 >= 4, wenn dies der Fall ist. Dies wird jedoch niemals der Fall sein. Sie sollten i < 4statt1 < 4
Bei jeder Iteration Ihrer for-Schleife wurde Folgendes überprüft:
- Zahlen [Zeile] [1] <niedrig
- Zahlen [Zeile] [2] <niedrig
- Zahlen [Zeile] [3] <niedrig
- Zahlen [Zeile] [4] <niedrig
- Zahlen [Zeile] [5] <niedrig
- Zahlen [Zeile] [6] <niedrig
- Zahlen [Zeile] [7] <niedrig
- Zahlen [Zeile] [8] <niedrig
- Zahlen [Zeile] [9] <niedrig
- ...
Schau dir das an:
for(int i=1;1<4;i++)
1 < 4ist immer true, also hast du hier eine Endlosschleife bis i == list[row].lengthund du bekommst ArrayIndexOutOfBoundsException. Sie sollten eine IDE wie IntelliJ IDEA verwenden, bei der solche Probleme vom Editor hervorgehoben werden.
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 < list.length; row++)
for (int col = 0; col < 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 < list.length; row++)
for (int col = 0; col < 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 < 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 < 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 < 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 < list[row].length; col++)
low = Math.min(low, list[row][col]);
return low;
}
}