我想用java打印数字镜像图案。与用户输入

问题描述 投票:0回答:1

我正在尝试用java中的用户输入打印数字镜像图案。 在这里,当用户输入最多 10 个时,它可以完美打印图案。enter image description here 但是当用户输入超过 10 个时,它不会完美打印图案 ex(11)enter image description here 我认为间距有问题。 下面我还提供了代码。

import java.util.Scanner;

public class TriangleMirrorPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the number of rows: ");
        int n = scanner.nextInt();
        printTriangleAndMirror(n);
    }

    public static void printTriangleAndMirror(int n) {
       
        for (int i = 1; i <= n; i++) {
            
            for (int j = 1; j <= i; j++) {
                System.out.print(j);
            }
            for (int j = n; j >i; j--) {
                System.out.print(" ");
            }
            for (int j = n; j >i; j--) {
                System.out.print(" ");
            }
            for (int j = i; j >=1; j--) {
                if(j==n){
                    continue;
                }
                System.out.print(j);
            }
            System.out.println();
        }
    }
}

我想让我的代码动态化,以便任何数量的输入都可以完美地打印图案

java algorithm loops pattern-matching mirror
1个回答
0
投票

以下是答案的英文翻译:

为了修复代码并使其能够正确打印任意行数的镜像图案,我们需要调整第二个和第三个循环中的空格数。当数字超过10时,我们需要在数字之间添加额外的空格以确保它们正确对齐。另外,我们需要确保镜像部分在数字长度方面得到正确处理。

这是修改后的代码,应该能够处理任何大小的输入:

java
import java.util.Scanner;  
  
public class TriangleMirrorPattern {  
    public static void main(String[] args) {  
        Scanner scanner = new Scanner(System.in);  
        System.out.print("Enter the number of rows: ");  
        int n = scanner.nextInt();  
        printTriangleAndMirror(n);  
    }  
  
    public static void printTriangleAndMirror(int n) {  
        for (int i = 1; i <= n; i++) {  
            // Print the left side of numbers  
            for (int j = 1; j <= i; j++) {  
                System.out.print(j);  
                // Add a space after a number if it's not the last one in the current row to maintain alignment  
                if (j < i) {  
                    System.out.print(" ");  
                }  
            }  
              
            // Print the middle spaces  
            int spaces = (n - i) *2; // We need to print spaces on both sides  
            for (int j = 0; j < spaces; j++) {  
                System.out.print(" ");  
            }  
              
            // Print the mirror numbers on the right side  
            for (int j = i - 1; j >= 1; j--) {  
                System.out.print(j);  
                // Add a space after a number if it's not the first one to maintain alignment  
                if (j > 1) {  
                    System.out.print(" ");  
                }  
            }  
            System.out.println();  
        }  
    }  
}
/*In this code, the following changes were made:

When printing the numbers on the left side, if the number is not the last one in the current row, we add a space after it to ensure proper alignment even for two-digit or multi-digit numbers.
We calculate the number of spaces needed to be printed in the middle and print them accordingly.
When printing the mirror numbers on the right side, we also ensure to add appropriate spaces between the numbers.
Now, regardless of the number entered by the user, this code should correctly print the desired mirror pattern.*/
© www.soinside.com 2019 - 2024. All rights reserved.