递归
ps:参数小的情况用递归
//5的阶层 package struct; import static java.lang.System.out; public class Day8 { public static void main(String[] args) { out.println(f(5)); } public static int f(int n){ if(n==1){ return 1; }else{ return n*f(n-1); } } }
//简单计算器 package struct; import java.util.Scanner; public class Day8 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int i = scanner.nextInt(); String str = scanner.next(); int j = scanner.nextInt(); switch (str){ case"+": System.out.println(add(i,j)); break; case "-": System.out.println(sub(i,j)); break; case "*": System.out.println(mult(i,j)); break; case "/": System.out.println(divi(i,j)); break; } scanner.close(); } public static int add(int i,int j){ return i+j; } public static int sub(int i,int j){ return i-j; } public static int mult(int i,int j){ return i*j; } public static double divi(double i,double j){ return i/j; } }
数组
数组的定义
数组声明创建
int[] nums;//定义 nums = new int[10];//创建一个数组nums.length=10 //赋值 nums[1]=1;//下标1的值为1