天天看點

Java輸入兩個整數,調用函數,比較最大值

Java輸入兩個整數,調用函數,比較最大值

package com.java;

import java.util.Scanner;

public class HelloWorld {
    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);
    }

    //獲得兩個數的較大值
    public static int getMax(int a,int b){
        if(a>b){
            return a;
        }else{
            return b;
        }
    }
}