Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hi all,
Im taking course intro to java programming and Im having problem w/ my program. I suppose to get an average number from three input number but I dont get the right result. For instance, i put 5,7, and 11 in the textField and the result is 15 and I know it's not right, cuz it's supposed to be 7...and this is the compile codes I have:
// calculate the average of the three values
private void calculateJButtonActionPerformed( ActionEvent event )
{
// get values input by user
int number1 = Integer.parseInt(
firstNumberJTextField.getText() );
int number2 = Integer.parseInt(
secondNumberJTextField.getText() );
int number3 = Integer.parseInt(
thirdNumberJTextField.getText() );// sum number1, number2, and number3 and divide by 3
int result = number1 + number2 + number3 / 3;// display the result
averageJTextField.setText( String.valueOf( result ) );} // end method calculateJButtonActionPerformed
I might put this wrong, but if someone out there who can help i'd be very appreciated...
thanks,
j3r3my07

You need to look into something called "Operator Precedence". The rules of Operator Precedence control which order operators are evaluated when no paranthese are used.
They are basically the same in most programming languages as they are in basic math.
What is: 2 + 4 / 2 ?
3? or 4?
Is it:
(2 + 4) / 2 ?
or
2 + (4 / 2) ?
Basically, you need some parantheses in your calculation line.

OMG what was I thinking...yah i guess i've must forgot about the parantheses...im gonna give it a try then...i'll post back later...
thanks man,
j3r3my

![]() |
web server connection
|
I would like to learn C++...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |