zoukankan      html  css  js  c++  java
  • 基础编程练习之打印图形

    1 打印99乘法表

         public static void main(String[] args) {
    		Scanner sc = new Scanner(System.in);
              int num = sc.nextInt();
              print(num); } public static void print(int n){ for(int i=1;i<=n;i++){ for(int j=1;j<=i;j++){ System.out.print(i+"*" +j+"="+i * j+" "); } System.out.println(); } }

    结果

    1*1=1    
    2*1=2    2*2=4    
    3*1=3    3*2=6    3*3=9    
    4*1=4    4*2=8    4*3=12    4*4=16    
    5*1=5    5*2=10    5*3=15    5*4=20    5*5=25    
    6*1=6    6*2=12    6*3=18    6*4=24    6*5=30    6*6=36    

    2 打印矩形框

        public static void main(String[] args) {
            for(int i=0;i<10;i++){
                for(int j=0;j<10;j++){
                    if(i==0 || i==9){
                        System.out.print("*");
                    }else{
                        if(j==0 || j==9){
                            System.out.print("*");
                        }else{
                            System.out.print(" ");
                        }                    
                    }
                }
                System.out.println();
            }

    结果

    **********
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    *        *
    **********

    3  打印菱形

    public static void main(String[] args) {
                printDiamond(10); // 10为对角线长度
            }
    
            /**
             * 打印菱形实现方法
             */
            public static void printDiamond(int size) {
    
                size = (size / 2) * 2; // 菱形对角线两侧的宽度是相同的,所以对角线长度size必定是偶数,(size+1)即为行数和列数
    
                int center = (size / 2); // 以左上角为坐标点(0,0),菱形中心点坐标(center,center)
    
                for (int i = 0; i <= size; i++) { //
                    for (int j = 0; j <= size; j++) { //
                        if (Math.abs(i - center) + Math.abs(j - center) == center) {
                            System.out.print("* ");
                        } else {
                            System.out.print("  ");
                        }
                    }
                    System.out.println();
                }
            }

    结果

              *           
            *   *         
          *       *       
        *           *     
      *               *   
    *                   * 
      *               *   
        *           *     
          *       *       
            *   *         
              *           
  • 相关阅读:
    java bio 之聊天室
    自定义gradle plugin
    gradle build 找不到tools.jar 解决方法
    java switch case 枚举类型的反编译结果
    spring mvc 关键接口 HandlerMapping HandlerAdapter
    H5 Video 去除 下载按钮 禁用右键
    https 请求发送 例子 tls && ssl
    mac 安装mongodb
    (扫盲)DTO数据传输对象
    (扫盲)RPC远程过程调用
  • 原文地址:https://www.cnblogs.com/holos/p/6580878.html
Copyright © 2011-2022 走看看