Beginner Java question

B.def

Member
Jun 13, 2010
68
0
0
I am trying to convert an int value to double.
This is what I'm working with. I am not aloud to declare the variables with a double, only int.

Code:
double a=0,b=1,c=2,d=3;
double average= (a+b+c+d)/4;
System.out.println("The average of " + a + " " + b + " " + c + " and "+d+" is "+average);
Result
Code:
run:
The average of 0.0 1.0 2.0 and 3.0 is 1
BUILD SUCCESSFUL (total time: 0 seconds)
I need the average to be more than integer.
 

JustAnAverageGuy

Diamond Member
Aug 1, 2003
9,057
0
76
double a=0,b=1,c=2,d=3;
double average= (a+b+c+d)/4.0;
System.out.println("The average of " + a + " " + b + " " + c + " and "+d+" is "+average);

See correction.

The reason you're getting the answer you are is due to the way certain values are stored.

Even though you've declared them as doubles, you assigned them integer values.

int / int= int (even if the result has decimals, they are truncated)
int / double = double (result is 'promoted')
 
Last edited:

B.def

Member
Jun 13, 2010
68
0
0
I'm sorry I typed it in wrong. the values defined up top are suppose to be int values as shown below
Code:
int a=0,b=1,c=2,d=3;
double average=(double)(a+b+c+d)/4;
System.out.println("The average of " + a + " " + b + " " + c + " and "+d+" is "+average);
I tested some other things out and realized I had to plug in double into the average equation to get the double values.

Thanks for the quick response though.
 

BrightCandle

Diamond Member
Mar 15, 2007
4,762
0
76
just bare in mind that the double cast has bound to the top result:
double average=((double)(a+b+c+d))/4

Pushing either top of bottom of the calc to be a double will make the entire calculation happen as a double.
 

B.def

Member
Jun 13, 2010
68
0
0
Yes, thanks. We just went over it today in class and it makes a lot more sense now.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |