-3

I have recently written the following code:

 import java.util.Scanner;
 import java.lang.Math;

  public class Circle{
    public static void main(String[] args){

      int d;
      int a;
      int v;

      Scanner keyboard = new Scanner(System.in);
      System.out.print("Please enter the value for the radius:");
      double radius = keyboard.nextDouble();

   d = 2*radius;
   a = 4*Math.PI*Math.pow(radius, 2)) ;
   v = 4/3*Math.PI*Math.pow(radius, 3) ;

   System.out.println("The diameter of the sphere is: "+ d);
   System.out.println("TThe surface area of the sphere is: "+ a);
   System.out.println( "The volume of the sphere is: "+ v);

}

} However, it doesn't work and keeps saying this:

  Circle.java:15: error: incompatible types: possible lossy conversion from double to int
d = 2 * radius;
      ^
  Circle.java:16: error: incompatible types: possible lossy conversion from double to int
a = 4 * Math.PI * (radius*radius) ;
                ^
  Circle.java:17: error: incompatible types: possible lossy conversion from double to int
v = 4/3 * Math.PI * (radius*radius*radius) ;

Can someone help please and explain the error that I have made? Any help is appreciated. Thank you!!!!!

Evelyn Zhang
  • 1
  • 1
  • 1

1 Answers1

1

The problem is that you are trying to assign a double value to an int variable (in lines 15,16,17)

you can solve it by changing the variables from int to double:

//...
double d;
double a;
double v;
//...

or by casting the double values to int (this will round the result)

//...
d = (int)(2*radius);
a = (int)(4*Math.PI*Math.pow(radius, 2));
v = (int)(4/3*Math.PI*Math.pow(radius, 3));
//...
Ofek
  • 1,065
  • 6
  • 19