Hey guys this is my first time using this site. Im having trouble creating a grading scale that averages out grades. Whenever I enter the grades in from the console I get some odd answers that make no sense. Can someone give me some pointers in the right direction?

``````    public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Final Exam average");
int Fexam = input.nextInt();
System.out.println("Enter Midterm average");
int Midterm = input.nextInt();
System.out.println("Enter Quiz average");
int Quiz = input.nextInt();
System.out.println("Enter Homework Average");
int HW = input.nextInt();
System.out.println("Class Average");
int CA = input.nextInt();

double Final = (Fexam / 100) * .80 ;
double M = (Midterm / 100) * .15 ;
double Q = (Quiz / 100) * .20 ;
double H = (HW / 100) * .20 ;
double C = (CA / 100) * .20;

double Finalgr =( (Final + M + Q + H + C)* 100);
System.out.println("your final average is: "+ Finalgr );
}

}``````
2
Contributors
2
Replies
37
Views
6 Months
Discussion Span
Last Post by Owais_2

Beware integer by integer division. The result is the FLOOR of that division. Thus 80 divided by 100 is 0, not 0.80. That's because an integer divided by an integer is an INTEGER, not a double. Thus this line (assuming Fexam equals 80)...

`` double Final = (Fexam / 100) * .80 ;``

is equivalent to this...

``````int temp1 = Fexam / 100; // temp1 equals 80 / 100 which equals 0.
double temp2 = temp1 * .80; // temp2 equals 0.0 since anything times 0 is 0.
double Final = temp2; // Final equals 0.``````

Compare that to THIS...

`` double Final = (Fexam / 100.0) * .80 ; // note 100.0, not 100``

which is this (again, assumes Fexam is 80)

`````` double temp1 = ((double)Fexam) / 100.0; // temp1 equals 0.80.
double temp2 = temp1 * .80; // temp2 equals 0.64
double Final = temp2; // Final equals 0.64.``````

Thus change `100` to `100.0` in your code and see if that works better.

Edited by AssertNull