Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Why isn`t my code working? What am I missing when i run it in netbeans? import j

ID: 3851310 • Letter: W

Question

Why isn`t my code working? What am I missing when i run it in netbeans?

import java.util.Scanner;

public class PeopleWeights {

public static void main(String[] args) {

double weights[]=new double[5];

Scanner s=new Scanner(System.in);

double sum=0,maxWeight;

for(int i=0;i<5;i++){ System.out.print("Enter weight "+(i+1)+": ");

weights[i]=s.nextDouble();

}

maxWeight=weights[0];

for(int i=0;i<5;i++){ System.out.print("You entered: "+weights[i]+" ");

sum=sum+weights[i];

if(weights[i]>maxWeight){ maxWeight=weights[i];

}

}

System.out.print(" ");

System.out.println("Total weight: "+sum);

System.out.println("Average weight: "+sum/5);

System.out.println("Max weight: "+maxWeight);

}

}

return;

}

}

Explanation / Answer

This code will run perfectly.

In your code please remove "return" statement it doesn't help you and have extra two closing flower braces,please remove them.

Program

import java.util.Scanner;
public class peopleWeights {
public static void main(String[] args) {
double weights[]=new double[5];
Scanner s=new Scanner(System.in);
double sum=0,maxWeight;
for(int i=0;i<5;i++){
System.out.print("Enter weight "+(i+1)+": ");
weights[i]=s.nextDouble();
}
maxWeight=weights[0];
for(int i=0;i<5;i++){
System.out.print("You entered: "+weights[i]+" ");
sum=sum+weights[i];
if(weights[i]>maxWeight){
maxWeight=weights[i];
}
}
System.out.print(" ");
System.out.println("Total weight: "+sum);
System.out.println("Average weight: "+sum/5);
System.out.println("Max weight: "+maxWeight);
}
}


Output for your code

Enter weight 1: 5

Enter weight 2: 8

Enter weight 3: 6

Enter weight 4: 3

Enter weight 5: 10

You entered: 5.0 You entered: 8.0 You entered: 6.0 You entered: 3.0 You entered: 10.0

Total weight: 32.0

Average weight: 6.4

Max weight: 10.0   

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote