zoukankan      html  css  js  c++  java
  • Java小程序之输出星号

    题目:打印出如下图案(菱形) 
        * 
       *** 
     ****** 
    ******** 
     ****** 
      *** 
       * 

    编程工具使用eclipse

    代码如下:

    package test;
    
    public class starsList {
        public int totaLines = 0; // 最大行数
    
        public static void main(String [] args){
            starsList aaa = new starsList();
            aaa.setNum(17,17);// 第一个是要显示的行数。第二个是总行数
        }
    
        // 单行星号打印(空格和星号)
        public void prints(int num,int totals){
            String string = "";
            int lang = totals-num>=0 ? num : num-(num-totals)*2;
            
            for(int a=0;a<lang;a++){
                string +="*";
            }
            
            int space = (totals-lang)/2;
            for(int b=0;b<space;b++){
                string = " "+string;
                string = string+" ";
            }
            
            System.out.println(string);
            System.out.println('
    ');
        }
        // 根据打印行数计算每行星号个数并调用单行打印
        public void setNum(int lines,int totals2){
            if(lines>totals2){
                System.out.println("要显示的行数("+lines+") 大于总行数("+totals2+")");
                return;
            }
            starsList bb = new starsList();
            for(int c = 0;c<lines;c++){
                int n = (2*c)+1;
                bb.prints(n,totals2);
            }
        }
    }
  • 相关阅读:
    JAVA面试基础
    扔硬币问题
    随机数生成随机数
    囚犯猜帽子问题
    十道智力题(三)
    十道智力题(二)
    十道智力题(一)
    lintcode:排颜色 II
    机器学习中的几个常见概念(持续更新中......)
    如何打印一棵树(Java)
  • 原文地址:https://www.cnblogs.com/wwlhome/p/7300375.html
Copyright © 2011-2022 走看看