【问题标题】:Finding the white space algorithm for a tentative size ASCII art program为暂定大小的 ASCII 艺术程序寻找空白算法
【发布时间】:2019-02-15 05:12:29
【问题描述】:

所以我们要设计一本 ASCII 艺术书,我快完成了,但我想不出一点:“Building Java Programs”字样两边的间距

Here is what the book needs to look like

到目前为止,这是我的代码(为了便于帮助,我只显示需要间距帮助的方法。假设 drawLine() 将虚线均匀地绘制到 SIZE 常量的值)

//constant SIZE = 8
public static void drawBottom() {
    //Dash line on top of the bottom portion of the book
    drawLine();
    //Printing first set of rightmost "/"'s
    for (int i = 1; i <= SIZE; i++)
        System.out.print("/");
    System.out.println();
    for (int i = 1; i <= SIZE / 2; i++) {
        //Leftmost pipe
        System.out.print("|");
    //    TO DO: Code label of book
    //    for (int j = 1; j <= ; j++) {
    //
    //    }
        //This loop is only here for example.
        //To show I can fill the space but need
        //the words in the space
        for (int j = 1; j <= SIZE * 3; j++) {
            System.out.print(" ");
        }
        //Rightmost pipe
        System.out.print("|");
        //"Pages" to right of label
        for (int j = 1; j <= -2 * i + (SIZE + 2); j++) {
            System.out.print("/");
        }
        //Move to draw next row
        System.out.println();
    }
    //Dash line on very bottom of entire drawing
    drawLine();
}

Here is my output (when SIZE = 8)

如何计算“Building Java Programs”文本块左右的间距?

我只知道当 SIZE = 8 时,两边各有一个空格

当SIZE = 10时,两边各有4个空格

当SIZE = 13时,两边各有8个空格

什么算法可以帮助我?

【问题讨论】:

标签: java ascii-art


【解决方案1】:

每行可分为三个区域:第一个区域由位于包括索引A 到包括索引B 的空格组成。第二个区域包含从包含索引C 到包含索引D 的文本。第三个区域再次由位于索引E 到包括索引F 的空间组成。侧翼管道位于索引0width + 1

|A......BC......DE......F|

第一个和第三个区域的长度为width/2 - text/2,其中text表示文本的长度。

那么索引是:

Index A: 1
Index B: width/2 - text/2  
Index C: B + 1
Index D: width/2 + text/2
Index E: D + 1
Index F: width

在循环内,可以在相应的区域显示所需的字符:

// Code: label of book
int width = 3 * SIZE; 
width = (width % 2 == 0) ? width : width - 1;           // if the width is odd, choose the next smallest even number 
String text = "Building Java Programs";
for(int j = 1; j <= width; j++) {
    if (j <= width / 2 - text.length() / 2) {           // j <= Index B
        System.out.print(" ");
    }
    else if (j >= width / 2 + text.length() / 2 + 1) {  // j >= Index E = Index D + 1    
        System.out.print(" ");
    }
    else {
        System.out.print(text);
        j = width / 2 + text.length() / 2;              // j = Index D
    }
}

当然,第一个和第二个if-语句也可以组合实现。

输出(SIZE = 8width = 24):

Without text...
|                        |////////
|                        |//////
|                        |////
|                        |//

With text...
| Building Java Programs |////////
| Building Java Programs |//////
| Building Java Programs |////
| Building Java Programs |//

两边各有一个空格 (1 + 22 + 1 = 24)。

输出(SIZE = 10width = 30):

Without text...
|                              |//////////
|                              |////////
|                              |//////
|                              |////
|                              |//

With text...
|    Building Java Programs    |//////////
|    Building Java Programs    |////////
|    Building Java Programs    |//////
|    Building Java Programs    |////
|    Building Java Programs    |//

两边各有四个空格 (4 + 22 + 4 = 30)。

输出(SIZE = 13width = 38):

Without text...
|                                      |/////////////
|                                      |///////////
|                                      |/////////
|                                      |///////
|                                      |/////
|                                      |///

With text...
|        Building Java Programs        |/////////////
|        Building Java Programs        |///////////
|        Building Java Programs        |/////////
|        Building Java Programs        |///////
|        Building Java Programs        |/////
|        Building Java Programs        |///

两边各有八个空格 (8 + 22 + 8 = 38)。

【讨论】:

  • 这行得通!但我最终找到了一种更简单的方法来解决它!检查我的答案
【解决方案2】:

我想通了!我需要使用的方程一直是斜率截距!

大小 = 8,空格 = 1

大小 = 10,空格 = 4

把它们变成点 (8, 1) 和 (10, 4)

记住斜率截距形式 y = mx + b

求m m = (y2 - y1)/(x2 - x1)

m = (4 - 1)/(10 - 8)

m = 3/2

求解 b 使用任一点。我将使用 (8, 1)

1 = 3/2 x 8 + b

b = 1 - (3/2)(8)

b = -11

利润! y = 3/2x - 11

或者在我们的例子中......

for(int j = 1; j <= (3*SIZE)/2 - 11; j++)
{
    System.out.print(" ");
}

System.out.print("Building Java Programs");

//put same loop here again

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-27
    • 1970-01-01
    • 1970-01-01
    • 2015-08-04
    • 2016-10-12
    • 2010-12-14
    相关资源
    最近更新 更多