0

I'm a beginner in Java programming. When I coded a simple console app it gave an output which was not what I wanted. My code is:

class myClass{
    public static void main(String[] args) {
        int ballsPlayed = 100;

        double o = (double)(( ballsPlayed / 6 )  + ( ( ballsPlayed % 6) / 10 ));
        System.out.println(o);
    }
}

( ( ballsPlayed % 6) / 10) should be 0.4 since remainder of 100/6 is 4 and 4/10 must give 0.4. Then the variable 'o' must give 16.4(16+0.4). But I am getting 16.0 as an output from console. What is the mistake I committed?

2
  • 1
    What do you mean by "it gave an output which was not wanted"? Was there an error, was the answer different than you were expecting? Commented May 1, 2015 at 14:08
  • A double isn't required to have a decimal point when instantiated :) You can declare double ballsPlayed = 100; Commented May 1, 2015 at 14:14

5 Answers 5

1

You must Typecast the ballsPlayed to double because ballsPlayed is an integer. It returns the integer part of (ballsPlayed % 6) / 10. Thus you got 0 except 0.4.

Try this,

double o = (double)(( ballsPlayed / 6 )  + ((double) ( ballsPlayed % 6) / 10 ));

Here you will get 16 + 0.4 = 16.4

Sign up to request clarification or add additional context in comments.

Comments

1

Just change the ballsPlayed variable from int to

double ballsPlayed = 100;

this will obviate the need for multiple casts. The output of (( ballsPlayed % 6) / 10 )) will be 0 not 0.4 because the compiler is dealing with an int value.

Comments

0

The output of (( ballsPlayed % 6) / 10 )) is 0 not 0.4 because all values in here is int. You need one double value for / operation to get double output. To get desired output change like

 ( (double)( ballsPlayed % 6) / 10 ))

or

(( ballsPlayed % 6) / 10.0 )) // Make 10 as double

Comments

0

Your problem starts here ballsPlayed / 6

An integer divided by an integer gives an integer - casting it to double is too late, you'll just get a double representation if the truncated result from the first integer division

If you want to get a fractional result from this, you need to cast ( or declare ) one of the numbers involved in the calculation in to a floating point data type.

This is pretty much what every one else has said ....

Comments

0

ballsPlayed is int so by default ballsPlayed/6 will give you int only so that is 16. Similar is the case with ballsPlayed % 6 which will give int and again division by 10 will give int which will result in 0.

Here you want the first division to be int and second division to be treated as double.. So just put the casting for second operation as

double o = (( ballsPlayed / 6 )  + ( ( (double)ballsPlayed % 6) / 10 ));

here the ballsPlayed % 6 will be 4 and (double)ballsPlayed % 6) will be 4.0, a double. You can also do the casting after division

double o = (( ballsPlayed / 6 )  + ( (double)( ballsPlayed % 6) / 10 ));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.