d2jsp
Log InRegister
d2jsp Forums > Off-Topic > General Chat > Homework Help > Intro To Computer Science Help Needed
Add Reply New Topic New Poll
Member
Posts: 671
Joined: May 12 2010
Gold: 682.00
Sep 21 2014 12:52pm



So i have Everything else on the program correct, but for some reason the Average keeps coming out to 97.0 instead of 97.5

Anyone know why?


EDIT


"final int SCORE1 = 100; // first test score
final int SCORE2 = 95; // second test score"

This post was edited by thugsrus0790 on Sep 21 2014 12:53pm
Member
Posts: 7,721
Joined: Oct 11 2008
Gold: 304.00
Sep 21 2014 01:01pm
try switching the order of addition
look up formatting commands

This post was edited by saber_x3 on Sep 21 2014 01:02pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Sep 21 2014 01:10pm
Dont use int, use double.
Member
Posts: 23,838
Joined: Feb 18 2009
Gold: 0.01
Sep 21 2014 08:43pm
need to typecast the variables
Member
Posts: 6,457
Joined: Mar 12 2007
Gold: 6,745.00
Sep 22 2014 08:42pm
Whenever you divide two ints it does integer division, truncating off any decimals that would other wise be there. You either need to use doubles or type cast the operation by putting (double) in front of the operation.
Member
Posts: 29,614
Joined: Sep 23 2008
Gold: 0.00
Sep 22 2014 09:04pm
Quote (thugsrus0790 @ Sep 21 2014 01:52pm)
http://i.gyazo.com/3769f5e0161c6f044725ca32176f3cf7.png


So i have Everything else on the program correct, but for some reason the Average keeps coming out to 97.0 instead of 97.5

Anyone know why?


EDIT


"final int SCORE1 = 100;  // first test score
  final int SCORE2 = 95;  // second test score"


Since SCORE 1 and SCORE 2 are ints, your average calculation is treated as an integer operation.. You need to cast your inputs to DOUBLE before the operation..

Code

average = ((double) SCORE1 + (double) SCORE2)/NUMBER;
Member
Posts: 29,548
Joined: Mar 27 2008
Gold: 504.69
Warn: 10%
Sep 23 2014 06:33am
Quote (thenoose @ Sep 22 2014 10:42pm)
Whenever you divide two ints it does integer division, truncating off any decimals that would other wise be there. You either need to use doubles or type cast the operation by putting (double) in front of the operation.


This. :) Actually learning the same things this semester.
Go Back To Homework Help Topic List
Add Reply New Topic New Poll