zoukankan      html  css  js  c++  java
  • java代码格式

    1. /**   
    2.  * Java编码格式个人推荐,参考JDK源码和Hyperic HQ源码(原spring旗下著名开源软件,现vmware)。   
    3.  * @author 
    4. *  
    5.  */   
    6. public class CodeRule {   
    7.        
    8.     //声明变量,等号两边有空格。   
    9.     private static int i = 1;   
    10.        
    11.     //方法声明,右括号和左大括号中间有空格。   
    12.     public static void main(String[] args) {   
    13.         //if语句,比较连接符(>)左右有空格,小括号和大括号中间有空格。   
    14.         //if 与 左括号中间有空格   
    15.         if (i > 0) {   
    16.             System.out.println(i);   
    17.         }   
    18.         //两个条件的连接(&&),左右有空格。   
    19.         if (i > 0 && i < 2) {   
    20.             System.out.println(i);   
    21.         }   
    22.            
    23.         //if..else 语句两种格式   
    24.         //1.参考JDK,个人使用方式,else跟大括号,前后都有空格   
    25.         if (i > 0 && i < 2) {   
    26.             System.out.println(i);   
    27.         } else if (i > 2) {   
    28.             System.out.println(i + 1);   
    29.         } else {   
    30.             System.out.println(i);   
    31.         }   
    32.         //2.参考Hyperic HQ源码, else另起一行,后仍有空格   
    33.          if (i == 1) {   
    34.              System.out.println(i);   
    35.          }   
    36.          else {   
    37.              System.out.println(i);   
    38.          }   
    39.             
    40.          //while语句,与if语句类型,while与括号中间有空格,括号内格式与if相同   
    41.          while (i > 0 && i < 2) {   
    42.              System.out.println(i);   
    43.              i++;   
    44.          }   
    45.             
    46.          //for语句,两种格式   
    47.          //1.参考Hyperic HQ,个人使用方式。分号后带空格,每个子语句中,连接符左右都带空格。   
    48.          //for与括号中间带空格,大小括号中间带空格。   
    49.          for (int j = 0; j < 10; j++) {   
    50.              System.out.println(i);   
    51.          }   
    52.          //2.参考JDK,区别在于子语句中,连接符左右无空格。   
    53.          for (int j=0; j<10; j++) {   
    54.              System.out.println(i);   
    55.          }   
    56.             
    57.          //+-*/,格式,四则运算符号前后有空格。   
    58.          //在JDK的有些代码里,在方法调用的参传递或在判断语句中存在的四则运算中,四则运算符号前后无空格。   
    59.          //为了不造成困扰和混淆,个人为均保留空格。   
    60.          int a = 1 + 2;   
    61.          int b = 1 - 2;   
    62.          int c = 1 * 2;   
    63.          int d = 1 / 2;   
    64.             
    65.          //三元表达式格式,每个符号中间均有空格   
    66.          int j = i > 2 ? 1 : -1;   
    67.             
    68.          //方法声明和调用,用逗号分隔的参数,逗号后有空格。   
    69.          sum(a, b);   
    70.          sum(c + d, j);   
    71.     }   
    72.        
    73.     //方法声明,多个参数,逗号后有空格   
    74.     private static int sum(int i, int j) {   
    75.         return i + j;   
    76.     }   
    77.        
    78.    
    79. }  
  • 相关阅读:
    [模板] 循环数组的最大子段和
    [最短路][几何][牛客] [国庆集训派对1]-L-New Game
    [洛谷] P1866 编号
    1115 Counting Nodes in a BST (30 分)
    1106 Lowest Price in Supply Chain (25 分)
    1094 The Largest Generation (25 分)
    1090 Highest Price in Supply Chain (25 分)
    树的遍历
    1086 Tree Traversals Again (25 分)
    1079 Total Sales of Supply Chain (25 分 树
  • 原文地址:https://www.cnblogs.com/gaoguofeng/p/5474868.html
Copyright © 2011-2022 走看看