简体   繁体   English

根据用户输入多次打印2D阵列

[英]Printing a 2D array multiple times depending on user input

I posted about this assignment before, and I have the program pretty much working with the exception of one thing. 我之前发布过有关此作业的信息,除了一件事外,我的程序几乎可以正常工作。 The program asks the user what file they want to print, how many times they want to tile it across and how many times they want to tile it down. 该程序询问用户他们要打印什么文件,它们要平铺多少次以及要平铺多少次。 My program reads the file into a 2d array, and then it is supposed to tile it out. 我的程序将文件读​​取到2d数组中,然后将其平铺。 I am using three for loops to try to print it and it is printing the correct amount, however it is only printing vertically. 我正在使用三个for循环尝试打印它,它正在打印正确的数量,但是它只是垂直打印。 I tried putting in a blank println to see if that would get it to print correctly but it is not working. 我尝试放入一个空白的println,以查看是否可以正确打印,但无法正常工作。 Anyone have any ideas? 有人有想法么? Here's the part of the code that stores the txt file into the 2d array and the method that is supposed to tile it: 这是将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();
            }
        }
    }

}

Don't use println() until you want to end the line (because there are no more tiles to the right of the current piece). 在要结束这一行之前,请不要使用println()(因为当前片段的右侧不再有平铺)。

If you need to print two tiles next to each other, you need to print a line of the first tile, then the same line again until you have the right number of tiles wide before ending the line (with println). 如果需要相邻打印两个图块,则需要先打印第一个图块的一行,然后再打印同一行,直到在行结束之前使用正确的图块数目(使用println)。

For three tiles wide you would print the same line three times before ending the line. 对于三块瓷砖,您将在结束该行之前打印同一行三遍。

Basically where I'm going is, switch the loop for the buffer with the loop for the tiles across. 基本上,我要去的地方是切换缓冲区的循环和瓷砖的循环。 For each line in the buffer, repeat it for the number of tiles across, then end the line with a newline or use println(""); 对于缓冲区中的每一行,请重复该操作以得到跨接的瓦片数,然后以换行结尾或使用println(“”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM