1. 程式人生 > >定義方法去返回兩個數中的最大值

定義方法去返回兩個數中的最大值

package _06_java中的方法的定義;


import java.util.Scanner;


//鍵盤錄入兩個資料,返回兩個數中的較大值
public class FunctionTest {
public static void main(String[] args) {
//建立鍵盤錄入物件
Scanner sc = new Scanner(System.in) ;

//錄入資料
System.out.println("請輸入第一個資料:");
int a = sc.nextInt() ;

System.out.println("請輸入第二個資料:");
int b = sc.nextInt() ;

//呼叫求最大值的功能方法進行實現
int max = getMax(a,b);
System.out.println("兩個資料的最大值是:"+max);
}
/*
* 兩個明確:
* 明確1:返回值型別:int
*  明確2:引數型別:int ,2個引數
* */
public static int getMax(int a,int b){
//使用if語句實現
//if(a>b){
//return a ;
//}else {
//return b ;
//}

//三元運算子
//int c = (a>b)? a :b ;
//return c ;
//優化後的
return (a>b)? a: b ;
}
}