Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为一个暂定大小的ASCII art程序寻找空白算法_Java_Ascii Art - Fatal编程技术网

Java 为一个暂定大小的ASCII art程序寻找空白算法

Java 为一个暂定大小的ASCII art程序寻找空白算法,java,ascii-art,Java,Ascii Art,因此,我们必须设计一本ASCII艺术书,我几乎完成了,但我想不出一点:单词“Building Java Programs”两边的间距 这是我到目前为止的代码(为了方便帮助,我只展示了需要间距帮助的方法。假设drawLine()将虚线均匀地绘制为大小常量) //常量大小=8 publicstaticvoiddrawbottom() { //书底部顶部的虚线 抽绳(); //打印第一组最右边的“/”字符 对于(int i=1;i每行可分为三个区域:第一个区域由从包含索引A到包含索引B的空格组成。第

因此,我们必须设计一本ASCII艺术书,我几乎完成了,但我想不出一点:单词“Building Java Programs”两边的间距

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

//常量大小=8
publicstaticvoiddrawbottom()
{
//书底部顶部的虚线
抽绳();
//打印第一组最右边的“/”字符

对于(int i=1;i每行可分为三个区域:第一个区域由从包含索引
A
到包含索引
B
的空格组成。第二个区域包含从包含索引
C
到包含索引
D
的文本。第三个区域同样由包含索引
D
的空格组成将索引
E
包括索引
F
。侧翼管道位于索引
0
宽度+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
    }
}
两边各有一个空格(
1+22+1=24

输出(
SIZE=10
width=30
):

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

输出(
SIZE=13
width=38
):


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

我算出了!我需要使用的等式是斜率截距

大小=8,空间=1

尺寸=10,空间=4

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

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

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

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

m=3/2

用任一点解b,我用(8,1)

1=3/2x8+b

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

b=-11

利润!y=3/2x-11

或者在我们的情况下


for(int j=1;j(另请参见:)这是可行的!但我最终找到了一种更简单的方法来解决它!检查我的答案
Without text...
|                        |////////
|                        |//////
|                        |////
|                        |//

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

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

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