Results 1 to 4 of 4
Thread: Math average.
- 04-16-2011, 07:23 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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 !
-WilliamLast edited by Willywonka; 04-16-2011 at 07:27 PM.
- 04-16-2011, 07:26 PM #2
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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
- 04-16-2011, 07:37 PM #3
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.
-
You are more than welcome here, especially if you keep answering your own questions (and maybe a couple of questions from other newbies)!
Similar Threads
-
average
By swrta in forum New To JavaReplies: 3Last Post: 03-16-2011, 09:52 AM -
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12-07-2010, 06:23 AM -
average
By anjigadu in forum New To JavaReplies: 4Last Post: 09-19-2010, 09:52 PM -
Need help getting average
By soccer_kid_6 in forum New To JavaReplies: 15Last Post: 09-12-2010, 11:59 PM -
Calculate Average
By sthack99 in forum New To JavaReplies: 4Last Post: 06-13-2008, 11:09 AM
Bookmarks