Newbie, Computing Student Help
Hi, im a first year computing student after having basically done about 2 years of learning databases, web development and networking i have now started to learn Java. However its just plain difficult for me as i have trouble understanding what everything means and such.
Anyway heres my current assignment:
class Main
{
public static void main(String args[])
{
int exam;
int coursework;
double result;
exam = 71;
coursework = 40;
result = (exam + coursework) / 2;
System.out.print( "EX = ");
System.out.print( exam );
System.out.print( " CW = ");
System.out.print( coursework );
System.out.print( " Mark = ");
System.out.println( result );
}
}
Which prints : EX = 71 CW = 40 Mark = 55.0
However i want it to print: EX = 71 CW = 40 Mark = 55.5
I know its basic but i do need the help and anyone with links or tips on learning java/programming overall would be greatly appreciated :)
Re: Newbie, Computing Student Help
Hey BITmixit, welcome to the JavaProgrammingForums.
In the future could you please surround your code with tags such as the ones in my signature, as It makes it easier for everyone to read.
The problem that you're facing here is that the computation done on (71+40)/2 is done under the assumption of integers, and only then assigned to a double variable;
So, (71+40)/2 as an int = 55, then storing 55 in a float/double yields 55.0;
There are two things you can do, either declare all the other variables as double, or I recommend converting the calculations to a double.
eg: double answer = (double) //sum here
Re: Newbie, Computing Student Help
Awesome that worked, thanks. Will do with the tags.