gpt4 book ai didi

java - 寻找暂定大小的 ASCII 艺术程序的空白算法

转载 作者:行者123 更新时间:2023-11-30 12:07:44 26 4
gpt4 key购买 nike

所以我们必须设计一本 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”文本 block 左右的间距?

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

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

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

什么算法可以帮到我?

最佳答案

每一行可分为三个区域:第一个区域由位于索引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)。

关于java - 寻找暂定大小的 ASCII 艺术程序的空白算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54703017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com