gpt4 book ai didi

java - 找不到代码有什么问题。 ArrayIndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-30 03:07:14 24 4
gpt4 key购买 nike

所以,我有这个作业,代码做了它应该做的事情,但在某些情况下程序崩溃了。我已经经历了几个小时了,但我就是找不到解决方法。我知道出了什么问题,但我无法修复它。只是不知道怎么办。问题似乎出在 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();
}
}
}

最佳答案

您的指数已关闭

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 ??
}
}

关于java - 找不到代码有什么问题。 ArrayIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451072/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com