问题
So we have to design an ASCII art book, and I'm almost done, but I can't figure out one little thing: The spacing on either side of the words "Building Java Programs"
Here is what the book needs to look like
Here is my code so far (for ease of help, I'm only showing the method where the spacing help is needed. Assume drawLine() draws the dashed line evenly to what the SIZE constant is)
//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)
How can I figure out the spacing to the left and right of the "Building Java Programs" text block?
All I know is that when SIZE = 8, there is one space on either side
When SIZE = 10, there are 4 spaces on either side
When SIZE = 13, there are 8 spaces on either side
What algorithm can help me here?
回答1:
Each line can be divided into three areas: The first area consists of spaces located from including index A
to including index B
. The second area contains the text located from including index C
to including index D
. And the third area again consists of spaces located from including index E
to including index F
. The flanking pipes are located at the indexes 0
and width + 1
:
|A......BC......DE......F|
The first and the third area have a length of width/2 - text/2
, where text
denotes the length of the text.
Then the indexes are:
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
Within the loop, the required characters can be displayed in the respective area:
// 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
}
}
Of course, the first and second if
-statements can also be implemented in combination.
Output (SIZE = 8
, width = 24
):
Without text...
| |////////
| |//////
| |////
| |//
With text...
| Building Java Programs |////////
| Building Java Programs |//////
| Building Java Programs |////
| Building Java Programs |//
There is one space on either side (1 + 22 + 1 = 24
).
Output (SIZE = 10
, width = 30
):
Without text...
| |//////////
| |////////
| |//////
| |////
| |//
With text...
| Building Java Programs |//////////
| Building Java Programs |////////
| Building Java Programs |//////
| Building Java Programs |////
| Building Java Programs |//
There are four spaces on either side (4 + 22 + 4 = 30
).
Output (SIZE = 13
, width = 38
):
Without text...
| |/////////////
| |///////////
| |/////////
| |///////
| |/////
| |///
With text...
| Building Java Programs |/////////////
| Building Java Programs |///////////
| Building Java Programs |/////////
| Building Java Programs |///////
| Building Java Programs |/////
| Building Java Programs |///
There are eight spaces on either side (8 + 22 + 8 = 38
).
回答2:
I figured it out! The equation I needed to use was slope-intercept all along!
SIZE = 8, space = 1
SIZE = 10, space = 4
Turn them into points (8, 1) and (10, 4)
Remember slope-intercept form y = mx + b
Find m m = (y2 - y1)/(x2 - x1)
m = (4 - 1)/(10 - 8)
m = 3/2
Solve for b Using either point. I'll use (8, 1)
1 = 3/2 x 8 + b
b = 1 - (3/2)(8)
b = -11
Profit! y = 3/2x - 11
Or in our case...
for(int j = 1; j <= (3*SIZE)/2 - 11; j++)
{
System.out.print(" ");
}
System.out.print("Building Java Programs");
//put same loop here again
来源:https://stackoverflow.com/questions/54703017/finding-the-white-space-algorithm-for-a-tentative-size-ascii-art-program