写一个Java 方法 JOPtinPane.showInputDialog();
加一个meaningful变量名字
加一个注释 解释每行的方法 public static void badMethod(int[] x) {
int x1 =0,x2 = 0;
int p = x[0], q=x[0];
for(int i = 0; i<x.length;i++) {
if (x[i]%2 == 0)
x1 = x1 +x[i];
else
x2 = x2+x[i];
if(x[i]>p)
p =x[i];
if(x[i]<q)
q=x[i];
}
S.O.P("x1 is “+x1);
S.O.P("x2 is “+x2);
S.O.P("p is “+p);
S.O.P("q is “+q);
加一个meaningful变量名字
加一个注释 解释每行的方法 public static void badMethod(int[] x) {
int x1 =0,x2 = 0;
int p = x[0], q=x[0];
for(int i = 0; i<x.length;i++) {
if (x[i]%2 == 0)
x1 = x1 +x[i];
else
x2 = x2+x[i];
if(x[i]>p)
p =x[i];
if(x[i]<q)
q=x[i];
}
S.O.P("x1 is “+x1);
S.O.P("x2 is “+x2);
S.O.P("p is “+p);
S.O.P("q is “+q);
S.O.P("x2 is “+x2);
S.O.P("p is “+p);
S.O.P("q is “+q);S.O.P是?
* 计算所有偶数之和和所有奇数之和,并把他们以及最大值最小值打印出来。
*
* @参数int x[]是输入的数组
*/
public static void main(int[] x) {
int x1 = 0, x2 = 0; //x1,x2分别记录偶数和是奇数和
int p = x[0], q = x[0]; //p,q分别记录最大值和最小值
for (int i = 0; i < x.length; i++) { //循环遍历数组
if (x[i] % 2 == 0) //当x[i]除以2的余数为0时则把它判断为偶数
x1 = x1 + x[i]; //偶数++
else
x2 = x2 + x[i];
if (x[i] > p) //如果该数大于当前最大值则把它赋给P
p = x[i];
if (x[i] < q) //如果该数小于当前最小值则把它赋给q
q = x[i];
}
System.out.println("x1 is " + x1);
System.out.println("x2 is " + x2);
System.out.println("p is " + p);
System.out.println("q is " + q);
}}
appropriately indent the code
add meaningful variable names
add a few lines of comments prior to your method to explain what it does.
public static void goodMethod(int[] nums) {
if (nums == null || nums.length == 0) {
throw new IllegalArgumentException("Argument is null!");
}
int even = 0; //偶数之和
int odd = 0; //奇数之和
int max = nums[0]; //最大数
int min = nums[0]; //最小数
for (int i = 0; i < nums.length; i++) {
if (nums[i] % 2 == 0) {
even += nums[i];
} else {
odd += nums[i];
}
if (nums[i] > max) {
max = nums[i];
}
if (nums[i] < min) {
min = nums[i];
}
}
System.out.println("even number sum is " + even);
System.out.println("odd number sum is " + odd);
System.out.println("Max is " + max);
System.out.println("Min is " + min);
}