java 打印空心菱形数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 打印空心菱形数相关的知识,希望对你有一定的参考价值。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package BASICJAVA;

import java.util.Scanner;

/**
 *
 * @author ABIR
 */
public class Print_Hollow_Rhombus_Number {
    public static void main(String[]args){
        Scanner abir=new Scanner(System.in);
        System.out.println("Enter sample input");
        int x=abir.nextInt();
        System.out.println("sample output");
        int c=1;
        while (c<=x){
          System.out.println ();
          int d = 1;
          while (d<=(x-c)){
            System.out.print (" ");
            d++;
          }
          if (c>1){
            System.out.print (1);
          }
          int e = 1;
          while (e<=(2*c-3)){
            System.out.print (" ");
            e++;
          }
          System.out.print (2*c-1);
          c++;
        }
        int f = x + 1;
        int j = x - 1;
        while (f<=(2*x-1)){
          System.out.println ();
          int k = 1;
          while (k<=(f-x)){
            System.out.print (" ");
            k++;
          }
          System.out.print (1);
          int l = 1;
          while (l<=(2*j-3)){
            System.out.print (" ");
            l++;
          }
          if (f<(2*x-1)){
            System.out.print ((2*j-3));
          }
          f++;
          j--;
        }
        System.out.println ();
        System.out.println("=======================================================");
      }
}

以上是关于java 打印空心菱形数的主要内容,如果未能解决你的问题,请参考以下文章

java 怎样写出一个菱形程序?空心和实心

java 打印空心菱形星

java 打印空心菱形

关于使用Java中的for循环和if语句打印空心菱形的详细分析(也算是通过重新梳理一遍来加深自己的理解吧!)

PHP练习--用for循环打印空心菱形

java 空心菱形