-
Help converting doubles to integers...
I'm currently in a Java programming class and stuck on an assginment.
The assignment asks me to do the following:
Write a program that:
a. Prompts the user to input five decimal numbers.
b. Prints the five decimal numbers.
c. Converts each decimal number to the nearest integer.
d. Adds the five integers.
e. Prints the sum and average of the integers.
I did part a and b with no problem but theres nothing in the chapter we're covering that talks about converting decimal numbers to the nearest integer (i.e. if it's 3.3 round it down to 3 and if it's 3.6 round it up to 4). If I can figure out part c then I can finish the rest of it.
There is a small section on type conversion (casting) in the chapter but it only talks about how it works, no examples of code or how to use casting.
Any help is appreciated
-
can you just do this...?
sum = (int)num1 + (int)num2 ... and so on...
and then
average = sum / 5
with average being a double
-
I can do the average and sum parts of the program just fine.
The problem is I can't figure out how to take the decimals that the user inputs and round them either up or down based on the number that was entered.
Example: If the user entered 65.58 the program needs to turn that into an integer and round it up to 66 since it's 65.58. If they had entered 65.3 then I want it to round it down to 65. Make sense?
So far the best I can figure out how to do it to simply convert them to integers which alway just drops the decimal place. (i.e. 65.3 = 65 and 65.8 = 65)
Prior to this class I've taken C++, JScript, and VB so I know that I can likely accomplish this using "if" statements but the professor hinted that it can be done without them.
Any ideas?
Thanks
-
Well I think in the Math class there are methods for rounding numbers, but i'm assuming you're not meant to use external methods since it's an assignment. You may want to look up the Math class anyway for future use.
Anyway, to round it without using any if statements you should
a) Multiply number by 2
b) Convert to an int
c) Divide number by 2.
The result of this will be an int of the correct value after rounding. Eg
5.3 * 2 = 10.6
10.6 toInt = 10
10 / 2 = 5
5.6 * 2 = 11.2
11.2 toInt = 11
11 / 2 = 6 (integer division)
-
I didn't know that was possible, thanks a ton
Edit: Actually this doesn't work. Heres why:
5.6 * 2 = 11.2
11.2 toInt = 11
11 / 2 = 6 (integer division)
when you divide the 11 by 2 the actual answer is 5.5 and since you're dividing two integers the decimal gets dropped and you end up with 5, not 6.
However what does work is adding .5 to the user inputted number and then casting it as an integer.
Example
------------
5.1 + .5 = 5.6 which rounds down to 5 which is correct according to what the user entered.
5.6 + .5 = 6.1 which turns into 6 and correctly rounds up.
Thanks though
-
Hey Gabriel,
How did the assignment go?
Another odd-ball question since you are familiar with C++ and VB, how do you enjoy the class so far?
And no, I'm not the Prof
-
Once I figured out that adding .5 to the user inputted number the rest of the assignment was easy and I managed to submit it on time so I got full credit.
I have taken VB, JScript and C++. I've done far more C++ than anything else.
The class isn't too hard in terms of the actual coding it's just that I'm horrible at doing math
Java reminds me more of C with the "System.out.println"
statement. It's not too hard to adapt to a new language if you've learned prior ones, the syntax is similar for all of them. It's just a matter of learning the new way to make the same things happen in the new language.
The professor for my Java class is the same one I had for my C++ classes. He used to be a programmer with a 9-5 job so he knows his stuff. It's a good class.
-
Sorry, I missed an add 1 in there hehe. You're right, what I wrote doesn't work. If you add 1 before dividing by 2 it works though.
5.3 * 2 = 10.6
10.6 toInt = 10
10 + 1 = 11
11 / 2 = 5
5.6 * 2 = 11.2
11.2 toInt = 11
11 + 1 = 12
12 / 2 = 6
-
Originally posted by Gabriel
Once I figured out that adding .5 to the user inputted number the rest of the assignment was easy and I managed to submit it on time so I got full credit.
That's good new to hear.
Originally posted by Gabriel
I have taken VB, JScript and C++. I've done far more C++ than anything else.
Ah, interesting. Are you a Computer Science major?
Originally posted by Gabriel
The class isn't too hard in terms of the actual coding it's just that I'm horrible at doing math
Bah, who isn't from time to time? 
Originally posted by Gabriel
Java reminds me more of C with the "System.out.println"
statement. It's not too hard to adapt to a new language if you've learned prior ones, the syntax is similar for all of them. It's just a matter of learning the new way to make the same things happen in the new language.
*nods*
In your Java class, what book are you working with?
-
5.3 * 2 = 10.6
10.6 toInt = 10
10 + 1 = 11
11 / 2 = 5
bally long way of saying
Code:
int rounded = (int)(myDouble+0.5)
though, eh mike?
-
Originally posted by cjard
though, eh mike?
What does that mean?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks