函数小练习

三个数比较最大值

public static void main(String[] args) {
	
	//设置键盘录入
	Scanner sc=new Scanner(System.in);
	
	System.out.println("请输入第1个数字:");
	int a=sc.nextInt();
	System.out.println("请输入第2个数字:");
	int b=sc.nextInt();
	System.out.println("请输入第3个数字:");
	int c=sc.nextInt();
	
	//设置变量max(这个变量名也可以改成别的,作用域不同,不受影响),来接受max方法的返回值
	int max=max(a,b,c);
	System.out.println(max);
	
}

public static int max(int a,int b,int c){
	
	//如果a>b且a>c,返回值是a
	if(a>b&&a>c){
		return a;
	//如果b>a且b>c,返回值是b
	}else if(b>a&&b>c){
		return b;
	//剩下的情况都返回c
	}else{
		return c;
	}
	
}

比较是否相等

public static boolean max(int a,int b) {
	/*
	 * 第一种,方法中的返回值写int,第二种,返回值写boolean
	 * int c =1; 
	 * int d=0; 
	 * if(a==b){
	 * 		 return c; }else{
	 *   return d; }
	 */

	if (a == b) {

		return true;
	} else {

		return false;

	}

猜你喜欢

转载自blog.csdn.net/qq_44063001/article/details/85001221