方法
package function;
public class Demo1 {
public static void main(String[] args) {
int a = add(1,2);
System.out.println(a);
System.out.println(add(1,2,3,1.0));
System.out.println(add(1.0,5.2));
}
public static int add(int a,int b){
int sum = 0;
sum = a + b;
return sum;
}
public static double add(double a,double b){
return a+b;
}
public static int add(int a,int b,int c){
return add(a,b) + c;
}
public static double add(int a,int b,int c,double d){
double sum = add(a,b,c);
return sum+d;
}
}
命令行传参
package function;
public class Demo2 {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
System.out.println("args[" + i + "]" + args[i]);
}
}
}
可以通过命令行进行参数传递,javac 类名 编译
然后因为类里面有包名,所以进行回退,回退至包名的上一级文件夹中进行java执行语句java function.Demo2 【传递的参数String类型】
可变参数
package function;
public class Demo3 {
public static void main(String[] args) {
method("this a test","fssd","fdfsdfsd");//传入的那一串参数必须是相同的类型
}
public static void method(String ...a){//可变长参数必须在最后面,
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}
递归
package function;
//递归实现阶层
public class Demo5 {
public static void main(String[] args) {
Demo5 demo5 = new Demo5();
long test = demo5.test(2);
System.out.println(test);
}
public long test(long n){
if (n == 1) {
return 1;
}else {
return test(n-1)*n;
}
}
}
package function;
import java.util.Scanner;
//实现计算器交互
public class Demo4 {
public static void main(String[] args) {
Demo4 demo4 = new Demo4();
Scanner scanner = new Scanner(System.in);
System.out.println("请输入运算数:");
while (scanner.hasNextDouble()){
double a = scanner.nextDouble();
String s = scanner.next();
double b = scanner.nextDouble();
switch (s){
case "+" :
System.out.println(demo4.add(a,b));;
break;
case "-" :
System.out.println(demo4.minus(a,b));
break;
case "*" :
System.out.println(demo4.multiply(a,b));
break;
case "/" :
System.out.println(demo4.except(a,b));
break;
default:
System.out.println("请输入合适字符");
}
System.out.println("请输入任意字符退出!");
}
scanner.close();
}
public double add(double a,double b){
return a+b;
}
public double minus(double a,double b){
return a-b;
}
public double multiply(double a,double b){
return a*b;
}
public double except(double a,double b){
return a/b;
}
}
标签:int,double,System,println,方法,public,out
From: https://www.cnblogs.com/1234sdg/p/17018312.html