之前我发过关于这个任务的文章,除了一件事之外,我还有很多工作。该程序询问用户他们想要打印什么文件,他们想要平铺多少次以及他们想要将其平铺多少次。我的程序将文件读入2d数组,然后它应该将其平铺。我正在使用三个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();
}
}
}
}
答案 0 :(得分:0)
在你想要结束这条线之前不要使用println()(因为当前部分的右边没有更多的图块)。
如果您需要彼此相邻打印两个图块,则需要打印第一个图块的一行,然后再次打印同一行,直到您在结束该行之前具有正确数量的图块(使用println)。 / p>
对于三个瓷砖宽度,您将在结束该行之前三次打印相同的行。
基本上我要去的地方是,切换缓冲区的循环,并使用tile的循环。对于缓冲区中的每一行,重复它以获得平铺的数量,然后用换行符结束该行或使用println(“”);