i want to add condition if >100 is error but when im try to add the condition the program is running but not what i want... can somebody tell me what should i do..tq

import java.util.*;

public class test_scores 
{
public static void main(String args[])
    {
    Scanner input = new Scanner(System.in);
    int num_of_scores,sum,count,temp;
    int[]score=new int[100];

    sum=0;
    count=0;
    temp=100;

    System.out.print("Number of test scores = ");
    num_of_scores=input.nextInt();

    do
        {
        System.out.print("Enter the scores ");
        score[count]=input.nextInt();

        if(score[count]<temp);
            {
            temp=score[count];
            }
        sum+=score[count];
        count++;
        if(score[count]>temp);
        {
            System.out.println("error");
            ++count;
        }
        {

        }
        }

    while(count<num_of_scores);
    sum-=temp;
    sum=sum/(num_of_scores-1);

    System.out.println("The average is="+sum);

    }

}

Recommended Answers

All 4 Replies

Remove the semicolons after the if statements. If statements followed by semicolons will not result in compile errors, but will mess up the output of the program.

After resolving that first problem I realized that your code was still not working correctly. I made some adjustments to the code and made it much simpler.
You should be able to use this code to fit your needs. i.e you can add your if statements to check for errors in input. The following will allow you to correctly find the average.

import java.util.*;

public class test_score
{
	public static void main(String args[])
	{
		Scanner input = new Scanner(System.in);
		int num_of_scores,sum,count;
		int[]score=new int[100];
		sum=0;
		count=0;
		System.out.print("Number of test scores = ");
		num_of_scores=input.nextInt();

		for(count=0; count<num_of_scores; count++)
		{
			System.out.print("Enter the scores ");
			score[count]=input.nextInt();
			sum += score[count];
		}

		System.out.print("Avg = " + sum/num_of_scores);
	}
}

The algorithm you were using to find the average was incorrect. All you need to do is add all of the test scores together and divide that sum by the total number of scores.
Also you should use double values instead of ints because the average can often times result in a floating point number.

tq for the feedback.. but actually my output i want is.. for example i want to key in 3 number of score test... then the minimum score will not include to calculate the average...

Example output!
Number of test scores = 3
Enter the scores 50
Enter the scores 20
Enter the scores 50
Avg = 50

and the score condition is 0-100! if score<0 && >100 it is error!!

tq

the code for the error would be written as

for(count=0; count<num_of_scores; count++)
		{
			System.out.print("Enter the scores ");
			score[count]=input.nextInt();
                        if (score[count]<0 || score[count]>100) break;
			sum += score[count];
		}

in order to ignore the lowest score, i would use an ArrayList as such:

//ArrayList variable
public ArrayList<Integer> score = new ArrayList<Integer>();

//code for input
for(count=0; count<num_of_scores; count++)
		{
			System.out.print("Enter the scores ");
                        int temp = input.nextInt();
                        if (score.get(count)<0 || score.get(count)>100) break;
			else score.add(input.nextInt());
		}

//to test for lowest score
int low = 0; //location of lowest known score as of yet
for (int i =1; i<score.size(); i++) {
if (score.get(i)<score.get(low)) low = i;
}
score.remove(low);

//to print lowest score
for (int i = 0; i<score.size(); i++) {
sum+=score.get(i);
}
System.out.println(sum/score.size());

That should work.

TJ

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.