Prompt the user to enter five numbers being five peoples wei

Prompt the user to enter five numbers, being five people\'s weights. Store the numbers in an array of doubles. Output the array\'s numbers on one line, each number followed by one space.

*NOTE* please do not use \"printf\".

Ex:

(2) Also output the total weight, by summing the array\'s elements.

(3) Also output the average of the array\'s elements.

(4) Also output the max array element.

Ex:

Solution

PeopleWeights.java


import java.text.DecimalFormat;
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int i = 0;
int n = 5;
double arr[]=new double[n];

for(i = 0; i < n; i++){
System.out.print(\"Enter weight \" + (i+1)+\": \");
arr[i]=scnr.nextDouble();
}
System.out.print(\"You entered: \");
for(i = 0; i < n; i++){
System.out.print(arr[i] + \" \");
}
System.out.println();
double total = 0;
double max = 0;
for(i=0; i<n; i++){
   if(max < arr[i]){
       max = arr[i];
   }
   total = total + arr[i];
}
DecimalFormat df = new DecimalFormat(\"#.##\");
double average = total/n;
System.out.println(\"Total weight: \"+df.format(total));
System.out.println(\"Average weight: \"+df.format(average));
System.out.println(\"Max weight: \"+df.format(max));
  
return;
}
}

Output:

Enter weight 1: 236
Enter weight 2: 89.5
Enter weight 3: 142
Enter weight 4: 166.3
Enter weight 5: 93
You entered: 236.0 89.5 142.0 166.3 93.0
Total weight: 726.8
Average weight: 145.36
Max weight: 236

Prompt the user to enter five numbers, being five people\'s weights. Store the numbers in an array of doubles. Output the array\'s numbers on one line, each num
Prompt the user to enter five numbers, being five people\'s weights. Store the numbers in an array of doubles. Output the array\'s numbers on one line, each num

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site