0

我之前发布过这个作业,除了一件事之外,我的程序几乎可以正常工作。该程序询问用户他们想要打印什么文件,他们想要平铺多少次以及他们想要平铺多少次。我的程序将文件读​​入二维数组,然后应该将其平铺。我正在使用三个 for 循环来尝试打印它并且它正在打印正确的数量,但是它只是垂直打印。我尝试放入一个空白的 println 以查看它是否可以正确打印,但它不起作用。有人有想法么?这是将 txt 文件存储到 2d 数组中的代码部分以及应该平铺它的方法:

import java.io.*;
import java.util.*;

class TileMap

{
    public static boolean yes;
    public static char response;
    public static int MAXSIDE = 100;
    public static Scanner scan = new Scanner(System.in);
    public static String fileName = "";
    public static int tilesAcross = 0;
    public static int tilesDown = 0;
    public static int imageHeight = 0;
    public static int imageWidth = 0;
    public static char userInput = 0;   
    static char [][] buffer = new char[MAXSIDE][MAXSIDE];
    static FileInputStream fstream = null;

public static void getImage()
{
    System.out.println("Getting Image...");

    try
    {   

        File file = new File(fileName);
        Scanner fstream = new Scanner(file);

        imageHeight = fstream.nextInt();
        imageWidth = fstream.nextInt();

        buffer = new char[imageHeight][imageWidth];
        int i = 0;

        while(fstream.hasNextLine())
        {
            String line = fstream.nextLine();

                for(int l = 0; l < line.length(); l++)
                {
                    buffer[i][l] = line.charAt(l);
                }
            i++;
        }

        /*
        for(int i = 0; i < imageHeight; i++)
        {
            String element = fstream.nextLine();
            for(int j = 0; j < imageWidth; j++)
            {
                buffer[i][j] = element.charAt(j);
            }
        }
        */


        fstream.close();
    }


    catch (Exception e)
    {
        System.err.println("Error: " + e.getMessage());
    }


}




public static void doTileJob ()
    {

    for(int m = 0; m < tilesDown; m++)
    {
        for (int n = 0; n < tilesAcross;n++)
        {   
            for(int i= 0; i < buffer.length; i++)
                {
                    int w = 0;
                    System.out.print(buffer[i]);
                    System.out.println(buffer[w]);
                    w++;
                    }
            System.out.println();
            }
        }
    }

}
4

1 回答 1

0

在您想要结束该行之前不要使用 println() (因为当前块的右侧没有更多的瓷砖)。

如果您需要打印两个相邻的图块,则需要打印第一个图块的一行,然后再次打印同一行,直到在结束该行之前您有正确数量的图块(使用 println)。

对于三个宽度的瓷砖,您将在结束该行之前将同一行打印三次。

基本上我要去的地方是,将缓冲区的循环与瓷砖的循环切换。对于缓冲区中的每一行,重复它的瓷砖数量,然后用换行符结束该行或使用 println("");

于 2013-09-27T04:45:21.437 回答