简体   繁体   English

为什么我的数组未正确填充?

[英]Why isn't my array filled correctly?

I am writing code for a game in Java. 我正在用Java编写游戏代码。 Specificly I'm working on the level creation using a character array filled by characters from a .txt file. 具体来说,我正在使用由.txt文件中的字符填充的字符数组进行关卡创建。 My problem is that the array is not filled as it should be and the final line remains empty. 我的问题是数组未按预期填充,并且最后一行保持为空。 I can't work this out so any kind of help will be gladly accepted, here's the problematic block of code : 我无法解决这个问题,因此很乐意接受任何帮助,这是有问题的代码块:

public static void main(String[] args) throws IOException{

char background[][] = new char [14][20];

try {

    FileInputStream fileinput = new FileInputStream("background.txt");
        int r;
        for(int i=0;i<=13;i++){
            for(int j=0;j<19;j++){
                while((r = fileinput.read()) != -1){
                    char c = (char) r;
                    background[i][j] = c;
                    break;
                }
            }
        }
        fileinput.close();
    }

    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (int i=0;i<=13;i++){
        for(int j=0;j<=19;j++){
            System.out.print(background[i][j]);
        }
    }
}

Also the code as a whole can be found in the following link: http://pastebin.com/HtwMpsjm Here's the .txt file too! 整个代码也可以在下面的链接中找到: http : //pastebin.com/HtwMpsjm这也是.txt文件!

You accidentally one of your conditions, i've commented on the changed line. 您不小心遇到了其中一种情况,我已对更改的行发表了评论。

As someone has mentioned in the comments, you might find it beneficial to treat your loops conditions as for(int i=0;i<20;i++) rather than for(int i=0i<=19;i++) it makes the code a little more readable. 正如评论中提到的那样,您可能会发现将循环条件视为for(int i=0;i<20;i++)而不是for(int i=0i<=19;i++)会有所帮助,这会使代码更具可读性。

public static void main(String[] args) throws IOException{

char background[][] = new char [14][20];

try {

    FileInputStream fileinput = new FileInputStream("background.txt");
        int r;
        for(int i=0;i<=13;i++){
            for(int j=0;j<=19;j++){//<<THIS LINE WAS CHANGED
                while((r = fileinput.read()) != -1){
                    char c = (char) r;
                    background[i][j] = c;
                    break;
                }
            }
        }
        fileinput.close();
    }

    catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    for (int i=0;i<=13;i++){
        for(int j=0;j<=19;j++){
            System.out.print(background[i][j]);
        }
    }
}

Why do we need multiple for loops here. 为什么我们在这里需要多个for循环。 If you want to read characters from a file you could use buffered reader and the this matrix could be created in one line of code like while((bufferedReader.read(background[i]) != -1) && (++i < 14)){ } . 如果要从文件中读取字符,则可以使用缓冲读取器,并且可以在一行代码中创建此矩阵,例如while((bufferedReader.read(background[i]) != -1) && (++i < 14)){ } Also you are using a while loop to read one char and then an unconditional break statment inside is not a good practice (in my opinion). 另外,您正在使用while循环读取一个字符,然后在内部无条件的中断语句不是一个好习惯(我认为)。 Try 尝试

public static void main(String[] args) throws IOException {

    char background[][] = new char[14][20];
    try {
        FileReader fileReader = new FileReader("background.txt");
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        int i=0;
        while((bufferedReader.read(background[i]) != -1) && (++i < 14)){ } // This like created your 2D array

        bufferedReader.close();
        fileReader.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    for (int i = 0; i <= 13; i++) {
        for (int j = 0; j <= 19; j++) {
            System.out.print(background[i][j]);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM