java 为什么我在打印一个有for循环的金字塔时会得到意外的输出?

46scxncf  于 2023-01-04  发布在  Java
关注(0)|答案(2)|浏览(131)

如果将5插入到下面的sc.input中:

*
 ***
*****

但是,我得到了下面:

*
 ***
*****

有什么问题吗?

public void test3() {
    Scanner sc = new Scanner(System.in);
    int n1 = sc.nextInt();
    for(int i=0; i<n1; i++){
        if(i<=n1/2){
            for(int j=0; j<n1; j++) {
                if( j<(n1/2)-i || j>(n1/2)+i ) {
                    for(int m=0; m<(n1/2)-i; m++){
                        System.out.print(" ");
                    }
                    
                } /*else if( j==2 ) {
                    
                        System.out.print("*");
                    
                    
                }*/ else {
                    System.out.print("*");                       
                    
                }
            }
            System.out.println();
        }
    }
}
ppcbkaq5

ppcbkaq51#

我注意到打印的每一行都有一个奇数的 *。用户输入是收集行数,还是收集最后一行的最大 * 数?如果输入收集要打印到屏幕上的行数,那么可以按下面的代码所示进行。

import java.util.Scanner;

/**
 * Output:
 *
 * Enter the number of rows: 10
 *              *
 *             ***
 *            *****
 *           *******
 *          *********
 *         ***********
 *        *************
 *       ***************
 *      *****************
 *     *******************
 *
 *
 * @author martinfall
 */
public class Test {

    /**
     * Main method.
     *
     * @param args
     */
    public static void main(String[] args) {
        // Create a new Scanner object
    Scanner input = new Scanner(System.in);

    // Prompt the user to enter the number of row 
    System.out.print("Enter the number of rows: ");
    int number = input.nextInt(); // number holds the number of rows

    // Outer loop prints the number of lines entered by user
    for (int i = 0; i < number; i++) {
        // Pad each line with the necessary blank spaces
        for (int j = 0; j < Math.ceil(number - i); j++) {
            System.out.print(" ");
        }

        // Print an odd number of * on each line, such as 1, 3, 5, etc.
        for (int k = 0; k < (2 * i) + 1; k++) {
            System.out.print("*");
        }

        // Print a new line at the end of each line of *
        System.out.println();
        }
    }
}

如果输入是收集最后一行中 * 的个数,那么平衡 * 的奇数行/偶数行就有点棘手了

ecbunoof

ecbunoof2#

您的循环中存在一些逻辑错误:

for(int i=n1/2; i>=0; i--)  // (#1)
{
    for(int j=0; j<n1; j++) {
        if( j < i || j>n1-i-1 ) { // (#2) 
            System.out.print(" "); // (#3)
        } else {
            System.out.print("*");                       
        }
    }
    System.out.println();
}
    • 我的备注**(每个注解都是指特定的代码部分,请参见代码注解中的(#numbers)):

1.为什么要循环到n1-1并检查n1/2呢?这样你就跳过了一半的迭代,这是毫无意义的。只要循环到n1/2,这样你就可以避免if-conditional
更妙的是:让我们数一下后院;以后会有用的
1.我们需要一个品牌,你的条件,以了解当一个空间必须打印。

    • 逻辑**:在第一行(高i),我们需要打印更多的空格。随着i的减少,我们将有越来越少的空格(直到最后一行,我们将有0个空格)。您只需要建立一个范围:
if( j < i || j>n1-i-1 )

因为范围是从0到n1-1,所以j>n1-i-1中的最后一个-1是必需的
1.在j上找到打印空间的正确条件后,我们就可以了。因此,我们不再需要forloop来打印空间
请注意,此逻辑也适用于n1的偶数值。

    • n1=7的输出**:
*   
  ***  
 ***** 
*******
    • n1=8的输出**:
**
  ****
 ******
********

相关问题