2013-09-27 142 views
0

我之前發佈過關於這個任務的內容,而且我有一個程序,除了一件事情之外,我幾乎可以處理這個程序。該程序向用戶詢問他們想要打印什麼文件,他們想要平鋪多少次以及他們想要平鋪多少次。我的程序將文件讀​​入一個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(); 
      } 
     } 
    } 

} 
+0

什麼是imageHeight和imageWidth以及它爲什麼是零?我想你正在創建一個大小爲[0] [0]的數組。 –

回答

0

直到要結束線(因爲有不使用的println()沒有更多的瓷磚在當前作品的右側)。

如果您需要打印兩個相鄰的拼貼,則需要先打印第一個拼貼的一行,然後重新打印同一行,直到您在結束該行(使用println)之前有足夠的拼貼數量。

對於三個瓷磚寬度,您將在結束該行之前打印相同的線條三次。

基本上,我要去的地方是,切換緩衝區的循環,並使用循環切換。對於緩衝區中的每一行,重複它的跨越塊數,然後用換行符結束行或使用println(「」);

+0

謝謝!我終於解決了它,你說的很有幫助! – Christina