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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© xukunn 中级黑马   /  2014-10-31 16:08  /  1553 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 xukunn 于 2014-10-31 16:19 编辑

题目:键盘输入一串字符串,用‘,’隔开,都是数字。控制台输出大于等于平均值的个数。
样例输入:1,2,3,4,5
样例输出:3
要求:字符串长度不超过128,平均值四舍五入取整数。

提供Eclipse,代码直接粘贴到网页上,后台会有3组测试案例测试,总共可以提交5次。










7 个回复

倒序浏览
计数器么
回复 使用道具 举报

是滴呀,,
回复 使用道具 举报
数组存储数据,然后写个for遍历if判断为每次大于平均值count=count+1,嗯,应该就是这样
回复 使用道具 举报
冥夜 发表于 2014-10-31 17:07
数组存储数据,然后写个for遍历if判断为每次大于平均值count=count+1,嗯,应该就是这样 ...

嗯,就是还有一些细节的地方,比如字符串的处理,平均值四舍五入用到的函数
回复 使用道具 举报
  1. import java.io.*;
  2. class huaweitest
  3. {
  4.         public static void main(String[] args) throws Exception
  5.         {
  6.                 String str=null;
  7.                 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  8.                 str=br.readLine();
  9.                 method_1(str);
  10.         }

  11.         public static void method_1(String str)
  12.         {
  13.                 if(str.length()>128)
  14.                 {
  15.                         System.out.println("字符串长度大于128了,恕我无能为力..");
  16.                         return;
  17.                 }
  18.                 String[] strs = str.split(",");
  19.                 int len = strs.length;
  20.                 double[] ints=new double[len];
  21.                 double sum=0;
  22.                 for(int i=0;i<len;i++)
  23.                 {
  24.                         ints[i]=Integer.parseInt(strs[i]);
  25.                         sum=sum+ints[i];
  26.                 }
  27.                 int average=(int)Math.round(((double)sum)/len);
  28.                 int count=0;
  29.                 System.out.println("sum:"+sum+"....."+"average:"+average);
  30.                 for(int i=0;i<len;i++)
  31.                 {
  32.                         if(ints[i]>=average)
  33.                         {
  34.                                 count++;
  35.                         }
  36.                 }
  37.                 System.out.println("count:"+count);
  38.         }
  39. }
复制代码

评分

参与人数 1黑马币 +4 收起 理由
OCTSJimmy + 4 写的不错,假如再抽取封装加注释一下就更完.

查看全部评分

回复 使用道具 举报 1 0
noiary 高级黑马 2014-10-31 23:57:27
7#
很棒的基础题
回复 使用道具 举报
这题考:
1、输入流(键盘录入)
     -->这里还有输入后的判定,(录入数据不达标、过长),有多种处理方式:a、自定义异常 b、无限循环直至录入正确
2、字符串切割(最简单正则)
3、集合(切割后的字符串存储)
4、类型转换(字符串转浮点,为什么是浮点——有可能输入小数啊)
5、循环(合计求值)
6、还是循环(判定并计数)
回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马