余晖与雨 2021-11-06 16:30 采纳率: 60.4%
浏览 25
已结题

关于java中普通算法求解

img


我知道先做一边然后采用反转做出下部分,但我无奈不知道上部分如何做
求做法

  • 写回答

1条回答 默认 最新

  • Mymel_晗 CSDN实力新星 2021-11-06 17:22
    关注

    这是我的代码,如有问题欢迎指正。

    如果可以的话请关注我的博客,会定时更新一些简单的算法问题与学习笔记。

    祝读者学业进步!


    代码:

    package Myanswer;
    
    import java.util.Scanner;
    
    public class Solution {
    
        public static void Print(int n) {
            int row = n;
            int space = 0;
            if (n <= 0 || n >= 10) {
                System.out.println("Your input is out of bound!");
                return;
            }
            while (row >= 1) {
                for (int j = 0; j < space; j++)
                    System.out.print(" ");
                for (int i = 0; i < (2 * row - 1); i++)
                    System.out.print(row);
                System.out.println("");
                row--;
                space++;
            }
            row = 2;
            space -= 2;
            while (row <= n) {
                for (int j = 0; j < space; j++)
                    System.out.print(" ");
                for (int i = 0; i < (2 * row - 1); i++)
                    System.out.print(row);
                System.out.println("");
                row++;
                space--;
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            Solution.Print(n);
        }
    
    }
    

    运行截图:

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 11月15日
  • 已采纳回答 11月7日
  • 请采纳用户回复 11月7日
  • 创建了问题 11月6日