Java中求平均数,数组实现,谢谢!

latitude4 2011-05-30 05:12:48
从控制台输入一组数字存入数组中,用空格间开 ,求输入的数的平均数 谢谢!……
...全文
3210 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
hbq85292429 2011-05-30
  • 打赏
  • 举报
回复
class Average {
public static void main(String[] args) {
int i,j,sum = 0;
double e = 0;
for(i=0; i<args.length; i++){
j = Integer.parseInt(args[i]);
sum += j;
}
e = sum/i;
System.out.println("平均数为:"+e);
}
}
riyuezhizhi 2011-05-30
  • 打赏
  • 举报
回复
呵呵,做出来啦
ZZZ5512536 2011-05-30
  • 打赏
  • 举报
回复

double sum=0;
System.out.println("输入数字,以空格间开");
String strInput = new Scanner(System.in).nextLine();
String[] strs = strInput.split(" ");
for(String str:strs){
sum += Double.parseDouble(str);
}
System.out.println("总和:"+sum);
System.out.println("平均数"+sum/strs.length);
老流氓123 2011-05-30
  • 打赏
  • 举报
回复
util包下边的scanner类下的方法:
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();

也可以用tokenizer
zlcing 2011-05-30
  • 打赏
  • 举报
回复
嗯。谢谢!
wofeizhenlong 2011-05-30
  • 打赏
  • 举报
回复
找到好地方了,新来的希望大家以后多多指教啊
wang_huanming 2011-05-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tkd03072010 的回复:]
楼主参考一下:

Java code

package CSDN;

import java.util.Scanner;

public class ArrAverage {
public static void main(String[] args) {
System.out.println("请输入数字:");
Scanner sc……
[/Quote]
+1
wsl1176 2011-05-30
  • 打赏
  • 举报
回复
有收获哈!
TKD03072010 2011-05-30
  • 打赏
  • 举报
回复
楼主参考一下:

package CSDN;

import java.util.Scanner;

public class ArrAverage {
public static void main(String[] args) {
System.out.println("请输入数字:");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
double sum = 0.0d;
String[] strArr = str.split(" ");
for (String s : strArr)
sum += Double.parseDouble(s);
System.out.println(str + " 平均数:" + sum/strArr.length);
}
}

结果:
请输入数字:
23 24 23.5 77 89
23 24 23.5 77 89 平均数:47.3
chenchenyangll 2011-05-30
  • 打赏
  • 举报
回复
这种直接要代码的 估计是个坑
wsl1176 2011-05-30
  • 打赏
  • 举报
回复
Java初学者送上一个小程序,功能跟LZ有点出入,我是输入一个数回车再输入另一个数。不好之处,尽情点评!
import java.util.Scanner;
public class Average {

public static void main(String[] args) {
System.out.println("你想求几个数的平均数?");
int s;
Scanner sc = new Scanner(System.in);
s = sc.nextInt();
System.out.println("请输入你要求"+s+"个数的平均数:");
double[] my_array = new double[s];
double num=0;
for(int i=0;i<s;i++)
{
double a;
Scanner aa = new Scanner(System.in);
a = aa.nextFloat();
my_array[i]=a;
num+=my_array[i];
}
System.out.println("你所求的"+s+"个数的平均数为"+num/2);
}
}
latitude4 2011-05-30
  • 打赏
  • 举报
回复
如 输入
24 23 34.5 77 89
输入上面五个数的平均值……

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