定义一个方法,该方法能够找出三个整数中的最大值并返回。在主方法中调用方法测试执行...

定义方法:

public int findMax(int a, int b, int c) {
  int max = a;
  if (b > max) {
    max = b;
  }
  if (c > max) {
    max = c;
  }
  return max;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 定义方法: ``` public int findMax(int a, int b, int c) { int max = a; if (b > max) { max = b; } if (c > max) { max = c; } return max; } ``` 在方法调用方法测试执行: ``` public static void main(String[] args) { int result = findMax(10, 20, 30); System.out.println("最大值是:" + result); } ``` 输出结果: ``` 最大值是:30 ``` ### 回答2: 可以定义一个名为`findMax`的方法,该方法接收三个整数参数`a`、`b`和`c`,并返回三个整数最大值方法的具体实现可以使用三目运算符`? :`,判断`a`、`b`和`c`之间的关系,返回最大值。 以下是具体的代码实现: ```java public class Main { public static int findMax(int a, int b, int c) { return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c); } public static void main(String[] args) { int a = 10; int b = 20; int c = 15; int max = findMax(a, b, c); System.out.println("最大值:" + max); } } ``` 在方法调用`findMax`方法,传入三个整数参数,将返回最大值赋值给`max`变量,并打印输出。以上代码输出结果为: ``` 最大值:20 ``` 这样就能找出三个整数最大值返回。 ### 回答3: 定义一个名为findMax的方法,该方法接受三个整数参数a、b和c,并返回最大值方法实现如下: //定义方法找出三个整数最大值返回 public static int findMax(int a, int b, int c){ int max = a; //假设a为最大值 if(b > max){ max = b; } if(c > max){ max = c; } return max; } 在方法调用方法进行测试: public static void main(String[] args) { int a = 10; int b = 20; int c = 15; int max = findMax(a, b, c); System.out.println("三个整数最大值为:" + max); } 输出结果为:三个整数最大值为:20。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值