![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Need Feedback and Simple Advice
Look at the following code. I just wrote it, and it's extremely exciting, because I've never written something this large completely from scratch before
. It's a lot of fun, playing with the compiler, then trying to fix the errors by listening to the compiler . Anyway... this program is supposed to take 3 numbers from the user, then output the average of the three numbers to the screen. My assignment says "write a program that prompts the user to enter the first, the second and the third whole number (as integers) and outputs the average of three numbers (as a double)."Now, my program so far does just that, except I have no type-casts in here, and hence the double value at the end will always just show a zero after the period. Do you think the assignment is saying that the program should be able to give the average of three numbers INCLUDING one space after the period? And how would I do that? Thanks
import java.util.*;
public class NumberAverage
{
public static void main(String[] args)
{
int n1, n2, n3;
double numberAverage;
System.out.println("Hi!");
System.out.println("I will calculate the average of");
System.out.println("three whole numbers for you.");
System.out.println();
System.out.println("Please enter three whole numbers,");
System.out.println("separated by space and then hit");
System.out.println("the ENTER key:");
Scanner scannerObject = new Scanner(System.in);
n1 = scannerObject.nextInt();
n2 = scannerObject.nextInt();
n3 = scannerObject.nextInt();
System.out.println();
System.out.println("You entered " + n1 + ", " + n2 + " and " + n3);
numberAverage = (n1 + n2 + n3)/3;
System.out.println();
System.out.println("The average of the three numbers");
System.out.println("you have entered is: " + numberAverage);
}
} |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
i changed the line
numberAverage = (n1 + n2 + n3)/3; to numberAverage = ((double)(n1 + n2 + n3))/3; that way you are not doing integer division on the 3 numbers. then use math.round or something, and yes i would give it to 1 or 2 deciaml places.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Sweeeet!! Thank you! That's exactly what I wanted. I think I'll just leave it like that. But let's say I want to LIMIT the spaces after the period to 1, 2 or 3, or heck, anything, how would I do that?
I don't know anything about the math class yet. Thanks again! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() |
http://java.sun.com/j2se/1.5.0/docs/...lang/Math.html
import java.lang.Math; and to round the numbers Math.round(double); look on the link all the info is there.,
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
I'm not quite sure how I would use this in my code to achieve the result I'm looking for, but thanks a lot!
I'm satisfied with the way my code is right now, without the rounding. The end-number is 4.6666666667 or something like that. That's fine with me, but still it's good to know about the math class ![]() |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
import the library as i stated above in the code and add after the working out
numAverage = Math.round(numAverage); i think anyways i cant really test it out here.
__________________
"Put your hand on a hot stove for a minute, and it seems like an hour. Sit with a pretty girl for an hour, and it seems like a minute. THAT'S relativity." - Albert Einstein |
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Jun 2005
Location: MA, US
Posts: 204
Rep Power: 4
![]() |
Math.round() will round to the nearest integer... so if you have x=1.333333 and you do:
double x = 1.333333; x=Math.round(x); x will be 1. Using this, we can round to any decimal place by simply multiplying by factors of 10 before rounding. For example, to round to 2 decimal places we would multiply by 10^2 or 100 and then use the round method, then divide by 100 after the rounding. double x = 1.333333; x*=100; x=(double)Math.round(x)/100; here, x will be 1.33. it might be useful for you to write a general method for this. public double myRound(double num, int dec) {
return (double)Math.round(num*Math.pow(10,dec))/100;
}of course, there are ways to do this within the API, but sometimes its just as easy to write your own. Hope this helps!
__________________
"A stupid man's report of what a clever man says can never be accurate, because he unconciously translates what he hears into something he can understand." - B. Russell http://web.bryant.edu/~srk2 |
|
|
|
|
|
#8 |
|
Programmer
|
Yep. However, you also can use this:
import java.text.*; DecimalFormat formatter = new DecimalFormat("0.00");//how many decimals
System.out.println(formatter.format(number));
__________________
countdown++; |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Jun 2005
Posts: 34
Rep Power: 0
![]() |
Thanks a lot guys. Appreciate it
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|