Results 1 to 4 of 4

Thread: Math average.

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

    Default Math average.

    [SOLVED]


    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;


    TextIO.readFile("result.txt");
    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);
    System.out.println();

    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 !
    -William
    Last edited by Willywonka; 04-16-2011 at 08:27 PM.

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

    Default

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

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

    Default

    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
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

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

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
  •