Results 1 to 4 of 4

Thread: Math average.

  1. #1
    Willywonka is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Math average.


    Im trying to calculate the average, but it discards the remainder.
    Have a look.

    //Reads a file, and gets the students name + 3 exam grades.
    //Then calculates the average grade.

    String name;
    int grade1;
    int grade2;
    int grade3;
    double average;

    name = TextIO.getln();

    grade1 = TextIO.getlnInt();

    grade2 = TextIO.getlnInt();

    grade3 = TextIO.getlnInt();

    average = (grade1+grade2+grade3)/3;

    System.out.printf("The average grade for %s was %1.2f", name, average);

    I've managed to work around it by just adding them up and dividing like this:

    average = (grade1+grade2+grade3);
    average = average / 3;
    With this i can display the correct value.

    I used jEdit, for my coding. And this is a question or "Test yourself" thingy from a book, and the solution uses the same code... so what did i do wrong ?
    HELP !
    Last edited by Willywonka; 04-16-2011 at 07:27 PM.

  2. #2
    Willywonka is offline Member
    Join Date
    Apr 2011
    Rep Power


    I figured it out :/ haha, sorry.
    I forgot to put 3.0.

    Durp !
    Nice forum by the way, maybe you guys will let me hang here in the newbie forums for a while ?

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Camp Lejuene, North Carolina
    Rep Power


    I don't think we could stop you from hanging here if you weren't. Though this community is free, and everyone is welcome.

    As long as you're not looking for a handout, questions are accepted here. From the looks of it, you won't have a problem asking questions here.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power

Similar Threads

  1. average
    By swrta in forum New To Java
    Replies: 3
    Last Post: 03-16-2011, 09:52 AM
  2. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 06:23 AM
  3. average
    By anjigadu in forum New To Java
    Replies: 4
    Last Post: 09-19-2010, 09:52 PM
  4. Need help getting average
    By soccer_kid_6 in forum New To Java
    Replies: 15
    Last Post: 09-12-2010, 11:59 PM
  5. Calculate Average
    By sthack99 in forum New To Java
    Replies: 4
    Last Post: 06-13-2008, 11:09 AM

Tags for this Thread

Posting Permissions

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