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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用多种方法格式化问题并使变量相关相关的知识,希望对你有一定的参考价值。

基本上我的主要问题来自我的程序中的最后两个方法,drawBlock和drawTiers。它被设计为输出蛋糕的图像,虽然它确实如此,但它并没有按照它的意图进行。例如,程序只允许3到9(3,5,7,9)之间的奇数输入,并使用它们来决定打印蛋糕的大小。输入3应该得到我们

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

虽然5会得到我们

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

分别。 drawTiers有一个int参数,而drawBlock有两个。层是什么意思决定层的宽度,因为每次调用drawBlock时,它都会使第二个参数变大,从而使层更宽。事情是我的程序包含一个层值,它没有做任何事情因为我为每个可能的场景编码而被忽略。这不是它的意图,我想知道如何让它正确使用层值。对不起,如果我没有正确解释,但我很感激任何帮助。 “sizeParam”也就是将用户输入发送到方法中的内容。

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

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

关键是打印字符而不是整行。有了它,您可以根据尺寸参数操纵每层需要打印的内容。

这是一次尝试。您可能需要根据您的确切输出要求调整一些数字

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('
');
    }
}

这是它为drawTiersNew打印的内容(9)

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

以上是关于使用多种方法格式化问题并使变量相关的主要内容,如果未能解决你的问题,请参考以下文章

Python将JSON格式文件导入 redis,多种方法

Python将JSON格式文件导入 redis,多种方法

如何使用Android片段管理器传递变量[重复]

面向对象相关

常用python日期日志获取内容循环的代码片段

使用片段着色器在特定位置绘制完美的水平线