编辑:我已经编写了平均值代码,但我不知道如何制作它,以便它也使用我的 args.length
中的 int
s
我需要编写一个 java 程序来计算:
- 平均值中读取的整数数量
- - 不一定是整数!
注意:我不想计算数组的平均值,而是计算 args 中的整数。
目前我已经写了:
int count = 0;
for (int i = 0; i<args.length -1; ++i)
count++;
System.out.println(count);
}
int nums[] = new int[] { 23, 1, 5, 78, 22, 4};
double result = 0; //average will have decimal point
for(int i=0; i < nums.length; i++){
result += nums[i];
}
System.out.println(result/count)
任何人都可以引导我走向正确的方向吗?或者举一个例子来指导我以正确的方式塑造这段代码?
提前致谢。
EDIT: I've written code for the average but I don't know how to make it so that it also uses int
s from my args.length
rather than the array.
I need to write a java program that can calculate:
- the number of integers read in
- the average value – which need not be an integer!
NOTE: I don't want to calculate the average from the array but the integers in the args
.
Currently I have written this:
int count = 0;
for (int i = 0; i<args.length -1; ++i)
count++;
System.out.println(count);
}
int nums[] = new int[] { 23, 1, 5, 78, 22, 4};
double result = 0; //average will have decimal point
for(int i=0; i < nums.length; i++){
result += nums[i];
}
System.out.println(result/count)
Can anyone guide me in the right direction? Or give an example that guides me in the right way to shape this code?
Thanks in advance.
发布评论
评论(11)
只需对代码进行一些小的修改即可(为了清楚起见,进行一些 var 重命名):
请注意,循环也可以简化:
编辑:OP 似乎想要使用 args 数组。这似乎是一个字符串数组,因此相应地更新了答案。
更新:
正如zoxqoj正确指出的那样,上面的代码没有考虑整数/双精度溢出。尽管我假设输入值足够小而不会出现该问题,但这里有一个用于非常大的输入值的代码片段:
这种方法有几个优点(尽管速度有点慢,所以不要将其用于时间关键的操作):
BigDecimal
可能比适合double
或long
的大小。Just some minor modification to your code will do (with some var renaming for clarity) :
Note that the loop can also be simplified:
Edit: the OP seems to want to use the
args
array. This seems to be a String array, thus updated the answer accordingly.Update:
As zoxqoj correctly pointed out, integer/double overflow is not taken care of in the code above. Although I assume the input values will be small enough to not have that problem, here's a snippet to use for really large input values:
This approach has several advantages (despite being somewhat slower, so don't use it for time critical operations):
BigDecimal
might be bigger than what fits into adouble
orlong
.这
基本上再次计算了
args.length
,只是不正确(循环条件应该是i)。为什么不直接使用 args.length (或 nums.length )呢?
否则你的代码看起来没问题。虽然看起来您想从命令行读取输入,但不知道如何将其转换为数字数组 - 这是您真正的问题吗?
This
basically computes
args.length
again, just incorrectly (loop condition should bei<args.length
). Why not just useargs.length
(ornums.length
) directly instead?Otherwise your code seems OK. Although it looks as though you wanted to read the input from the command line, but don't know how to convert that into an array of numbers - is this your real problem?
这看起来似乎很古老,但 Java 从那时起就已经发展起来了。介绍了 Streams & Java 8 中的 Lambdas。因此可能会帮助每个想要使用 Java 8 功能来做到这一点的人。
或整数。您可以使用 Arrays.stream(
mapToDouble(s -> Double.parseDouble(s))
它将字符串流转换为双精度流。Collectors.averagingDouble()
,它直接计算并返回平均值。一些 这里有示例。It seems old thread, but Java has evolved since then & introduced Streams & Lambdas in Java 8. So might help everyone who want to do it using Java 8 features.
or int. You can do this using
Arrays.stream(<arr>)
. Once you have stream of String array elements, you can usemapToDouble(s -> Double.parseDouble(s))
which will convert stream of Strings into stream of doubles.Stream.collect(supplier, accumulator, combiner)
to calculate average if you want to control incremental calculation yourselves. Here is some good example.Collectors.averagingDouble()
which directly calculates and returns average. some examples here.你不能这样做,因为 result/count 不是 String 类型,而 System.out.println() 仅接受 String 参数。也许尝试:
you can't do this because result/count is not a String type, and System.out.println() only takes a String parameter. perhaps try:
对于 1. 读入的整数数量,您可以使用数组的 length 属性,例如 :
,它给出数组中的元素数量。
2.计算平均值:
你正在以正确的方式做事。
for 1. the number of integers read in, you can just use length property of array like :
which gives you no of elements in an array.
And 2. to calculate average value :
you are doing in correct way.
而不是:
你可以只是
平均值是你的参数的总和除以你的参数的数量。
你也可以缩短这段代码,我会让你尝试询问你是否需要帮助!
这是我的第一个答案,如果有问题请告诉我!
Instead of:
you can just
The average is the sum of your args divided by the number of your args.
you can make this code shorter too, i'll let you try and ask if you need help!
This is my first answerso tell me if something wrong!
如果您尝试从命令行参数获取整数,您将需要这样的东西:
至于实际的平均代码,其他人建议了如何调整它(所以我不会重复他们所说的内容) )。
编辑:实际上,最好将其放在上面的循环中,根本不使用 nums 数组
If you're trying to get the integers from the command line args, you'll need something like this:
As for the actual averaging code, others have suggested how you can tweak that (so I won't repeat what they've said).
Edit: actually it's probably better to just put it inside the above loop and not use the
nums
array at all我将向您展示两种方法。如果您的项目中不需要大量统计数据,只需实施以下操作即可。
如果您计划进行大量统计,最好不要重新发明轮子。那么为什么不看看 http://commons.apache.org/proper/ commons-math/userguide/stat.html
你会爱上真正的爱!
I'm going to show you 2 ways. If you don't need a lot of stats in your project simply implement following.
If you plan on doing a lot of stats might as well not reinvent the wheel. So why not check out http://commons.apache.org/proper/commons-math/userguide/stat.html
You'll fall into true luv!
// 问题:让,使用循环从键盘中取出 10 个整数,并将它们的平均值打印在屏幕上。
// 这里他们要求用户使用循环输入 10 个整数并对这些数字进行平均。所以在我看来,java 的正确答案如下:-
// question: let, Take 10 integers from keyboard using loop and print their average value on the screen.
// here they ask user to input 10 integars using loop and average those numbers.so the correct answer in my perspective with java is below:-