Java基础知识体系-基础语法-08

//方法的演示
public class MethodDemo {
	
	public static void main(String[] args) {
		say();
		say();
		System.out.println(Math.sqrt(25));
		
		//sayHi();//编译错误,有参必须传参
		//sayHi(250);//编译错误,参数类型不匹配
		sayHi("zhangsan");// String name = "zhangsan"
		sayHi("lisi");// String name = "lisi"
		sayHi("wangwu");//String name = "wangwu"
		
		int  a = getNum();//getNum()的值就是return后的那个数
		System.out.println(a);//88
		
		double b =plus(55,66);//double a =55,double b=66
		System.out.println(b);
//		double num1 = 5.5,num2 = 6.6;
//		double b = plus(num1,num2);//double a =5.5,double b =6.6
//		System.out.println(b);
		System.out.println("over");
	}
	
	//有参有返回值
	public static double plus(double a,double b){
		double sum = a + b;
		return sum;//返回sum中的那个数
		//return a+b;//返回a+b的值
	}
	
	//无参有返回值
	public static int getNum(){
		//return;//编译错误,必须返回一个值
		//return 8.88;//编译错误,返回值类型不匹配
		return 88;//1.结束方法的执行,2.返回一个结果给调用方
		
	}
	
	//有参无返回值---有参可以使用方法更灵活
	public static void sayHi(String name){
		System.out.println("大家好,我叫"+name);
		return;//1.结束方法的执行
	}
	
	//无参无返回值
	public static void say(){
		System.out.println("大家好,我叫WKJ");
	}

}

你可能感兴趣的:(Java)