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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 余尚勇 中级黑马   /  2013-6-23 23:01  /  2173 人查看  /  16 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 余尚勇 于 2013-6-26 23:21 编辑

帮忙看下代码,感觉没错,结果却是0,不知道哪里错了?
#region 从键盘接收用户输入的数字并求最大值
        Console.WriteLine("请输入你要求的数字的个数:");
            int num = int.Parse(Console.ReadLine());
            int[] scores = new int[num];
            int max = scores[0];
            for (int i = 0; i < scores.Length; i++)
            {
               
                Console.WriteLine("请输入第{0}个人的成绩:",i+1);
                int score = int.Parse(Console.ReadLine());
                for (int j = 0; j < scores.Length; j++)
                {
                    if (scores > max)
                        max = scores;
                }

            }
            Console.WriteLine("最大数是:{0}",max);
            #endregion
                Console.ReadKey();

360软件小助手截图20130623225639.jpg (48.06 KB, 下载次数: 0)

360软件小助手截图20130623225639.jpg

16 个回复

倒序浏览
本帖最后由 黄文超 于 2013-6-23 23:11 编辑

LZ你确定你的代码能够编译?
注意10行和13、14行
  1.             #region 从键盘接收用户输入的数字并求最大值
  2.             Console.WriteLine("请输入你要求的数字的个数:");
  3.             int num = int.Parse(Console.ReadLine());
  4.             int[] scores = new int[num];
  5.             int max = scores[0];
  6.             for (int i = 0; i < scores.Length; i++)
  7.             {

  8.                 Console.WriteLine("请输入第{0}个人的成绩:", i + 1);
  9.                 scores[i] = int.Parse(Console.ReadLine());
  10.                 for (int j = 0; j < scores.Length; j++)
  11.                 {
  12.                     if (scores[j] > max)
  13.                         max = scores[j];
  14.                 }

  15.             }
  16.             Console.WriteLine("最大数是:{0}", max);
  17.             #endregion
  18.             Console.ReadKey();
复制代码
回复 使用道具 举报 1 0
本帖最后由 nilaoyededie 于 2013-6-23 23:32 编辑

#region 从键盘接收用户输入的数字并求最大值
        Console.WriteLine("请输入你要求的数字的个数:");
             int num = int.Parse(Console.ReadLine());
             int[] scores = new int[num];
             int max = scores[0];
             for (int i = 0; i < scores.Length; i++)
             {
                 
                 Console.WriteLine("请输入第{0}个人的成绩:",i+1);
                 //int score = int.Parse(Console.ReadLine());  这里错了,应为score【i】打不出英文的中括号了
                 for (int j = 0; j < scores.Length; j++)
                 {
                     if (scores > max)  还有这里改为score【j】
                         max = scores; 还有这里改为score【j】
                 }

             }
             Console.WriteLine("最大数是:{0}",max);
             #endregion
回复 使用道具 举报
黄文超 发表于 2013-6-23 23:09
LZ你确定你的代码能够编译?
注意10行和13、14行

不行啊,scores中的i改成 j 也还是0啊
#region 从键盘接收用户输入的数字并求最大值
            Console.WriteLine("请输入你要求的数字的个数:");
            int num = int.Parse(Console.ReadLine());
            int[] scores = new int[num];
            int max = scores[0];
            for (int i = 0; i < scores.Length; i++)
            {
               
                Console.WriteLine("请输入第{0}个人的成绩:",i+1);
                int score = int.Parse(Console.ReadLine());
                for (int j = 0; j < scores.Length; j++)
                {
                    if (scores[ j ] > max)
                        max = scores[ j ];
                }

            }
            Console.WriteLine("最大数是:{0}",max);
            #endregion
                Console.ReadKey();

112.jpg (41.13 KB, 下载次数: 0)

112.jpg
回复 使用道具 举报
不错哈,。,。赞同上面的写法,
回复 使用道具 举报
本帖最后由 nilaoyededie 于 2013-6-23 23:32 编辑

我打不出来[]了
把那个 score改为score【i】
回复 使用道具 举报
陈壹 中级黑马 2013-6-24 01:05:47
7#
本帖最后由 陈壹 于 2013-6-24 09:32 编辑
余尚勇 发表于 2013-6-23 23:17
不行啊,scores中的i改成 j 也还是0啊
#region 从键盘接收用户输入的数字并求最大值
            Console ...


会行才怪,scores数组从创建到结束都没有被赋值,都是默认为0,max=scores[0];当然值为0,
int score = int.Parse(Console.ReadLine());应改成
scores = int.Parse(Console.ReadLine());
然后把第二个for循环去掉,直接在后面接上
if (scores[ i ] > max)
                        max = scores[ i ];
                }


