根据用户输入多次打印二维数组

问题描述:

我之前发布过关于这个任务的内容,而且我有一个程序,除了一件事情之外,我几乎可以处理这个程序。该程序向用户询问他们想要打印什么文件,他们想要平铺多少次以及他们想要平铺多少次。我的程序将文件读​​入一个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]的数组。 –

直到要结束线(因为有不使用的println()没有更多的瓷砖在当前作品的右侧)。

如果您需要打印两个相邻的拼贴,则需要先打印第一个拼贴的一行,然后重新打印同一行,直到您在结束该行(使用println)之前有足够的拼贴数量。

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

基本上,我要去的地方是,切换缓冲区的循环,并使用循环切换。对于缓冲区中的每一行,重复它的跨越块数,然后用换行符结束行或使用println(“”);

+0

谢谢!我终于解决了它,你说的很有帮助! – Christina