Hi, i need help with this code just a bit. I am wondering what am i doing wrong with my "average" I don't expect you guys to just give me my answer but if you can point me in the right direction i would love you all!! underneath the code is what i am SUPPOSED to have and only the average section is wrong.
public class GradesV2
{
public static void main(String[ ] args)
{
//Local variables
int totalPoints = 0;
int numTests = 0;
int testGrade = 0;
double average = 0.0;
testGrade = 97;
totalPoints += testGrade;
numTests ++;
average = totalPoints / testGrade;
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 79;
totalPoints += testGrade;
numTests++;
average = totalPoints / testGrade;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 83;
totalPoints += testGrade;
numTests++;
average = totalPoints / testGrade;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 88;
totalPoints += testGrade;
numTests++;
average = totalPoints / testGrade;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
}
}
This is what my result should be but it's not.
n = 1 New test grade: 97 total points: 97 average 97.0
n = 2 New test grade: 79 total points: 176 average 88.0
n = 3 New test grade: 83 total points: 259 average 86.3333333
n = 4 New test grade: 88 total points: 347 average 86.75
public class GradesV2
{
public static void main(String[ ] args)
{
//Local variables
int totalPoints = 0;
int numTests = 0;
int testGrade = 0;
double average = 0.0;
testGrade = 97;
totalPoints += testGrade;
numTests ++;
average = (double)totalPoints/numTests;
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 79;
totalPoints += testGrade;
numTests++;
average = (double)totalPoints/numTests;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 83;
totalPoints += testGrade;
numTests++;
average = (double)totalPoints/numTests;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
testGrade = 88;
totalPoints += testGrade;
numTests++;
average = (double)totalPoints/numTests;
System.out.println();
System.out.print("n = " + numTests + " New test grade: " + testGrade + " total points: " + totalPoints + " average: " + average);
}
}
Your errors:
1.) average should always be the total points divided by the number of tests. That's just how you get averages.
2.) you need to use a cast (for converting int to double) on one of the int variables so that you produce real doubles (instead of just .0 on the end).