Java 使用 if else 多种方法比较 最大值 || 最小值

文章说明*

本篇文章介绍如何使用【if else 比较大小 】,仅是对自己学习的记录,并没有深入分析每个模块。感谢支持!

方法一:

		int a = 5;
		int b = 10;
		int c = 15;

		int Max = 0;
		if (a > b && a > c) {
			Max = a;
		}
		if (b > a && b > c) {
			Max = b;
		} else {
			Max = c;
		}
		System.out.println(Max);

方法二:

		int a = 5;
		int b = 10;
		int c = 15;

		int Max = a;

		if (Max < b) {
			Max = b;
		}
		if (Max < c) {
			Max = c;
		}
		System.out.println(Max);

方法三

		int a = 5;
		int b = 10;
		int c = 15;

		int Max = 0;
		if (a > b) {
			if (a > c) {
				Max = a;
			} else {
				Max = c;
			}
		} else {
			if (b > c) {
				Max = b;
			} else {
				Max = c;
			}

		}
		System.out.println(Max);

方法四:

		int a = 5;
		int b = 10;
		int c = 15;
		
		int Max=0;
		if (a>b) {
			Max=a;
		}else {
			Max=b;
		}
		if (Max<c) {
			Max=c;
		}
		System.out.println(Max);

三元运算符比较:

        int a = 55;
        int b = 63;
        int c = 88;
        String Max = (a > b && a > c) ? "a大" : (b > c && b < c) ? "b" : "c";
        System.out.println(Max);

        int aa = 55;
        int bb = 63;
        int cc = 88;
        int Max1 = (aa > bb) ? aa : bb;
        int Max2 = (Max1 > cc) ? Max1 : cc;
        System.out.println(Max2);
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值