public class CodeRule {
//声明变量,等号两边有空格。
private static int i = 1;
private static int j = 1;
private static int k = 1;
//多个声明之间,每隔3-4行之间空一行,同种类型的变量应该摆放在一起
private static String a = 1;
private static String b = 1;
//方法声明,右括号和左大括号中间有空格。
public static void main(String[] args) {
//if语句,比较连接符(>)左右有空格,小括号和大括号中间有空格。
//if 与 左括号中间有空格
if (i > 0) {
System.out.println(i);
}
//两个条件的连接(&&),左右有空格。
if (i > 0 && i < 2) {
System.out.println(i);
}
//if..else 语句格式, else另起一行,后仍有空格
if (i == 1) {
System.out.println(i);
}
else {
System.out.println(i);
}
//while语句,与if语句类型,while与括号中间有空格,括号内格式与if相同
while (i > 0 && i < 2) {
System.out.println(i);
i++;
}
//for语句,分号后带空格,每个子语句中,连接符左右都带空格。
//for与括号中间带空格,大小括号中间带空格。
for (int j = 0; j < 10; j++) {
System.out.println(i);
}
//+-*/,格式,四则运算符号前后有空格。
//在JDK的有些代码里,在方法调用的参传递或在判断语句中存在的四则运算中,四则运算符号前后无空格。
//为了不造成困扰和混淆,个人为均保留空格。
//一般的语句,同样为3-4行之间应该空出一行
int a = 1 + 2;
int b = 1 - 2;
int c = 1 * 2;
int d = 1 / 2;
//三元表达式格式,每个符号中间均有空格
int j = i > 2 ? 1 : -1;
//方法声明和调用,用逗号分隔的参数,逗号后有空格。
sum(a, b);
sum(c + d, j);
}
//方法声明,多个参数,逗号后有空格
private static int sum(int i, int j) {
return i + j;
}
//凡是有大括号{}的结构,例如if..else/for/try..catch/方法声明等等,
//两个或以上这样的结构,之间要空一行
//结构内,如果代码只有1-2行,可以考虑不用空行(上下没有空行),2行以上则需在结构{下方加一空行,{上方加一空行
}
//声明变量,等号两边有空格。
private static int i = 1;
private static int j = 1;
private static int k = 1;
//多个声明之间,每隔3-4行之间空一行,同种类型的变量应该摆放在一起
private static String a = 1;
private static String b = 1;
//方法声明,右括号和左大括号中间有空格。
public static void main(String[] args) {
//if语句,比较连接符(>)左右有空格,小括号和大括号中间有空格。
//if 与 左括号中间有空格
if (i > 0) {
System.out.println(i);
}
//两个条件的连接(&&),左右有空格。
if (i > 0 && i < 2) {
System.out.println(i);
}
//if..else 语句格式, else另起一行,后仍有空格
if (i == 1) {
System.out.println(i);
}
else {
System.out.println(i);
}
//while语句,与if语句类型,while与括号中间有空格,括号内格式与if相同
while (i > 0 && i < 2) {
System.out.println(i);
i++;
}
//for语句,分号后带空格,每个子语句中,连接符左右都带空格。
//for与括号中间带空格,大小括号中间带空格。
for (int j = 0; j < 10; j++) {
System.out.println(i);
}
//+-*/,格式,四则运算符号前后有空格。
//在JDK的有些代码里,在方法调用的参传递或在判断语句中存在的四则运算中,四则运算符号前后无空格。
//为了不造成困扰和混淆,个人为均保留空格。
//一般的语句,同样为3-4行之间应该空出一行
int a = 1 + 2;
int b = 1 - 2;
int c = 1 * 2;
int d = 1 / 2;
//三元表达式格式,每个符号中间均有空格
int j = i > 2 ? 1 : -1;
//方法声明和调用,用逗号分隔的参数,逗号后有空格。
sum(a, b);
sum(c + d, j);
}
//方法声明,多个参数,逗号后有空格
private static int sum(int i, int j) {
return i + j;
}
//凡是有大括号{}的结构,例如if..else/for/try..catch/方法声明等等,
//两个或以上这样的结构,之间要空一行
//结构内,如果代码只有1-2行,可以考虑不用空行(上下没有空行),2行以上则需在结构{下方加一空行,{上方加一空行
}