zoukankan      html  css  js  c++  java
  • 打印数字图形(Java实现)

    Problem Description

    从键盘输入一个整数n(1≤n≤9),打印出指定的数字图形。

    Input

    正整数n(1≤n≤9)。

    Output

    指定数字图形。

    Sample Input

    5

    Sample Output

        1
       121
      12321
     1234321
    123454321
     1234321
      12321
       121
        1

    Hint

     

    Source

     1 import javax.swing.*;
     2         import java.lang.Math;
     3         import java.util.Scanner;
     4 
     5 public class Main {
     6     public static void main(String[] args) {
     7         Scanner sc = new Scanner(System.in);
     8         int n = sc.nextInt();
     9         int i, j, k;
    10         for (i = 0; i < 2*n-1; i++){  // 一个大循环,包含整个菱形的行
    11             k = 0;
    12             if (i < n){  // 整个菱形的上三角
    13                 for (j = 0; j < n+i; j++){  // 找到对应函数关系,打印相应符号
    14                     if (j < n-i-1){
    15                         System.out.printf(" ");
    16                     }
    17                     else if(j >= n-i-1&&j < n){
    18                         k++;
    19                         System.out.printf("%d", k);
    20                     }
    21                     else{
    22                         k--;
    23                         System.out.printf("%d", k);
    24                     }
    25                 }
    26                 System.out.printf("
    ");
    27             }
    28             else{
    29                 for (j = 0; j < 3*n-i-2; j++){
    30                     if (j < i-n+1){
    31                         System.out.printf(" ");
    32                     }
    33                     else if (j >= i-n+1 && j < n){
    34                         k++;
    35                         System.out.printf("%d", k);
    36                     }
    37                     else{
    38                         k--;
    39                         System.out.printf("%d", k);
    40                     }
    41                 }
    42                 System.out.printf("
    ");
    43             }
    44         }
    45     }
    46 }

     

  • 相关阅读:
    Spring Boot 使用Redis
    openTSDB(转)
    httpClient 超时时间设置(转)
    HTTPClient 超时链接设置
    入坑python 自己写的小工具,纪念一下
    Linux下SVN创建新的项目
    java对象数组的概述和使用
    解决fastDFS客户端连接超时问题
    显示目录结构
    centos7开启80和8080端口
  • 原文地址:https://www.cnblogs.com/sugerandmaster/p/11480395.html
Copyright © 2011-2022 走看看