Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Please, find my mistake

  1. #1
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default Please, find my mistake

    Hey guys, this is the assignment that I should do today:

    Write a program to read a list of exam scores given as integer percentages in the range 0 to 100. Display the total number of grades and the number of grades in each letter-grade category as follows: 90-100 is A, 80-89 is B, 70-79 is C, 60-69 is D, and 0-59 is F. Prin the percentage of the total for each letter grade, the lowest, the highest, and average grade. Use negative score as a snetinel value to indicate the end of the input.

    For Example, input: 98,87,86,85,85,78,73,72,72,70,66,63,50,-1

    Output should be:

    Total number pf grades entered was 13

    A's =1
    B's=4
    C's=5
    D's=2
    F's=1

    The % of A's = 7.69&
    The % of B's = 30.77%
    The % of C's = 38.46%
    The % of D's = 15.38%
    The % of F's = 7.69&

    avg = 75.77
    smallest = 50
    largest = 98


  2. #2
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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 );







    }
    }

  3. #3
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

  5. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 19th, 2012)

  6. #5
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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!

  7. #6
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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();

  9. The Following User Says Thank You to javapenguin For This Useful Post:

    Daler (June 19th, 2012)

  10. #8
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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

  11. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.
    Last edited by javapenguin; June 19th, 2012 at 08:29 PM.

  12. #10
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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?

  13. #11
    Member Daler's Avatar
    Join Date
    Jun 2012
    Posts
    34
    Thanks
    15
    Thanked 0 Times in 0 Posts

    Default 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"?

Similar Threads

  1. where located my mistake
    By erdy_rezki in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 26th, 2012, 11:31 AM
  2. Replies: 12
    Last Post: May 8th, 2012, 06:22 PM
  3. I am making a silly mistake but I cant find where! PLEASE HELP!
    By akmi5 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 14th, 2012, 02:44 AM
  4. how come this program only loops twice? wheres my mistake?
    By Rainy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 3rd, 2011, 10:49 AM
  5. New to Java I need help with a simple mistake
    By Reynalto in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:12 PM