输入两个数,输出最大的那个数

编写一个Java程序,用来比较两个整数的大小,并输出最大的那个数

第一种写法:

package demo;
import java.util.Scanner;
public class GetMax {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入两个数:");
        int x = sc.nextInt();
        int y = sc.nextInt();
        System.out.println("最大数是"+getMax(x,y));
    }
    public static int getMax(int a,int b){
        int max;
        if(a > b){
            max = a;
        }
        else{
            max = b;
        }
        return max;
    }
}

第二种写法函数体:

 public static int getMax(int a,int b)
    {
        int max = 0;
        if (a > b) {
            return a;
        }
        else{
            return b;
        }
    }

第三种写法函数体:

 public static int getMax(int a,int b)
    {
      int max;
      max = a>b?a:b;
      return max;
    }
发布了8 篇原创文章 · 获赞 11 · 访问量 131

猜你喜欢

转载自blog.csdn.net/qq_43428938/article/details/105318518
今日推荐