错误:非法启动表达式static char [] [] spielfeld = new char [arraylaenge] [arraybreite];

时间:2016-12-03 10:37:22

标签: java arrays illegalstateexception

我在Java中实现了推箱子游戏。我从.txt文件中读取了游戏字段(Spielfeld)。现在我在从.txt文件中获取维度后尝试创建一个char数组。

问题是我在编译时遇到以下错误:

  

Pflichtaufgabe24.java:151:错误:表达式静态非法启动   char [] [] spielfeld = new char [arraylaenge] [arraybreite];

到目前为止,这是我的代码:

//author: Safwen Ben Said

import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Pflichtaufgabe24 {
//C:\Users\safwen\Desktop\Sokoban
private static final String FILENAME      = "C:\\Users\\safwen\\Desktop\\Sokoban\\map.txt";
static char[][] safwensspielfeld = new char[4][8]; // Array 2-dimentionnel déclaré mais non initialisé



    static int aktuelles_x = 0;
    static int aktuelles_y = 0;

    static int arraylaenge = 0;
static int arraybreite = 0;

    public static void spielfeldausgeben(char[][] spielfeld, int anzahlzeilen, int anzahlspalten) {

        for (int k = 0; k < anzahlzeilen; k++) {
            for (int j = 0; j < anzahlspalten; j++) {
                if (j == 0) {
                    System.out.println(' ');
                }

                System.out.print(spielfeld[k][j]);

            }
        }
    }

    public static void move_up() {
       System.out.println(' ');
        if (aktuelles_x != 0) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_x = aktuelles_x - 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();

    }

    public static void move_down() {
        System.out.println(' ');
        if (aktuelles_x != 3) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_x = aktuelles_x + 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();
    }

    public static void move_right() {
        System.out.println(' ');
        if (aktuelles_y != 7) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_y = aktuelles_y + 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();
    }

    public static void move_left() {
        System.out.println(' ');
        if (aktuelles_y != 0) {
            safwensspielfeld[aktuelles_x][aktuelles_y] = '.';
            aktuelles_y = aktuelles_y - 1;
            safwensspielfeld[aktuelles_x][aktuelles_y] = 'P';
        }
        spielfeldausgeben();

    }

    public static void exit() {
        System.out.println(' ');
        System.out.println("das Spiel wird auf Ihren Wunsch beendet.");
    }

    public static void main(String[] args) {
        Scanner safwens_scanner = new Scanner(System.in);
        String eingabe;


        BufferedReader br = null;
        FileReader fr = null;

            BufferedReader br2 = null;
        FileReader fr2 = null;

        try {

        fr = new FileReader(FILENAME);
        br = new BufferedReader(fr);

            String sCurrentLine;
            int iCurrentLine = 0;

            br = new BufferedReader(new FileReader(FILENAME));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
                iCurrentLine ++;
                if (sCurrentLine.length() > arraybreite){
                    arraybreite = sCurrentLine.length();
                }
                System.out.println(arraybreite); //Safwen´s  little check while programming (not part of the final solution)
                arraylaenge++;




            }

        } catch (IOException e) {

            e.printStackTrace();

        } finally {

            try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }
    static char[][] spielfeld = new char[arraylaenge][arraybreite];

    try {

        fr2 = new FileReader(FILENAME);
        br2 = new BufferedReader(fr);

        String sCurrentLine2;
        int iCurrentLine2 = 0;

        br2 = new BufferedReader(new FileReader(FILENAME));

    while ((sCurrentLine2 = br2.readLine()) != null) {



             for (int z = 0; z < sCurrentLine2.length(); z++){ 

            spielfeld[iCurrentLine2][z] = sCurrentLine2.charAt(z);
             }



        }

    } catch (IOException e) {

        e.printStackTrace();

    } finally {

        try {

            if (br != null)
                br.close();

            if (fr != null)
                fr.close();

        } catch (IOException ex) {

            ex.printStackTrace();

        }

    }
    spielfeldausgeben(spielfeld, arraylaenge, arraybreite);

    }

 }

如果有任何提示或帮助,我将不胜感激。

PS:这是我的map.txt文件

#####
#@$.#
#####

PS 2:

 # : wall @:  player
 + : player on target field 
 $ : box
 * : box on target field 
 . : target field 
 ´´ : field

0 个答案:

没有答案