java - java 钻石星号的高度

标签 java for-loop height width

如何让这段代码正确显示高度?因此,如果菱形星号的宽度为 3,高度为 5,那么菱形将如下所示。

     *
    ***
    ***
    ***
     *

它只是将多余的高度添加到中间。到目前为止,这是我的代码。它可以很好地处理宽度。

public static void main(String[] args) {
    int width, height, heightT;
    Scanner scan = new Scanner(System.in);

    while(true) {
        System.out.println("Enter width of diamond (odd number between 0-19): ");
        width = scan.nextInt();
        if (width % 2 != 0) {
            if (0 <= width && width <= 19) {
                break;
            }
        }
    }
    while(true) {
        System.out.println("Enter height of diamond (greater than or equal to width): ");
        height = scan.nextInt();
        if(height >= width) {
            heightT = height - width;
            break;
        }
    }

    for(int i = 1; i < width + 1; i += 2) {
        for(int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");

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

        System.out.print("\n");
    }

    for(int i = width - 2; i > 0; i -= 2) {
        for(int j = 0; j < 9 - i / 2; j++)
            System.out.print(" ");

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

        System.out.print("\n");
    }
}

最佳答案

在解决此类逻辑时,您必须始终关注您知道永远不会改变的事情。在这种情况下,三件事永远不会改变:

<强>1。第一行。始终等于一个星号。

<强>2。中间线。始终等于星号的宽度。

<强>3。最后一行。始终等于一个星号。

现在,将菱形从中间行分成两部分。请注意,总高度始终是顶部(或底部)的高度乘以 2 加 1(中间行)。你会得到更多类似这样的东西:

    for(int i = 0; ((i*2)+1) <= height; i++){
        //Print top
    }

    // Print middle row outside of loops or inside if you must.

    for(int i = 0; ((i*2)+1) <= height; i++){
        //Print bottom.
    } 

编辑:这只适用于高度大于或等于宽度的钻石。

编辑:此代码解决了您的问题(给定高度大于或等于宽度)。但是,它无法正确处理width = 0width = 1。我相信这对您来说已经足够微不足道了。

public static void main(String[] args) {
    int height = 9, width = 9;
    int spaces = (width - 1) / 2, asterisks = 1;



    for(int i = 0;((i*2)) < height; i++){
        for(int ii = 0;ii < spaces;ii++){
           System.out.print(" ");  
        }
        for(int ii = 0;ii < asterisks;ii++){
           System.out.print("*");
        }
    spaces--;
    if(asterisks < width)asterisks += 2;
    System.out.println();
    }

    spaces = 0;

    for(int i = 0; ((i*2)+1) < height; i++){
        if(width < height){
            for(int ii = 0;ii < spaces;ii++){
                System.out.print(" ");
            }
            for(int ii = 0;ii < asterisks;ii++){
               System.out.print("*");
            }
        }else{
            for(int ii = 0;ii < (spaces + 1);ii++){
                System.out.print(" ");
            }
            for(int ii = 0;ii < (asterisks - 2);ii++){
               System.out.print("*");
            }
        }
    spaces++;
    asterisks -= 2;
    System.out.println();
    }
}

关于java - java 钻石星号的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33492451/

相关文章:

java - 对继承对象进行多次转换

java - AKKA 在一段时间后停止记录堆栈跟踪

javascript - 从 jsVanilla 创建动态选择框

function - 使用 CSS,元素的高度可以是浏览器宽度的函数吗?

html - 使div高度自动按比例调整

javascript - 使用 Javascript 暂时增加元素的宽度和高度,同时保持其居中位置

Google Apps Engine 上的 Java 与 Python

java - 在java中修改复杂的csv文件

php - 如何根据项目的组和数量生成列?

Perl for 循环失控