Click here to Skip to main content
15,936,633 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
((reads an unspecified number of scores and determines how many scores are above or equal to the average and how many scores are below the average. Enter a negative number to signify the end of the input. Assume that the maximum number of scores is 100.))




The problem is that it is at the output is Error

What I have tried:

Java
package javaapplication28;
import java.util.*;
public class JavaApplication28 {
    
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter scores: (negative number signifies end): ");
		double[] scores = new double[100];  
        double num;
        double average=0; 						   	
        double Scores=0; 			   				   
			
		for (int i = 0; i < 100; i++) {
			num = input.nextDouble();			
			if (num > 0) {
                scores[i] = num;				
                average += num;					
                Scores++;
            } else {
                break;
            } 
                     
            average /= Scores;
            int aboveOrEqual=0;						
            int below=0;								
		
            for (int j= 0; j< Scores; j++) {
                if (scores[i] >= average)
				    aboveOrEqual++;				
                else
				    below++;							
            }

            System.out.println("\nAverage of scores: " + average);
            System.out.println("Number of scores above or equal to average: " + aboveOrEqual);
            System.out.println("Number of scores below average: " + below);
        }
    }
}
Posted
Updated 19-Oct-22 12:39pm
v2
Comments
Dave Kreskowiak 19-Oct-22 19:09pm    
I fixed up the formatting of the code you posted. I think the problems are pretty obvious now.

We have no idea what values your user eventer, so there is nothing we can practically do to directly help you.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
Member 15766438 19-Oct-22 21:22pm    
Thank you, you have successfully solved it
OriginalGriff 20-Oct-22 0:21am    
You're welcome!
Your comment: "The problem is that it is at the output is Error" is rather vague. What is the error?

You should show the full error message or an example of what is happening so that we can better help you.

Your code as shown is very poorly indented and aligned. If you were to properly indent your code I think the problem would reveal itself very clearly.

Pay close attention to the for-loop where the numbers are input. I think you're missing a brace to end the loop.

Other things I noticed:
What happens when you enter a score of 0? Is that the correct behavior?
Look at the for-loop where you check for above/below average. Are you using the correct index for scores?
 
Share this answer
 
v2
Comments
Member 15766438 19-Oct-22 21:23pm    
Thank you I solved it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900