Błąd przy wyświetlaniu tablicy

0

Cześć nie wiem co jest grane testuje chcę wyświetlić tablicę i wywala błąd :

 run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
	at tester.Tester.main(Tester.java:67)
0  0  0  -1  0  0  0  -1  0  0  C:\Users\Tomek\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

kod:

 /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package tester;

import java.awt.Container;
import java.util.Random;
import javax.swing.JButton;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;

/**
 *
 * @author Tomek
 */
public class Tester {
    
 private JButton StartGameButton;
    private JButton Results;
    private JMenuBar Menu;
    private JMenu menuFile, menuHelp;
    private JMenuItem iNewGame, iInformations, iShowResult, iClose;
    private JButton[][] buttonsField;
    private JPanel p;
    private Container grid = new Container();
    private static int[][] gameFiled;
    private static int randomX, randomY;//do przechowywania wsp losowych min 
    
    public static void RandomPlace(){
        Random rand = new Random();
        randomX = rand.nextInt(10);
        randomY = rand.nextInt(10);
    }
    
    public static void MinesPlaces(){
        gameFiled = new int [10][10];
        int numbersOfMines = 10;
        while (numbersOfMines > 0){
            RandomPlace();
            if (gameFiled[randomX][randomY] == -1){
                MinesPlaces();
            }
            gameFiled[randomX][randomY] = -1;
            numbersOfMines--;
        }
        
    }
    
    public static void Tablica(){
        for(int i=0; i< gameFiled.length; i++){
            for(int j=0; i< gameFiled[i].length; j++)
                System.out.print(gameFiled[i][j]+"   ");
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
       
        RandomPlace();
        MinesPlaces();
        Tablica();
        
       
    }
        
    }
    


0

for(int j=0; i< gameFiled[i].length; j++) <-- tutaj masz zły warunek najprawdopodobniej

0

Może to jakiś błąd wielkością tablicy :/

0

ArrayIndexOutOfBoundsException informuje o próbie odwołania się do indeksu w tablicy, który nie istnieje np. jeżeli zadeklarujesz tablice w taki sposób

 int[] tab = new int[10];

czyli tworzysz tablice liczb (int) 10-elementową o indeksach (0, 1, 2, 3, 4, 5, 6, 7, 8, 9) a w programie próbujesz czegoś takiego:

public class Main {

    public static void main(String[] args) {
        int[] tab = new int[10];
        int index = 10;

        System.out.println(tab[index]);
    }
}

w efekcie otrzymasz błąd Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 czyli informacje, że próbujesz odwołać się do indeksu tablicy, który nie istnieje.

0

W takim razie gdzie u mnie wychodzę poza tablicę, bo nie widzę :/

0

Przecież @Aisekai napisał, tu wychodzisz poza tablicę

 for(int j=0; i< gameFiled[i].length; j++)

Powinno być j< gameFiled[i].length.

0

tak jak napisał @Aisekai masz błąd w warunku w pętli (czytaj co ludzie piszą do Ciebie) zamień

  public static void Tablica(){
        for(int i=0; i< gameFiled.length; i++){
            for(int j=0; i< gameFiled[i].length; j++) //<- tu jest błąd
                System.out.print(gameFiled[i][j]+"   ");
            System.out.println();
        }
    }

na

  public static void Tablica(){
        for(int i=0; i< gameFiled.length; i++){
            for(int j=0; j< gameFiled[i].length; j++) // zmieniłem i na j
                System.out.print(gameFiled[i][j]+"   ");
            System.out.println();
        }
    }

dodatkowo IDE informuje Cię w, której linii programu masz błąd at tester.Tester.main(Tester.java:67)

0

O Boże ślepy jestem... Dziękuje Panowie.

1 użytkowników online, w tym zalogowanych: 0, gości: 1