Need Help with this question 1 Prompt the user to enter five
Need Help with this question
(1) 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. (2 pts)
Ex:
(2) Also output the total weight, by summing the array\'s elements. (1 pt)
(3) Also output the average of the array\'s elements. (1 pt)
(4) Also output the max array element. (2 pts)
Ex:
Code I have So far...
import java.util.Scanner;
public class PeopleWeights
{
//Main method
public static void main(String[] args)
{
//Array to hold weights
double[] weights = new double[5];
int i;
double total=0, average, max = 0;
Scanner sc = new Scanner(System.in);
//Reading 5 weights from user
for(i=0; i<5; i++)
{
System.out.print(\"Enter weight \" + (i+1) + \": \");
//Storing weights in array
weights[i] = sc.nextDouble();
}
//Printing, calculating total and average of weights
System.out.print(\"\ \ You entered: \");
for(i=0; i<5; i++)
{
//Printing each weight
System.out.print(\" \" + weights[i]);
//Calculating total weight
total += weights[i];
//Finding maximum weight
if(weights[i] > max)
max = weights[i];
}
//Printing total weight
System.out.println(\"\ \ Total Weight: \" + total);
//Printing average weight
System.out.printf(\"\ Average Weight: %.2f \ \", (total)/5.0);
//Printing max weight
System.out.println(\"\ Max Weight: \" + max + \" \ \ \");
return;
}
}
Here is my input compared to correct input...Please let me know where changes need to be made. Thank you
1. Compare output
0/1
236 89.5 142 166.3 93
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total W
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0
2. Compare output
0/1
123.4 56 98 174 215.8
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 123.4 56.0 98.0 174.0 215.8 Total W
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 123.4 56.0 98.0 174.0 215.8
3. Compare output
0/1
236 89.5 142 166.3 93
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total Weight: 726.8 Averag
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8
4. Compare output
0/1
236 89.5 142 166.3 93
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total Weight: 726.8 Average Weight: 145.36 Max Weight: 236.0
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999
5. Compare output
0/1
236 89.5 142 166.3 93
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total Weight: 726.8 Average Weight: 145.36 Max Weight: 236.0
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total weight: 726.8 Average weight: 145.35999999999999 Max weight: 236.0
6. Compare output
0/1
123.4 56 98 174 215.8
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 123.4 56.0 98.0 174.0 215.8 Total Weight: 667.2 Average Weight: 133.44 Max Weight: 215.8
Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 123.4 56.0 98.0 174.0 215.8 Total weight: 667.2 Average weight: 133.44 Max weight: 215.8
| Input | 236 89.5 142 166.3 93 |
| Your output starts with | Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 Total W |
| Expected output starts with | Enter weight 1: Enter weight 2: Enter weight 3: Enter weight 4: Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93.0 |
Solution
import java.util.Scanner;
public class PeopleWeight
{
public static void main(String[] args)
{
double[] weights = new double[5];
int i;
double total=0, average=0.0f, max = 0.0f;
Scanner sc = new Scanner(System.in);
for(i=0; i<5; i++)
{
System.out.print(\"Enter weight \" + (i+1) + \": \");
weights[i] = sc.nextDouble();
}
System.out.print(\"\ \ You entered: \");
for(i=0; i<weights.length; i++)
{
System.out.print(\" \" + weights[i]);
total += weights[i];
}
for(i=0; i<weights.length; i++)
{
if(weights[i]>max)
max=weights[i];
}
System.out.println(\"\ \ Total Weight: \" + total);
System.out.printf(\"\ Average Weight: %.2f \ \", (total)/5.0);
int maxwt=(int)max;
System.out.println(\"\ Max Weight: \" + maxwt + \" \ \ \");
}
}
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
comment:
u actally need maxwt to be int integer right;
i just coverted double to int thats it



