Use the for loop to write a program to prompt the user to enter twelve n amount of numbers and then display the minimum, maximum, sum and the average of these numbers.
I don't can't puzzle how I would use a for loop in order to this, I've been having a bit of trouble.
import java.util.Scanner;
public class Loop{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter number 1: ");
double a = scanner.nextDouble();
System.out.println("Enter number 2: ");
double b = scanner.nextDouble();
System.out.println("Enter number 3: ");
double c = scanner.nextDouble();
System.out.println("Enter number 4: ");
double d = scanner.nextDouble();
System.out.println("Enter number 5: ");
double e = scanner.nextDouble();
System.out.println("Enter number 6: ");
double f = scanner.nextDouble();
System.out.println("Enter number 7: ");
double g = scanner.nextDouble();
System.out.println("Enter number 8: ");
double h = scanner.nextDouble();
System.out.println("Enter number 9: ");
double i = scanner.nextDouble();
System.out.println("Enter number 10: ");
double j = scanner.nextDouble();
System.out.println("Enter number 11: ");
double k = scanner.nextDouble();
System.out.println("Enter number 12: ");
double l = scanner.nextDouble();
double minNum = Math.min(a, Math.min(b, Math.min(c, Math.min(d, Math.min(e, Math.min(f, Math.min(g, Math.min(h, Math.min(i , Math.min(j, Math.min(k, l)))))))))));
double maxNum = Math.max(a, Math.max(b, Math.max(c, Math.max(d, Math.max(e, Math.max(f, Math.max(g, Math.max(h, Math.max(i , Math.max(j, Math.max(k, l)))))))))));
double sumNum = (a + b + c + d + e + f + g + h + i + j + k + l);
for(int i = 0; )
}
}
I expected the output to be used with the for loop, but then again I do not understand how I would use it in this method.
Here is a nicer way to do this, which doesn't even require storing all the numbers at once. The approach here is to keep a running tab on the largest and smallest input, as each new number is input. Also, we maintain a running total of all numbers entered, which later easily can be used to compute the average.
Scanner scanner = new Scanner(System.in);
double max, min, avg;
double total = 0.0d;
boolean first = true;
int numbers = 12;
for (int i=0; i < numbers; ++i) {
System.out.println("Enter number:");
double d = scanner.nextDouble();
if (first) {
max = d;
min = d;
total = d;
first = false;
}
else {
if (d > max) {
max = d;
}
else if (d < min) {
min = d;
}
total += d;
}
}
avg = total / numbers;
System.out.println("max: " + max + ", min: " + min + ", avg: " + avg);
Note that a more advanced approach might be to do something like store all 12 numbers into a list, and then leverage the Java 8 stream API to find the max, min, and average. But I suspect that is not the answer you want for your question.
Place this java code in the main function
`Scanner s=new Scanner(System.in);
int t=s.nextInt(); //12
int sum=0;
int min=Integer.MIN_VALUE;
int max=Integer.MAX_VALUE;
for(int i=0;i<t;i++){
int temp=s.nextInt();
if(temp<min)
min=temp;
if(temp>max)
max=temp;
sum+=temp;
}
System.out.println(min +" "+max+" "+sum);`
You can do this.
Scanner scanner = new Scanner(System.in);
double max = Double.MAX_VALUE, min = Double.MIN_VALUE, total = 0.00d;
int n = 12; // Change this to the number of values needed
for (int i=0; i < n; i++) {
System.out.println("Enter number:");
double d = scanner.nextDouble();
if (d > max) {
max = d;
}
else if (d < min) {
min = d;
}
total += d;
}
Double avg = total / n;
System.out.println("max: " + max + ", min: " + min + ", avg: " + avg);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.