四 方法
4.4命令行传递参数
main方法也可以传递参数 通过cmd 命令提示符运行 先编译 注意包的位置(回退)
public class Demo03 { public static void main(String[] args) { for (int i = 0; i < args.length; i++) { System.out.println(args[i]); } } }
4.5 可变参数(不定项)
int。。。 i 只能在最后面
public class Demo04 { public static void main(String[] args) { Demo04 demo04 = new Demo04(); demo04.test(1,3,4,5,6); } public static void test(int... i) { System.out.println(i[0]); System.out.println(i[1]); System.out.println(i[2]); System.out.println(i[3]); } }
public class Demo05 { public static void main(String[] args) { printMax(1,32,44,3,666); } public static void printMax(double... numbers){ if (numbers.length == 0){ System.out.println("没有值传进来"); return; } int i = 0; double result = numbers[0]; for (i = 1; i < numbers.length; i++) { if (result < numbers[i]){ result = numbers[i]; } } System.out.println("数组中最大值是第" + i +"个" + result); }
4.6 递归
递归深度越大 占用空间越多
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); //递归体 } }
4.7 作业 计算器
加减乘除 循环接收 用户交互
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("1:加法 2:剑法 3:惩罚 4:除法"); System.out.println("请输入要执行的操作"); while (scanner.hasNext()){ int x = scanner.nextInt(); System.out.println("请输入两个数"); double a = scanner.nextDouble(); double b = scanner.nextDouble(); switch (x){ case 1: System.out.println(add(a,b)); break; case 2: System.out.println(jian(a,b)); break; case 3: System.out.println(cheng(a,b)); break; case 4: System.out.println(chu(a,b)); break; default: System.out.println("小猫咪不知道哦"); } System.out.println("请输入要执行的操作"); } } public static double add(double a,double b){ return a+b; } public static double jian(double a,double b){ return a-b; } public static double cheng(double a,double b){ return a*b; } public static double chu(double a,double b){ return a/b; }
add sub multiply div
标签:Java,double,day3,System,static,println,public,out From: https://www.cnblogs.com/piaoguo326/p/17843403.html