Warm up: People\'s weights (Java) (1) Prompt the user to enter five numbers, bei
ID: 3789333 • Letter: W
Question
Warm up: People's weights (Java)
(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:
Here is my current Code
import java.util.Scanner;
public class PeopleWeights {
public static void main(String[] args) {
/* Type your code here. */
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.println("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.print("Total weight: " + total);
System.out.printf("Average weight: %.2f ", (total)/5.0);
int maxwt=(int)max;
System.out.println("Max weight: " + maxwt + " ");
return;
}
}
Screenshots of my input compared to Expected Input
Please help in letting me know where I am going wrong with this code...Highly frustrated with how close I am. Thank you
SUBMITTED: 01:44 PM ON 02/08/17 1. Compare output 236 89.5 Input 142 166.3 93 Enter weight 1 Enter weight 2 Enter weight 3: Your output starts with Enter weight 4 Enter weight 5: You entered: 236.0 89.5 142.0 166. 3 93 Total w Enter weight 1 Enter weight 2 Enter weight 3: Expected output starts with Enter weight 4 Enter weight 5: You entered: 236.0 89.5 142.0 166.3 93 .0Explanation / Answer
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
for above output code is:
import java.util.*;
public class allmain {
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.println("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();
System.out.print("Total weight: " + total);
System.out.println();
System.out.printf("Average weight: %.2f ", (total)/5.0);
int maxwt=(int)max;
System.out.println("Max weight: " + maxwt + " ");
return;
}
}
comment:
the code you had writen is absolutly corect..the thing is ..the print new line statement..
i placed these statement where you asked..thank u
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.