方法的重载
命令行传参
实例
public static void main(String[] args) {
//调用可变参数的方法
printMax(2,3,4,5,6);
printMax(new double[]{1,2,3});
}
public static void printMax(double...i){
if (i.length == 0) {
System.out.println("no arguement passed");
return;
}
double result = i[0];
//排序
for (int j = 1; j < i.length; j++) {
if (i[j] > result){
result = i[j];
递归
实例
//递归
//3! 3*2*1
public static void main(String[] args) {
System.out.println(f(3));
}
public static int f(int n){
if (n == 1){
return 1;
}else {
return n*f(n-1);
}
}
家庭作业
加减乘除小程序,思路:
-
利用scannner接收输入的两个数字和运算符
-
利用switch区分用户输入的用算符
-
新建结果变量用于输出最后结果
实例
public class homeWork { public static void main(String[] args) { Scanner inNum01 = new Scanner(System.in); System.out.println("input num01:"); int i = inNum01.nextInt(); Scanner doMath = new Scanner(System.in); System.out.println("input +-*/:"); char grade = doMath.next().charAt(0); Scanner inNum02 = new Scanner(System.in); System.out.println("input num02:"); int j = inNum02.nextInt(); switch (grade) { case '+': int sum = i+j; System.out.println("num1+num2="+sum); break; case '-': int sum01 = i-j; System.out.println("num1-num2=" + sum01); break; case '*': int sum02 = i*j; System.out.println("num1*num2=" + sum02); break; case '/': int sum03 = i/j; System.out.println("num1/num2=" + sum03); break; default: System.out.println("未知参数"); } }