A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 实践出真知 中级黑马   /  2015-1-18 22:56  /  1990 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
从数组中组选取最大值
例;{1,3,5,6,8,4,9}
思路:
1、数组中选取最大值,先定义一个功能,获取数组的最大值,
函数有返回值,返回值类型为int,数组参数类型为int型
2、数组中两个元素进行比较,用到for循环,
3、循环中两个元素进行大小比较,用if语句判断大小,返回最大值
4,返回值被主函数调用,然后输出打印
*/
class max
{
        public static void main(String[]args)
        {
            int arr={1,3,5,6,8,4,9};
                int max=getMax(arr);
                System.out.println("max"+max);
        }
}
        public static int getMax(int arr[])
        {
                int max=0;
                for (int x=1; x<arr.length;x++ )
                {
                        if (max<arr[x])
                        {
                                max=arr[x];
                        }
                        return max;
                }
        }

8 个回复

倒序浏览
代码是你自己敲到输入框上的吗。。。好多错误啊。。
  1. class Demo
  2. {
  3.         public static void main(String[]args)
  4.         {
  5.             int[] arr={1,3,5,6,8,4,9};
  6.                 int max=getMax(arr);
  7.                 System.out.println("max="+max);
  8.         }

  9.         public static int getMax(int arr[])
  10.         {
  11.                 int max=0;
  12.                 for (int x=1; x<arr.length;x++ )
  13.                 {
  14.                         if (max<arr[x])
  15.                         {
  16.                                 max=arr[x];
  17.                         }
  18.                        // return max;
  19.                 }
  20.                                 return max;
  21.         }
  22. }
复制代码
回复 使用道具 举报
磊哥  你是故意让我们找错误的吗?
回复 使用道具 举报
史云龙 发表于 2015-1-18 23:02
代码是你自己敲到输入框上的吗。。。好多错误啊。。

哥们  你这错误也不少啊
如果如果数组为负数
那么你拿int max=0作比较 还能比出来吗
回复 使用道具 举报
  1. public class Demo2{
  2.                 public static void main(String[]args)
  3.                 {
  4.                     int[] arr={1,3,5,6,8,4,9};
  5.                         int max=getMax(arr);
  6.                         System.out.println("max="+max);
  7.                 }

  8.                 public static int getMax(int arr[])
  9.                 {
  10.                         int max=0;
  11.                         for (; max<arr.length-1;max++ )
  12.                         {
  13.                                 if (arr[max]<arr[max+1])
  14.                                 {
  15.                                         arr[max] = arr[max+1];
  16.                                 }
  17.                                 else arr[max+1] = arr[max];
  18.                         }
  19.                                         return arr[max];
  20.                 }
  21.         }
复制代码

楼主的代码有错误,二楼同学代码也不是很完善,如果数组内出现全部是负数的情况,你的代码就会输出  0   。
回复 使用道具 举报
路尽鹭飞 发表于 2015-1-19 00:35
楼主的代码有错误,二楼同学代码也不是很完善,如果数组内出现全部是负数的情况,你的代码就会输出  0    ...

代码是不错  但是不规范  注释呢?说好的阅读性呢?
回复 使用道具 举报 0 1
拜托,干嘛要用中间值,用数组下标比不是很好吗
回复 使用道具 举报
本帖最后由 Novice.Jin 于 2015-1-19 16:15 编辑
  1. /**
  2. * 该类用于提取数组中的最大值
  3. * @author Novice
  4. *
  5. */

  6. class GetMax{
  7.         private int[] arr;
  8.         private GetMax(){}//必须传入一个数组,才能创建该类
  9.         /**
  10.          * 创建该类对象时,需要传入一个整形数组
  11.          * @param arr for int[10]
  12.          */
  13.         GetMax(int[] arr){//构造函数,在创建对象时接收一个数组
  14.                 this.arr = arr;
  15.         }
  16.         /**
  17.          * 该方法用于提取整形数组中的最大值
  18.          * @return int for int[]
  19.          */
  20.         public int getMax(){
  21.                 int max = 0;//用于存放数组中最大数组的下表
  22.                 for (int i = 0 ; i < this.arr.length-1 ; i++){//循环判断,不多做解释
  23.                         if (this.arr[max] < this.arr[i+1]){
  24.                                 max = i+1;
  25.                         }
  26.                 }
  27.                 return this.arr[max];//返回一个最大的整数,该整数来自于一个整形数组
  28.         }
  29. }

  30. public class Demo {

  31.         public static void main(String[] args) {
  32.                 // TODO Auto-generated method stub
  33.                 int[] arr = {1,2,3,4,5,6,7,8,9,10};
  34.                 GetMax getMax = new GetMax(arr);
  35.                 System.out.println(getMax.getMax());

  36.         }

  37. }
复制代码


回复 使用道具 举报
这样bug比较少
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马