Method in a Switch Statement
So I need to find a way to have a method inside a switch statement. I have no idea how though. Any help would be appreciated. In each of the following cases are the different methods I need to have executed if the case is called.
So, a menu comes up asking what they want to do. They enter 1,2,3,4, or 0 and it goes to the method in the case where it asks them what the radius or length of the side is, and it tells them the area or volume.
Code:
import java.util.Scanner;
public class Menu
{
public static void main( String args[] )
{
Scanner input = new Scanner( System.in );
int num;
double radius;
double volume;
double length;
double area;
System.out.println("1. Circle Area");
System.out.println("2. Sphere Volume");
System.out.println("3. Square Area");
System.out.println("4. Cube Volume");
System.out.println();
System.out.println("Enter number (or 0 to exit)");
num = input.nextInt();
while (num != 0)
{
if(num <0 || num >4)
{
System.out.println("Wrong input");
}
if(num >=1 && num <=4)
{
switch(num)
{
case 1:
{
public void determineCircleArea()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of circle: " );
double radius = input.nextDouble();
System.out.printf( "Area is %f\n", circleArea( radius ) );
}
public double circleArea( double radius )
{
double area = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 2 );
return area;
}
}
break;
case 2:
{
public void determineSphereVolume()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter radius of sphere: " );
double radius = input.nextDouble();
System.out.printf( "Volume is %f\n", sphereVolume( radius ) );
}
public double sphereVolume( double radius )
{
double volume = ( 4.0 / 3.0 ) * Math.PI * Math.pow( radius, 3 );
return volume;
}
}
}
break;
case 3:
{
public void determineSquareArea()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter length of square: " );
double length = input.nextDouble();
System.out.printf( "Volume is %f\n", squareArea( length ) );
}
public double squareArea( double length )
{
double area = ( 4.0 / 3.0 ) * Math.pow( length, 2 );
return area;
}
}
}
break;
case 4:
{
public void determineCubeVolume()
{
Scanner input = new Scanner( System.in );
System.out.print( "Enter length of sphere: " );
double length = input.nextDouble();
System.out.printf( "Volume is %f\n", cubeVolume( length ) );
}
public double cubeVolume( double length )
{
double volume = ( 4.0 / 3.0 ) * Math.pow( length, 3 );
return volume;
}
}
}
break;
}
}
}
}
}