Re: Please, find my mistake

Here is my code:

import java.util.Scanner;

public class Grades

{

public static void main(String[] args)

{

int numberOFGrades = 0;

double smallest=0, largest=0, average, sum=0;

double grade=1;

int A=0, B=0,C=0,D=0,F=0;

double percentA, percentB, percentC, percentD, percentF;

Scanner scan = new Scanner(System.in);

while (grade<=100 && grade > 0)

{

System.out.println("Please, write your grades ");

grade = scan.nextInt();

if(grade>=90 && grade < 100)

{

A++; // the total of A's

}

else if(grade>=80 && grade < 90)

{

B++; // the total of B's

}

else if(grade>=70 && grade < 80)

{

C++; // the total of C's

}

else if(grade>=60 && grade < 70)

{

D++; // the total of D's

}

else if(grade>=0 && grade <= 59)

{

F++; // the total of F's

}

if (grade >0)

{

numberOFGrades++; // If the grade is negative, It will show the total of grades that was entered

}

}

//the quantity of each grade

System.out.println();

System.out.println("Total of grades entered was " + numberOFGrades);

System.out.println();

System.out.println("Total of A's = " +A);

System.out.println("Total of B's = " +B);

System.out.println("Total of C's = " +C);

System.out.println("Total of D's = " +D);

System.out.println("Total of F's = " +F);

percentA = (A/numberOFGrades)*100;

System.out.println("The percent of A's = " + percentA + "%");

percentB = (B/numberOFGrades)*100;

System.out.println("The percent of B's = " + percentB + "%");

percentC = (C/numberOFGrades)*100;

System.out.println("The percent of C's = " + percentC + "%");

percentD = (D/numberOFGrades)*100;

System.out.println("The percent of D's = " +percentD + "%");

percentF = (F/numberOFGrades)*100;

System.out.println("The percent of F's = " + percentF + "%");

if(grade < 50 && grade>0)

smallest = grade;

if(grade > 90 && grade <=100)

largest = grade;

sum = A + B + C + D + F;

average = (sum/numberOFGrades);

System.out.println("the average is " + average );

System.out.println("the smallest number is " + smallest );

System.out.println("the largest number is " + largest );

}

}

Re: Please, find my mistake

when I input different grades, the percentage is always 0, but when I input the same grade, this grade just shows 100%

Also, I don't know how to find average, and this stuff

Re: Please, find my mistake

while (grade<=100 && grade > 0)

This won't work if grade is 0 exactly.

while (grade<=100 && grade >= 0)

is better.

This probably isn't going to be code that you can use directly, but it might help

(Also, are you allowing extra credit? i.e. what if someone got a 105?)

Assuming no extra credit,

if (grade <=100 && grade >=90)

letter = 'A';

else if (grade >=80 && grade <=89)

letter = 'B';

else if (grade >=70 && grade <=79)

letter = 'C';

else if (grade >=60 && grade <=69)

letter = 'D';

else if (grade >=0 && grade <=59)

letter = 'F';

else

letter = '?';

percentA = (A/numberOFGrades)*100;

That is the result of an annoying thing in java,

for instance

double value3 = (11/5) * 100;

will return 200, not 250.

One way to make it be a double

double value3 = (double)(11)/(double)(5) * 100;

value3 is now 220.00000000000003;

I think you can use the DecimalFormat class to fix it to be two digits after the decimal.

Re: Please, find my mistake

Thanks a lot for your reply!

I will try it now, and if it works I will let you know!

Re: Please, find my mistake

Oh God! It works... so we have to convert integers to doubles first! Wow! Thank you so much...

Do you know how to find smallest, average, and largest grade ? Please, let me know... I am just a beginner, I am really sorry about that

Re: Please, find my mistake

Is grade meant to be entered as a double, i.e. a decimal, or as an integer?

double grade=1;

grade = scan.nextInt();

if it's a double, then a better way of putting that would be

grade = scan.nextDouble();

Re: Please, find my mistake

Write a program to read a list of exam scores given **as integer** percentages in the range 0 to 100

Re: Please, find my mistake

It's a bit confusing as to what is what.

grade should probably be an int. You're using it as an int everywhere else.

Also, what if the input were all 100's, then you wouldn't have any smallest as it is only checking for between 1 and 59 (unless you fixed it to check for 0 too.)

so if all numbers weren't equal but were all passing, then you'd have no smallest.

Assuming there is one grade only, then that one grade would be both smallest and largest, right? (And average too I think.)

With only one grade read in, that sole grade would automatically be the largest and the smallest, right?

Any subsequent, if there are any, grades would have to be either smaller, larger, or the same as the first one.

Grade should be of type int then.

Since grade is of type int, smallest and largest should be too. Average might not be (it could be a decimal in theory perhaps.)

Smallest should be set to something large. If you initialize it to 0, then unless somebody gets a -1, which you said they can't, that will be the smallest, even if the actual smallest grade happens to be a 100.

Try making smallest 101 to start out with.

0.) If it happens that your first number entered is less than 0 or greater than 100, than you'll have to change some things in your output as your only grade value will be invalid. Smallest will be 101, (101 was chosen because you could just put an if statement like

if (smallest == 101)

to check to see if your first entered grade happened to be an invalid one. If it was 100, then if you actually had all 100's then it would act like your first value was invalid even though it wasn't. ) If the first value is not invalid, then

1.) Read in first value. Make that smallest and largest.

2.) For each new value, if larger than largest, make that the new largest, if smaller than smallest, make that the new smallest.

As for average, if you mean the average of all the scores, then simply add then all together every time you read in one and then at the end divide by the total number of (valid) grades.

Also, your

if (grade > 0) block isn't necessary.

If your grade is less than 0, it will fail the while loop and not even go to that part. Simply increase the gradeCount every time a new grade is read in.

Re: Please, find my mistake

I followed all your steps, and everything works except "the smallest".. it's still becomes the negative number... what the problem?

Re: Please, find my mistake

Thanks a lot for your help! I have already written the program and it actually worked.. I used the DecimalFormat class as you said before!

Thanks a lot! I really appreciate it...

So how to make it as "solved"?