回复 使用道具 举报
高腾 中级黑马 2013-6-24 08:21:25
8#
在你基础上改的,其实写的不严谨 没有做一些非法判断
  1.             Console.WriteLine("请输入你要求的数字的个数:");
  2.             int num = int.Parse(Console.ReadLine());
  3.             int max = 0;
  4.             for (int i = 0; i < num; i++)
  5.             {
  6.                 Console.WriteLine("请输入第{0}个人的成绩:", i + 1);
  7.                 int score = int.Parse(Console.ReadLine());
  8.                 if (score > max)
  9.                     max = score;
  10.             }
  11.             Console.WriteLine("最大数是:{0}", max);
  12.             Console.ReadKey();
复制代码
回复 使用道具 举报
static void Main(string[] args)
        {
            Console.WriteLine("请输入你要求的数字的个数:");
            int num = int.Parse(Console.ReadLine());
            int[] scores = new int[num];
            int max = scores[0];
            for (int i = 0; i < scores.Length; i++)
            {
               
                Console.WriteLine("请输入第{0}个人的成绩:",i+1);
                int score = int.Parse(Console.ReadLine());
                for (int j = 0; j < scores.Length; j++)
                {
                    if (score> max)        //此处应用变量score和max比较,数组scores无法和int类型值比较。
                        max = score;
                }

            }
            Console.WriteLine("最大数是:{0}",max);
                Console.ReadKey();
        }
    }
}
回复 使用道具 举报
  1. Console.WriteLine("请输入你要求的数字的个数:");
  2.                         int num = int.Parse(Console.ReadLine());
  3.                         int score = 0;
  4.                         int max = 0;
  5.                         for (int i = 0; i < num; i++)
  6.                         {

  7.                                 Console.WriteLine("请输入第{0}个人的成绩:", i + 1);

  8.                                 score = int.Parse(Console.ReadLine());

  9.                                 if (score > max)
  10.                                         max = score;

  11.                         }
  12.                         Console.WriteLine("最大数是:{0}", max);

  13.                         Console.ReadKey();
复制代码

评分

参与人数 1技术分 +1 收起 理由
杞文明 + 1

查看全部评分

回复 使用道具 举报
把里面那个for循环拿出来,并在循环的时候带上角标。参考代码如下:
static void Main(string[] args)
        {
            Console.WriteLine("请输入你要求的数字的个数:");
            int num = ReadInt();
            int[] scores = new int[num];
            int max = scores[0];
            for (int i = 0; i < scores.Length; i++)//给用户输入想输入的各个数字
            {
                Console.WriteLine("请输入第{0}个人的成绩:",i+1);
                scores[i] = ReadInt();
            }
            for (int j = 0; j < scores.Length; j++)//把最大值找出来
            {
                if (scores[j] > max)
                {
                    max = scores[j];
                }
            }
            Console.WriteLine("其中最大数是:{0}",max);
            Console.ReadKey();
        }
        
        static int ReadInt() //该方法要求用户输入一个大于或等于0的整数并返回。
        {
            int number = 0;
            do
            {
                try
                {
                    number = Convert.ToInt32(Console.ReadLine());
                    if (number >= 0)
                    {
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                catch
                {
                    Console.WriteLine("请输入整数!");
                    continue;
                }
            } while (true);
            return number;
        }
回复 使用道具 举报
  1. Console.WriteLine("请输入你要求的数字的个数:");
  2.             int num = int.Parse(Console.ReadLine());
  3.             int[] scores = new int[num];
  4.             int max = scores[0];
  5.             for (int i = 0; i < scores.Length; i++)
  6.             {
  7.                
  8.                 Console.WriteLine("请输入第{0}个人的成绩:",i+1);
  9.                 scores[i] = int.Parse(Console.ReadLine());//这里改了下,直接用这个数组接受变量在比较,不用再弄一个新的。
  10.                 for (int j = 0; j < scores.Length; j++)
  11.                 {
  12.                     if (scores[j] > max)//这两行也改动了,之前应该是没法编译通过的吧。
  13.                         max = scores[j];//
  14.                 }

  15.             }
  16.             Console.WriteLine("最大数是:{0}",max);
  17.             
  18.                 Console.ReadKey();
复制代码

搜狗截图_2013-06-24_20-05-49.png (7.49 KB, 下载次数: 0)

搜狗截图_2013-06-24_20-05-49.png
回复 使用道具 举报
黄文超 发表于 2013-6-23 23:09
LZ你确定你的代码能够编译?
注意10行和13、14行

虽然有一个地方错了,还是要感谢你
回复 使用道具 举报
万大述 发表于 2013-6-24 18:17
把里面那个for循环拿出来,并在循环的时候带上角标。参考代码如下:
static void Main(string[] args)
     ...

谢谢了,
回复 使用道具 举报
万大述 发表于 2013-6-24 18:17
把里面那个for循环拿出来,并在循环的时候带上角标。参考代码如下:
static void Main(string[] args)
     ...

Thank you。。。
回复 使用道具 举报
回复 使用道具 举报
nilaoyededie 发表于 2013-6-23 23:14
#region 从键盘接收用户输入的数字并求最大值
        Console.WriteLine("请输入你要求的数字的个数:");
...

谢谢了。非常感谢你的答案
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马