1.写方法,给方法传递两个整数,返回a和b中比较大的那个数。
法1:
public class Homework3_1_1 {
public static int compare(int a, int b) {
if (a > b) return a;
else return b;
}
public static void main(String[] args) {
System.out.println(compare(1, 6));
}
}
法2:
public class Homework3_1 {
public static int compare(int a, int b) {
int m = (a > b) ? a : b;
return m;
}
public static void main(String[] args) {
System.out.println(compare(1, 6));
}
}
2.写方法,把传入的数组翻转并返回。
public class Homework3_2 {
public static int[] reverse(int[] arr) {
int[] ret = new int[arr.length];
int index = 0;
for (int i = arr.length - 1; i >= 0; i--) {
ret[index] = arr[i];
index++;
}
return ret;
}
public static void main(String[] args) {
int[] a = {1, 5, 6, 3, 2};
int[] b = reverse(a);
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
}
}