黑马程序员技术交流社区

标题: 三个数中求最大 [打印本页]

作者: Java1211张美红    时间: 2015-12-16 09:40
标题: 三个数中求最大
方法一:
               if (x1 > x2 & x1 > x3) {
                        System.out.println("x1是最大值" + x1);
                }else if (x2 > x1 & x2 > x3 ) {
                        System.out.println("x2是最大值" + x2);
                }else if (x3 > x1 & x3 > x2 ) {
                        System.out.println("x3是最大值" + x3);
                }
方法二:
                 if ( x1 > x2 ) {
                        if ( x3 > x1) {
                                System.out.println("x3是最大值" + x3);
                        }else {
                                System.out.println("x1是最大值" + x1);
                        }
                }else {
                        if ( x3 > x2) {
                                System.out.println("x3是最大值" + x3);
                        }else {
                                System.out.println("x2是最大值" + x2);
                        }
                }
方法三:
               int temp,max;

                temp = (x1 > x2) ? x1 : x2;
                max = (temp > x3) ? temp : x3;

                System.out.println(max + "是最大数字");
作者: li_joy99    时间: 2015-12-16 10:22
考虑相等的情况
作者: 李永佳    时间: 2015-12-16 11:59
放到数组中不是更方便吗?
作者: 胖小子    时间: 2015-12-16 20:12
int   max  ;
max= (a>b)?a:b;
max = (max>c)? max:c;
作者: 姚成晖    时间: 2015-12-17 20:08
需要考虑3个数中2个数 或者三个数都相等的情况~
作者: 我有一个邮箱了    时间: 2015-12-17 21:47
int   max  ;
max= (a>b)?a:b;
max = (max>c)? max:c;
这个比较简单
作者: Java1211张美红    时间: 2015-12-17 22:14
李永佳 发表于 2015-12-16 11:59
放到数组中不是更方便吗?

放到数组,怎么弄?
作者: Java1211张美红    时间: 2015-12-17 22:17
li_joy99 发表于 2015-12-16 10:22
考虑相等的情况

                int temp,max;

                temp = (x1 >= x2) ? x1 : x2;
                max = (temp >= x3) ? temp : x3;

                System.out.println(max + "是最大数字");
加个等号就行了吧!
作者: Java1211张美红    时间: 2015-12-17 22:18
我有一个邮箱了 发表于 2015-12-17 21:47
int   max  ;
max= (a>b)?a:b;
max = (max>c)? max:c;

对,三元运算符看起来比较简洁!
作者: 愿爱无忧    时间: 2015-12-17 22:28
真勤劳呀,我还是初级黑马呢,
作者: 我有一个邮箱了    时间: 2015-12-18 21:41
求任意多整数中的最大值和最小值:
只需要将想要比较的值放如数组中
class MaxMin {
public static void main(String[] args) {
                int[] arr ={45,67,24,89,33,56,95};
                int max = getMax(arr);
                System.out.println("最大值: "+max);
                int min = getMin(arr);
                System.out.println("最大值: "+min);

        }
        public static  int getMax(int[] arr){//获取数组中最大值
                int max = arr[0];
                for (int i =1;i <arr.length ;i++ ) {
                        max = max < arr[i]? arr[i]:max;
                }
                return max;
        }
        public static  int getMin(int[] arr){//获取方法中最小值
                int min = arr[0];
                for (int i =1;i <arr.length ;i++ ) {
                        min = min > arr[i]? arr[i]:min;
                }
                return min;
        }
}

作者: anuo    时间: 2015-12-18 22:02
三元运算
作者: 正阳门下    时间: 2015-12-18 23:01
李永佳 发表于 2015-12-16 11:59
放到数组中不是更方便吗?

三个数还是不用数组了
作者: 正阳门下    时间: 2015-12-18 23:10
相等的情况其实不用考虑
作者: 李放    时间: 2015-12-18 23:19
支持楼主!~
作者: 天若也易老    时间: 2015-12-18 23:29
这个如何
Math.max(int z,Math.max(int x,int y))

作者: 奔跑着    时间: 2015-12-19 02:00
我有一个邮箱了 发表于 2015-12-18 21:41
求任意多整数中的最大值和最小值:
只需要将想要比较的值放如数组中
class MaxMin {

数组的话直接用sort()排序方法,然后arr[0]就是最小值,arr[arr.length-1] 就是最大值,




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2