Results 1 to 6 of 6
  1. #1
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Question Almost Done - Need Help

    I'm trying to teach myself how to program by looking for online project ideas. This one needs an average of 10 scores, also needs low score and high score.

    Why does it say the min and max are the same? Here's my code:

    import java.util.*;
    import java.text.*;
    public class ExamGrades
    {
    public static void main ( String [ ] args )
    {


    Scanner scan = new Scanner( System.in);
    DecimalFormat answer = new DecimalFormat( "#0.00");
    int grade;
    int min = 0;
    int max = 0;
    double total = 0;

    for( int i = 1; i <=10; i++)
    {
    System.out.print( "Please enter an exam grade between 0 and 100");

    grade = scan.nextInt();
    if ( grade >= 0 && grade <= 100)
    {
    max = grade;
    min = grade;


    total += grade;

    if( grade < min)
    {
    min = grade;

    }
    if ( grade > max)
    {
    max = grade;
    }



    }
    }
    double average = total / 10;
    System.out.println( "The minimun value of your exam grades is " + min);
    System.out.println( "The maximum value of your exam grades is " + max);
    System.out.println( "The average of your exam grades is " + answer.format(average));



    }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Urgle View Post
    Why does it say the min and max are the same?
    Because you tell them to be the same:
    Java Code:
          if (grade >= 0 && grade <= 100)
          {
            max = grade;
            min = grade;
    To prevent this from happening, work out your strategy and algorithm on paper first before committing it to code.

  3. #3
    Urgle is offline Member
    Join Date
    Oct 2008
    Posts
    30
    Rep Power
    0

    Default

    ok, I figured that out, but ive been wondering how to find how many scores are above/below average. Ive tried everthing I can think of but its not working.

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Have you had the chance to work with arrays yet? You should play around with those, and I'm sure you'll figure out how to work it into your code.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Urgle View Post
    ok, I figured that out, but ive been wondering how to find how many scores are above/below average. Ive tried everthing I can think of but its not working.
    "its not working" tells us little. You will be much better served to show us your attempt at this and tell us specifically how it's not working out.

    Also, when posting your code, please use code tags so that your code will retain its formatting and be readable. To do this, you will need to paste already formatted code into the forum, highlight this code, and then press the "code" button at the top of the forum Message editor prior to posting the message. You may want to click on the Preview Post button to make sure that your code is formatted correctly. Another way is to place the tag &#91;code] at the top of your block of code and the tag &#91;/code] at the bottom, like so:

    Java Code:
    &#91;code]
      // your code block goes here.
      // note the differences between the tag at the top vs the bottom.
    &#91;/code]
    Good luck.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First of all design a strong pattern to workout the problem. I'll give some tips, may be you can find something useful.

    First collect all values and store them in an array from the user. So you need to learn about arrays. How to define, how to add and so on.

    If you are stored them in an array, you can sort them. Once you sort all elements, first and last elements gives the required values. Easy to calculate total, average and so on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •