NEED JAVA CODE HELP! Any help you guys can give would be amazing. Prompt the use
ID: 3579991 • Letter: N
Question
NEED JAVA CODE HELP! Any help you guys can give would be amazing.
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's my code btw:
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.println("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 FROM MY CODE IS ATTACHED:
Total: 4/6
1. Compare output
1/1
Input
236
89.5
142
166.3
93
Your output correctly 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
2. Compare output
1/1
Input
123.4
56
98
174
215.8
Your output correctly starts with
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
1/1
Input
236
89.5
142
166.3
93
Your output correctly 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 weight: 726.8
4. Compare output
0/1
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 weight: 726.8
Average weight: 145.36
Max weight: 236
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
Total weight: 726.8
Average weight: 145.35999999999999
5. Compare output
0/1
Input
236
89.5
142
166.3
93
Your output
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
Expected output
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
1/1
Input
123.4
56
98
174
215.8
Your output
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 correctly 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
Explanation / Answer
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.println();
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.