zoukankan      html  css  js  c++  java
  • Java打印实心、空心的三角形和菱形

    1.实心三角形

    代码:

     1 import java.util.Scanner;
     2 
     3 public class Test {
     4 
     5     public static void main(String[] args) {
     6         int rows;
     7         Scanner sc = new Scanner(System.in);
     8         System.out.println("Please input rows:");
     9         rows = sc.nextInt();
    10         sc.close();
    11         for (int i = 1; i <= rows; i++) {//控制打印行数
    12             for (int j = 1; j <= rows - i; j++) {//控制每行打印空格数
    13                 System.out.print(" ");
    14             }
    15             for (int j = 1; j <= i * 2 - 1; j++) {//控制打印星号数
    16                 System.out.print("*");
    17             }
    18             System.out.println();//每打一行,换行
    19         }
    20     }
    21 
    22 }
    View Code

    2.空心三角形

    代码:

     1 import java.util.Scanner;
     2 
     3 public class Test {
     4     public static void main(String[] args) {
     5         int rows;
     6         Scanner sc = new Scanner(System.in);
     7         System.out.println("Please input rows:");
     8         rows = sc.nextInt();
     9         sc.close();
    10         for (int i = 1; i <= rows; i++) {
    11             for (int j = 1; j <= rows - i; j++) {
    12                 System.out.print(" ");
    13             }
    14             for (int j = 1; j <= 2 * i - 1; j++) {
    15                 if (i == 1 || i == rows) { // 如果是第一行或最后一行,打印所有星号
    16                     System.out.print("*");
    17                 } else if (j == 1 || j == 2 * i - 1) { // 如果是每行的第一个或者最后一个,打印星号
    18                     System.out.print("*");
    19                 } else { // 其余打印空格
    20                     System.out.print(" ");
    21                 }
    22             }
    23             System.out.println();
    24         }
    25     }
    26 }
    View Code

    3.实心菱形

    代码:

     1 import java.util.Scanner;
     2 
     3 public class Test {
     4     public static void main(String[] args) {
     5         int rows;
     6         Scanner sc = new Scanner(System.in);
     7         System.out.println("Please input odd rows:");// 只能是奇数行
     8         rows = (sc.nextInt() + 1) / 2;// 上半部分行数
     9         sc.close();
    10         for (int i = 1; i <= rows; i++) {
    11             for (int j = 1; j <= rows - i; j++) {
    12                 System.out.print(" ");
    13             }
    14             for (int j = 1; j <= 2 * i - 1; j++) {
    15                 System.out.print("*");
    16             }
    17             System.out.println();
    18         }
    19         for (int i = rows - 1; i >= 1; i--) { // 下半部分不可重复打印上半部分最后一行,i=rows-1)
    20             for (int j = 1; j <= rows - i; j++) {
    21                 System.out.print(" ");
    22             }
    23             for (int j = 1; j <= 2 * i - 1; j++) {
    24                 System.out.print("*");
    25             }
    26             System.out.println();
    27         }
    28     }
    29 }
    View Code

    4.空心菱形

    代码:

     1 import java.util.Scanner;
     2 
     3 public class Test {
     4     public static void main(String[] args) {
     5         int rows;
     6         Scanner sc = new Scanner(System.in);
     7         System.out.println("Please input odd rows:");// 只能是奇数行
     8         rows = (sc.nextInt() + 1) / 2;// 上半部分行数
     9         sc.close();
    10         sc.close();
    11         for (int i = 1; i <= rows; i++) {
    12             for (int j = 1; j <= rows - i; j++) {
    13                 System.out.print(" ");
    14             }
    15             for (int j = 1; j <= 2 * i - 1; j++) {
    16                 if (i == 1) {
    17                     System.out.print("*");
    18                 } else if (j == 1 || j == 2 * i - 1) {
    19                     System.out.print("*");
    20                 } else {
    21                     System.out.print(" ");
    22                 }
    23             }
    24             System.out.println();
    25         }
    26         for (int i = rows - 1; i >= 1; i--) {// 此处只需i=rows-1即可
    27             for (int j = 1; j <= rows - i; j++) {
    28                 System.out.print(" ");
    29             }
    30             for (int j = 1; j <= 2 * i - 1; j++) {
    31                 if (i == 1) {
    32                     System.out.print("*");
    33                 } else if (j == 1 || j == 2 * i - 1) {
    34                     System.out.print("*");
    35                 } else {
    36                     System.out.print(" ");
    37                 }
    38             }
    39             System.out.println();
    40         }
    41     }
    42 }
    View Code
  • 相关阅读:
    [剑指Offer] 59.按之字形顺序打印二叉树
    [剑指Offer] 58.对称的二叉树
    [剑指Offer] 57.二叉树的下一个结点
    [剑指Offer] 56.删除链表中重复的结点
    [剑指Offer] 55.链表中环的入口结点
    [计算机网络] C++模拟telnet登陆SMTP服务发送邮件过程
    [计算机网络-应用层] 因特网中的电子邮件
    [计算机网络-应用层] DNS:因特网的目录服务
    [剑指Offer] 54.字符流中的第一个不重复的字符
    [剑指Offer] 53.表示数值的字符串
  • 原文地址:https://www.cnblogs.com/linkchen/p/10453366.html
Copyright © 2011-2022 走看看