我之前發佈過關於這個任務的內容,而且我有一個程序,除了一件事情之外,我幾乎可以處理這個程序。該程序向用戶詢問他們想要打印什麼文件,他們想要平鋪多少次以及他們想要平鋪多少次。我的程序將文件讀入一個2d數組,然後它應該將其平鋪。我使用三個for循環來嘗試打印它,它正在打印正確的數量,但它只是垂直打印。我試圖放入一個空白的println,看看它是否能夠正確打印,但它不工作。有人有主意嗎?下面是存儲txt文件到二維陣列和方法是應該平鋪部分代碼是:根據用戶輸入多次打印二維數組
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();
}
}
}
}
什麼是imageHeight和imageWidth以及它爲什麼是零?我想你正在創建一個大小爲[0] [0]的數組。 –