读字符来创建图片

时间:2014-07-06 20:44:10

标签: java filewriter printwriter

基本上,输入文件包含指示多个空格的整数行,以及要打印以创建图片的字符。一行中的第一个整数将始终是要打印的空格数(可能为0)。每个备用整数都将是要打印的字符数。一行中的最后一个整数将始终为-1,标记该行的结尾。例如,行0 2 4 2 -1表示“0空格 - 2个字符 - 4个空格 - 2个字符 - 下一行”。字符可以是任何字符,例如“#”或“%”

这是我对循环的想法

while there is more of the file to read
{
    set your boolean flag for spaces to true (you'll toggle it back and forth)
    read the first number
    while the number just read is not -1
    {
        if the flag is true print that number of spaces and toggle the flag
        else print that number of characters and toggle the flag
        read the next number
    }
    print a newline
}

我需要的是如何读取行并打印正确的空格/字符。考虑到每条线具有不同的长度和空间,我无法找出读取每个字符的最佳方法。 这是一个input.txt文件示例。

4 3 3 -1
2 4 1 1 2 -1
1 1 1 1 1 2 1 1 1 -1
0 3 2 1 1 3 -1
5 1 4 -1
2 3 1 3 1 -1
1 5 1 3 -1
1 3 1 1 1 1 1 1 -1
1 5 1 3 -1
2 3 1 3 1 -1

我打算使用

String.format("%" + numberOfSpaces + "s", ""); 

打印空格和字符。

0 个答案:

没有答案