java 如何使用do-while循环获取五个数字的总和、平均值、最小值和最大值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48501795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get the sum, average, minimum and maximum of five numbers-java using do-while loop
提问by Kleine Hansel Buenafe
I'm trying to get the sum, average, minimum and maximum of five numbers but somehow I get this output. I'm trying to re-code it all over again but it is still the same. Can you help me check this guys... Here's my code:
我试图得到五个数字的总和、平均值、最小值和最大值,但不知何故我得到了这个输出。我试图重新编码它,但它仍然是一样的。你能帮我检查一下这些家伙吗……这是我的代码:
import java.util.*;
public class Kleine {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double average;
int count = 0, sum = 0, num, min = 0, max = 0;
System.out.println("Please enter the number of numbers you wish to evaluate:");
do {
num = scan.nextInt();
sum += num;
count++;
} while (count < 5);
average = sum / 5;
{
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
System.out.println("Your average is: " + average);
System.out.println("The sum is: " + sum);
System.out.println("Your maximum number is: " + max);
System.out.println("Your minimum number is: " + min);
}
}
Here's the output:
这是输出:
Please enter the number of numbers you wish to evaluate:
1
10
5
-3
6
Your average is3.0
The sum is:19
Your maximum number is 6
Your minimum number is 0
BUILD SUCCESSFUL (total time: 19 seconds)
The minimum and maximum numbers goes somewhere... a little advice please...
最小和最大数字在某处......请给一些建议......
回答by Tim Biegeleisen
The best way to handle the min/max values is to keep track of them as your read in each value:
处理最小值/最大值的最佳方法是在读取每个值时跟踪它们:
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int i=0; i < 5; ++i) {
num = scan.nextInt();
if (num > max) max = num;
if (num < min) min = num;
sum += num;
}
double average = sum / 5.0d;
I seed the max and min values with the smallest/largest integer values, respectively. This lets us capture the actual min and max values as they are read in. Also, I think that a basic for
loop works better here than a do
while loop.
我分别用最小/最大整数值作为最大值和最小值的种子。这让我们可以在读入时捕获实际的最小值和最大值。此外,我认为基本for
循环在这里比do
while 循环更有效。
Note that I compute the average using a double type, because it may not be a pure integer value (even in your sample data the average is not an integer).
请注意,我使用 double 类型计算平均值,因为它可能不是纯整数值(即使在您的示例数据中,平均值也不是整数)。
回答by Haroon
Use
利用
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
And your
和你的
{
if(num>max)
max=num;
if(num<min)
min=num;
}
needs to be inside the do-while loop, or else it runs only for the last value of number entered.
需要在 do-while 循环内,否则它只对输入的数字的最后一个值运行。
回答by Farhan Qasim
Min and Max were now equal to the max and min of integer. now if any number is less than min and greater than max, min and max takes their position. The average and the sum functionality was great. The problem in your code was that it was getting the max and min after the loop for input has executed. The flow was wrong.
Min 和 Max 现在等于整数的最大值和最小值。现在,如果任何数字小于 min 且大于 max,则 min 和 max 占据它们的位置。平均值和总和功能很棒。您的代码中的问题是它在输入循环执行后获得了最大值和最小值。流量错了。
import java.util.*;
public class Kleine {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
double average;
int count=0, sum=0, num=0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
System.out.println("Please enter the number of numbers you wish to evaluate:");
do{
if(num>max) max=num;
if(num<min) min=num;
num=scan.nextInt();
sum+=num;
count++;
}while(count<5);
average = sum/5;
System.out.println("Your average is"+average);
System.out.println("The sum is:"+sum);
System.out.printf("Your maximum number is %d\n",max);
System.out.printf("Your minimum number is %d\n",min);
}
}
回答by DJDaveMark
For a start you can use Math.min
& Math.max
. The average is sum / count.
首先,您可以使用Math.min
& Math.max
。平均值是总和/计数。
An example getting a min number without a loop would be:
在没有循环的情况下获取最小数字的示例是:
long min = Integer.MAX_VALUE;
min = Math.min(min, 9);
min = Math.min(min, 4);
min = Math.min(min, 6);
// min = 4
Do something similar for max.
为最大做类似的事情。
You'd also be better off starting with a list or array of numbers. Get the output right, then add more complicated things like user input.
您最好从数字列表或数组开始。获得正确的输出,然后添加更复杂的内容,例如用户输入。
回答by F. Vorontsov
You can do it this way without defining number of integers to read:
您可以这样做而无需定义要读取的整数数量:
import java.util.ArrayList;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>();
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("Next number?");
numbers.add(in.nextInt());
IntSummaryStatistics summaryStatistics = numbers.stream()
.mapToInt(Integer::valueOf)
.summaryStatistics();
System.out.println(String.format("Max: %d, Min: %d, Average: %s, Sum: %d", summaryStatistics.getMax(), summaryStatistics.getMin(), summaryStatistics.getAverage(), summaryStatistics.getSum()));
}
}
}
回答by Rahul
If I just change the existing code, the logic should be like below:
如果我只是更改现有代码,则逻辑应如下所示:
do{
num=scan.nextInt();
sum+=num;
if(count==0){
min=num;
max=num;}
if(num>max)
max=num;
if(num<min)
min=num;
count++;
}while(count<5);
average = sum/5;
The issue was that your min-max condition was outside the loop and you were initializing min and max by 0. You should set min/max to your first input number.
问题是您的 min-max 条件在循环之外,并且您将 min 和 max 初始化为 0。您应该将 min/max 设置为您的第一个输入数字。