黑马程序员技术交流社区

标题: 输出菱形星星图案 [打印本页]

作者: nanliner    时间: 2016-5-30 00:22
标题: 输出菱形星星图案
输出菱形星形
      *
     * *
    * * *
   * * * *
  * * * * *
* * * * * *
* * * * * * *
* * * * * *
  * * * * *
   * * * *
    * * *
     * *
      *
  1. class LingXing {
  2.         public static void main(String[] args) {
  3.                 for(int x=1;x<=13;x++){
  4.                         for(int m=0;m<=7-x;m++){
  5.                                 System.out.print(" ");
  6.                         }
  7.                         if(x>7){
  8.                                 for(int n=0;n<=x-7;n++){
  9.                                         System.out.print(" ");
  10.                                 }
  11.                         }
  12.                         for(int y=1;y<=x&&y<=14-x;y++){
  13.                                 System.out.print("*"+" ");
  14.                         }
  15.                         System.out.println();
  16.                 }
  17.         }
  18. }

  19. 改进
  20. 键盘录入数据
  21. import java.util.Scanner;
  22. class LingXing {
  23.         public static void main(String[] args) {
  24.                 Scanner sc = new Scanner(System.in);
  25.                 System.out.println("请输入数据:");
  26.                 int a = sc.nextInt();
  27.                 if(a%2==0){
  28.                         for(int x=1;x<=a;x++){
  29.                                 for(int m=0;m<=(a)/2-x;m++){
  30.                                         System.out.print(" ");
  31.                                 }
  32.                                 if(x>(a)/2){
  33.                                         for(int n=0;n<=x-((a)/2);n++){
  34.                                                 System.out.print(" ");
  35.                                         }
  36.                                 }
  37.                                 for(int y=1;y<=x&&y<=(a)-x;y++){
  38.                                         System.out.print("*"+" ");
  39.                                 }
  40.                                 System.out.println();
  41.                         }
  42.                 }else{
  43.                         for(int x=1;x<=a;x++){
  44.                                 for(int m=0;m<=(a+1)/2-x;m++){
  45.                                         System.out.print(" ");
  46.                                 }
  47.                                 if(x>(a+1)/2){
  48.                                         for(int n=0;n<=x-((a+1)/2);n++){
  49.                                                 System.out.print(" ");
  50.                                         }
  51.                                 }
  52.                                 for(int y=1;y<=x&&y<=(a+1)-x;y++){
  53.                                         System.out.print("*"+" ");
  54.                                 }
  55.                                 System.out.println();
  56.                         }
  57.                 }
  58.         }
  59. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2