Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.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中绘制箭头?_Java - Fatal编程技术网

如何在Java中绘制箭头?

如何在Java中绘制箭头?,java,Java,以下是我试图创建的内容: * ** *** **** ********* **** *** ** * 以下是我所创造的: * ** *** **** ********** **** *** ** * 这是我的密码: for(int i = 1; i <= 5; i++){ for(int j = 1; j<=i; j++){ System.out.print("*"); }

以下是我试图创建的内容:

    *
    **
    ***
    **** 
*********
    ****
    ***
    **
    *
以下是我所创造的:

*
**
***
**** 
**********
****
***
**
*
这是我的密码:

for(int i = 1; i <= 5; i++){
    for(int j = 1; j<=i; j++){
        System.out.print("*");
    }
    System.out.println();
    if(i == 4){
        for(int f = 0; f < 5; f++){
            System.out.print("*");
        }
    }
}

for(int i = 1; i <= 5; i++){
    for(int j = 4; j>=i; j--){
        System.out.print("*");
    }
    System.out.println();
}

for(int i=1;i您应该在打印“*”的for循环之前添加另一个打印“”的内部for循环

private static void printArrow (final int min, final int max, final int tip)
{
    final int numSpaces = tip - max - 1;

    // print the top
    for (int i = min; i <= max; i++)
    {
        for (int j = 0; j < numSpaces; j++)
            System.out.print(" ");

        for (int j = 0; j < i; j++)
            System.out.print("*");

        System.out.println();
    }

    // print the tip
    for (int i = 0; i < tip; i++)
    {
        System.out.print("*");
    }
    System.out.println();

    // print the bottom
    for (int i = max; i >= min; i--)
    {
        for (int j = 0; j < numSpaces; j++)
            System.out.print(" ");

        for (int j = 0; j < i; j++)
            System.out.print("*");

        System.out.println();
    }
}
另一个例子

public static void main(String[] args)
{
    // this will print the arrow in your question
    printArrow(1, 4, 9);
}
printArrow(1, 10, 19);
        *
        **
        ***
        ****
        *****
        ******
        *******
        ********
        *********
        **********
*******************
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *
输出

public static void main(String[] args)
{
    // this will print the arrow in your question
    printArrow(1, 4, 9);
}
printArrow(1, 10, 19);
        *
        **
        ***
        ****
        *****
        ******
        *******
        ********
        *********
        **********
*******************
        **********
        *********
        ********
        *******
        ******
        *****
        ****
        ***
        **
        *

您可以使用
printf
方法告诉
Java
以特定格式打印。例如,您需要在前面加5个空格,然后打印文本:

System.out.printf(“%5s”,“hello”);

要获得所需的输出,请尝试以下代码。注意:可能有更好的解决方案

for (int i = 1; i <= 5; i++) {
    if (i != 5) // middle line should not be prefix with spaces
        // empty 5 spaces before starting the loop
        System.out.printf("%5s", "");

    for (int j = 1; j <= i; j++) {
        System.out.print("*");
    }
    System.out.println();
    if (i == 4) {
        for (int f = 0; f < 5; f++) {
            System.out.print("*");
        }
    }
}

for (int i = 1; i <= 5; i++) {
    // print 5 space before
    System.out.printf("%5s", ""); // <== note the printf here with empty string
    for (int j = 4; j >= i; j--) {
        System.out.print("*");
    }
    System.out.println();
}

for(int i=1;i这是我的答案。您只需先插入空格,然后打印
*
,直到到达中间行。如果要使其动态,只需将
k
的边界值动态替换为与
f
的边界相同的值:

public static void main(String[] args) {
    int k = 1;
    int m = 0;
    for (int i = 1; i <= 5; i++) {
        m = i - 1;
        while (k != 6 && m != 4) {
            System.out.print(" ");
            k++;

        }

        for (int j = 1; j <= i; j++) {

            System.out.print("*");
        }
        System.out.println();
        if (i == 4) {

            for (int f = 0; f < 5; f++) {

                System.out.print("*");
            }
        }
        k = 1;
    }

    k = 1;
    for (int i = 1; i <= 5; i++) {
        while (k != 6) {
            System.out.print(" ");
            k++;

        }

        for (int j = 4; j >= i; j--) {

            System.out.print("*");
        }
        System.out.println();
        k = 1;
    }

}
publicstaticvoidmain(字符串[]args){
int k=1;
int m=0;

对于(inti=1;i给定您提供的形状,并假设它将以单空间字体输出,我们可以在网格上绘制它

将其直观地分成两部分,我们可以看到有两种主要模式

尾巴(绿色)和头(红色)

还有第二种模式,头部的恒星数量有增有减

尾巴的大小可能会有所不同,头部的大小也可能会有所不同,而且它仍然是我们认为是箭头的形状

当输出文本时,最简单的迭代顺序通常是从左到右,从上到下,除非处理从右到左的语言或垂直阅读,我将假设输出是西方文化,因为这是编程输出流中流行的

所以,问题是,如何最好地构建输出所需的字符串

给定输出将在流上的格式

宽度是你的外环,尾/长度是你的内环

您提供的代码使用10表示宽度,将其分成两组,每组5个,第二组偏移1


for(int i=1;i)您还应该打印空格。它与其他for循环类似。请重试;-)如果我没有编辑我的答案,您如何更改答案?某种魔法?如空格数(或*)你知道,为什么你需要一个循环?为了使它是动态的,比如说你想增加星号和空格的数量,你只需要增加循环中条件的值。而不是仅仅增加5。我如何为每个值设置它,有趣的是,这是公认的答案。它甚至没有按照OP的要求打印箭头。提示箭头的一角也突出了1'*'far@MichaelMarkidis我已经解决了这个小问题。特别有趣的是,OP明确要求不要给出答案,就像是作业一样,但答案实际上已经给了你。我想这里的每个人都给出了答案。据我所知,给出答案是OP什么都没做的问题并且想要从头开始的答案。@RyanTheLeach让我知道如何向OP解释,使其在没有此代码的情况下适用于任何值。在不改变角度的情况下,您可以使其宽度或长度通用,但不能同时适用于这两种情况。幸运的是,OP没有要求将其通用化,因此我对其关注较少。请随意改进,我也没有得到代表这是一个维基答案。