Maya - 8 months ago 60

Java Question

I am trying to multiply 4 by the quotient of the number of darts hit inside of a circle and the number of darts thrown per trial. I tried to use the print statements below it to figure out where the problem is. It prints the correct numbers, just not the answer to the equation. Am I doing something wrong?

`//calculation for pi`

public static double getPi(int hits, int darts)

{

double answer = 4 * (hits / darts); //problem here

System.out.println(answer);

System.out.println(hits);

System.out.println(darts);

return answer;

}

Answer

If you divide an int by an int, the result will be an int. If you expect to get a floating number, then you will have to convert any operand to double.

For example:

```
int a = 4;
int b = 6;
double c = a / b; // result is 0.000000
double d = (double)a / b; // result is 0.666667
```