method
System.out.println("");
System是类
out是对象
printIn是方法
public class Demo1 {
public static void main(String[] args) {
int num = num(1, 3);
System.out.println(num);
}
//修饰符+返回值类型+方法名+()+{}
public static int num(int i, int j){
return i+j;
}
}
()里面的是参数
return后面的事返回值
可变参数
本质就是数组
你能写在数据类型最后面,而且只能写一个
递归
尽量少用
public static void main(String[] args) {
Demo3 demo3 = new Demo3();
int i = demo3.text(5);
System.out.println(i);
}
public static int text(int n){
if (n==1){
return 1;
}else {
return n*(text(n-1));
}
}
计算器
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入一个数字");
double a = scanner.nextDouble();
System.out.println("在输入一个标点符号");
String b = scanner.next();
System.out.println("请输入一个数字");
double c = scanner.nextDouble();
switch (b) {
case "+":
System.out.println(add(a, c));
break;
case "-":
System.out.println(minus(a, c));
break;
case "*":
System.out.println(multiply(a, c));
break;
default:
System.out.println(except(a, c));
}
}
public static double add(double a, double b) {
return a + b;
}
public static double minus(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double except(double a, double b) {
if (b==0){
System.out.println("被除数不能为0");
}
return a/b;
}