简体   繁体   English

使用多种方法格式化问题并使变量相关

[英]Formatting issue with multiple methods and making variables relevant

Essentially my main issues comes from the last two methods in my program, drawBlock and drawTiers. 基本上我的主要问题来自我的程序中的最后两个方法,drawBlock和drawTiers。 It's designed to output the image of a cake and while it does, it doesn't do it in the way it's meant to. 它被设计为输出蛋糕的图像,虽然它确实如此,但它并没有按照它的意图进行。 For instance, the program only allows odd input between 3 and 9 (3,5,7,9) and it uses these to decide how big of a cake to print. 例如,程序只允许3到9(3,5,7,9)之间的奇数输入,并使用它们来决定打印蛋糕的大小。 An input of 3 should get us 输入3应该得到我们

              *
              |
              |
          =========
          |..xxx..|
/^^^^^^^^^^^^^^^^^^^^^^^^^^^\ 
[|_______||_______||_______|] 
[|___||_______||_______|___|] 
\===========================/

While a 5 would get us 虽然5会得到我们

                       *
                       |
                       |
                   =========
                   |..xxx..|
                   |..xxx..|
          |........xxxxxxxxx........|
          |........xxxxxxxxx........|
/^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\ 
[|_______||_______||_______||_______||_______|] 
[|___||_______||_______||_______||_______|___|] 
[|_______||_______||_______||_______||_______|] 
[|___||_______||_______||_______||_______|___|] 
\=============================================/

respectively. 分别。 drawTiers has one int argument while drawBlock has two. drawTiers有一个int参数,而drawBlock有两个。 Tiers is what's meant to decide how wide the tiers are as each time it calls drawBlock it's suppose to make the second argument larger, thus making tiers wider. 层是什么意思决定层的宽度,因为每次调用drawBlock时,它都会使第二个参数变大,从而使层更宽。 Thing is while my program include a tier value, it doesn't do anything as it's ignored since I coded for each possible scenario. 事情是我的程序包含一个层值,它没有做任何事情因为我为每个可能的场景编码而被忽略。 This isn't how it's meant to be done and I'm wondering how to get it to use the tiers value properly. 这不是它的意图,我想知道如何让它正确使用层值。 Sorry If I didn't explain properly but I'd appreciate any help. 对不起,如果我没有正确解释,但我很感激任何帮助。 Also "sizeParam" is just what the user input is sent down into the method as. “sizeParam”也就是将用户输入发送到方法中的内容。

public static void drawBlock (int sizeParam, int tierParam) {
        if (sizeParam == 3) {
            System.out.printf("%20s", "|..xxx..|\n");
        }
        else if (sizeParam == 5) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%29s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%38s", "|........xxxxxxxxx........|\n");
            }
        }
        else if (sizeParam == 7) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%38s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%47s", "|........xxxxxxxxx........|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%56s", "|..............xxxxxxxxxxxxxxx..............|\n");
            }
        }
        else if (sizeParam == 9) {
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%47s", "|..xxx..|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%56s", "|........xxxxxxxxx........|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%65s", "|..............xxxxxxxxxxxxxxx..............|\n");
            }
            for (int count = 0; count < sizeParam/2; count++) {
                System.out.printf("%74s", "|....................xxxxxxxxxxxxxxxxxxxxx....................|\n");
            }
        }
    }

    public static void drawTiers (int tiersParam) {
        for (int count = 0; count <= tiersParam/10; count++) {
            int size = tiersParam;
            drawBlock(size, tiersParam + 1);
    }
    }

The key is to print characters instead of full lines. 关键是打印字符而不是整行。 With that you can manipulate what needs to be printed per tier, based on size parameter. 有了它,您可以根据尺寸参数操纵每层需要打印的内容。

Here is an attempt on the same. 这是一次尝试。 You may need to tweak some numbers based on your exact output requirements 您可能需要根据您的确切输出要求调整一些数字

public static void drawTiersNew (int tiersParam) {
    for (int level = 0; level < tiersParam/2 ; level++) {
        drawBlockNew(level, tiersParam/2);
    }
}

public static void drawBlockNew (int level, int tierParam) {
   for (int repeat = 1; repeat <= tierParam; repeat++) {
        for (int count = 0; count < (tierParam - level)*9 ; count++) {
            System.out.print(' ');
        }
        System.out.print('|');
        for (int count = 0; count < (2 + 6 * level) ; count++) {
            System.out.print('.');
        }
        for (int count = 0; count < (3 + 6 * level); count++) {
            System.out.print('x');
        }
        for (int count = 0; count < (2 + 6 * level); count++) {
            System.out.print('.');
        }
        System.out.print('|');
        System.out.print('\n');
    }
}

And here is what it prints for drawTiersNew(9) 这是它为drawTiersNew打印的内容(9)

                                    |..xxx..|
                                    |..xxx..|
                                    |..xxx..|
                                    |..xxx..|
                           |........xxxxxxxxx........|
                           |........xxxxxxxxx........|
                           |........xxxxxxxxx........|
                           |........xxxxxxxxx........|
                  |..............xxxxxxxxxxxxxxx..............|
                  |..............xxxxxxxxxxxxxxx..............|
                  |..............xxxxxxxxxxxxxxx..............|
                  |..............xxxxxxxxxxxxxxx..............|
         |....................xxxxxxxxxxxxxxxxxxxxx....................|
         |....................xxxxxxxxxxxxxxxxxxxxx....................|
         |....................xxxxxxxxxxxxxxxxxxxxx....................|
         |....................xxxxxxxxxxxxxxxxxxxxx....................|

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

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