找不到代码有什么问题。 ArrayIndexOutOfBoundsException异常

时间:2015-12-24 10:12:32

标签: java arrays

所以,我有这个功课,代码做了它应该做的,但在某些情况下程序崩溃了。我已经经历了几个小时,我无法找到解决方法。我有点疑问什么是错的,但我无法解决它。只是不知道怎么做。问题似乎与y,z整数或soucetSloupce soucetRadku方法有关,不知道究竟问题在哪里。

package mazorku5;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class MazorKU5 {
    public static int[][] dejPole(int b, int a){
    int[][] pole = new int[b][a];

    for (int i = 0; i < pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            pole[i][j] = (int)(Math.random()*10);
        }
    }
    return pole;
}

public static void tiskPole(int [][] pole){
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++) {
            System.out.print (pole[i][j] + " ");
        }
    System.out.println(); 
    }
}

public static int soucetRadku(int[][]pole, int z){
    int suma=0;
    for (int j = 0; j < pole[z].length;j++){
        suma = suma + pole[z][j];
    }
    return suma;
}

public static int soucetSloupce(int[][]pole, int y){
    int suma=0;
    for (int j = 0; j < pole[y].length;j++){
        suma = suma + pole[j][y];
    }
    return suma;
}

public static float aritmPrumer(int[][]pole){
float suma=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            suma = suma + pole[i][j];
        }
    }
    return suma/(pole.length*pole[0].length);
}

public static int kolikX(int[][]pole, int x){
    int pocet=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            if (pole[i][j] == x)pocet++;
        }
    }
    return pocet;
}

public static int loser(int[][]pole, float prumer){
    int pocet=0;
    for (int i = 0; i<pole.length; i++){
        for (int j = 0; j < pole[i].length;j++){
            if (pole[i][j] < prumer)pocet++;
        }
    }
    return pocet;
}

public static void main(String[] args) {
    Random ran = new Random();
    Scanner sc = new Scanner(System.in);
    int i = 0;
    int znovu = 1;
    int z; 
    int y;
    int x;
    int loserP;
    float prumer;
    while (znovu==1) {
        System.out.println("Zadejte kolik ma mit pole sloupcu.");
        int a=sc.nextInt();
        while (a <= 0) {
            System.out.println("Zadejte kolik ma mit pole sloupcu.");
            a= sc.nextInt();
        }
        System.out.println("Zadejte kolik ma mit pole radku.");
        int b=sc.nextInt();
        while (b <= 0) {
            System.out.println("Zadejte kolik ma mit pole radku.");
            b= sc.nextInt();
        }
        int [][] pole = new int[b][a];
        pole= dejPole(b,a);
        tiskPole(pole);
        System.out.println("Radek?");
        z= sc.nextInt()-1;
        while (z > b || z < 0) {
            System.out.println("Radek?");
            z= sc.nextInt()-1;
        }
        System.out.println(soucetRadku(pole,z));
        System.out.println("Sloupec?");
        y= sc.nextInt()-1;
        while (y > a || y < 0) {
            System.out.println("Sloupec?");
            y= sc.nextInt()-1;
        }   
        System.out.println(soucetSloupce(pole,y));
        prumer=aritmPrumer(pole);
        System.out.println("Jake cislo hledame?");
        x = sc.nextInt();
        int pocetX=kolikX(pole,x);
        System.out.println(x + " mame: " + pocetX + ". ");
        System.out.println("Aritm. prumer je: " + prumer);
        loserP=loser(pole, prumer);
        System.out.println("Hodnot mensich nez prumer je: " + loserP);
        System.out.println("Znovu?(1)");
        znovu = sc.nextInt();
    }
}
}

1 个答案:

答案 0 :(得分:2)

您的指数已关闭

public static int soucetSloupce(int[][]pole, int y){
   int suma=0; for (int j = 0; j < pole[y].length;j++){ // pole.length??
    suma = suma + pole[j][y]; //  y,j ??
   }
 }