In: Computer Science
Why won't the program run the code after the first switch statement. It shows Press c to continue or q to quit, but does not give the user the option to fill it in. Please help to run the code fully. This program is to find the area of shapes the user decides and must give them an option to quit or restart the program. Please provide the explanation and code in JAVA here. Thank you.
import java.util.Scanner;
import java.lang.Math;
public class area
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String square;
String circle;
String rectangle;
String triangle;
System.out.println("Please choose one of the shapes you want the
area of.");
System.out.println("Square, Circle, Rectangle, Triangle.");
String shape = scan.nextLine();
switch (shape)
{
case "Square":
System.out.println("Please enter the length of one side of the
square.");
double squareLength = scan.nextDouble();
double squareArea = (squareLength * squareLength);
System.out.println("The area of your square is "+ squareArea +
".");
break;
case "Circle":
System.out.println("Please enter the radius of the circle.");
double circleRadius = scan.nextDouble();
double circleArea = (circleRadius * circleRadius)*Math.PI;
System.out.println("The area of your circle is " + circleArea +
".");
break;
case "Rectangle":
System.out.println("Please enter the width of the
rectangle.");
double rectangleWidth = scan.nextDouble();
System.out.println("Please enter the length of the
rectangle.");
scan.nextDouble();
double rectangleLength = scan.nextDouble();
double rectangleArea = (rectangleWidth * rectangleLength);
System.out.println("The area of your rectangle is "+ rectangleArea
+ ".");
break;
case "Triangle":
System.out.println("Please enter the height of the
trangle.");
double triangleHeight = scan.nextDouble();
System.out.println("Please enter the base of the trangle.");
double triangleBase = scan.nextDouble();
double triangleArea = (triangleHeight * triangleBase) / 2;
System.out.println("The area of your triangle is " + triangleArea +
".");
break;
default:
System.out.println("Please choose an option.");
scan.nextDouble();
}
System.out.println("Press c to continue and or q to quit.");
String option = scan.nextLine();
switch (option)
{
case "c":
break;
case "q":
System.out.println("Have a good day!");
break;
}
}
}
import java.util.Scanner;
import java.lang.Math;
class Area
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
String square;
String circle;
String rectangle;
String triangle;
char option;
do
{
System.out.println("Please choose one of the shapes
you want the area of.");
System.out.println("Square, Circle, Rectangle,
Triangle.");
String shape = scan.nextLine();
switch (shape)
{
case "Square":
System.out.println("Please enter the length of one side of the
square.");
double
squareLength = scan.nextDouble();
double
squareArea = (squareLength * squareLength);
System.out.println("The area of your square is "+ squareArea +
".");
break;
case "Circle":
System.out.println("Please enter the radius of the circle.");
double
circleRadius = scan.nextDouble();
double
circleArea = (circleRadius * circleRadius)*Math.PI;
System.out.println("The area of your circle is " + circleArea +
".");
break;
case "Rectangle":
System.out.println("Please enter the width of the
rectangle.");
double
rectangleWidth = scan.nextDouble();
System.out.println("Please enter the length of the
rectangle.");
scan.nextDouble();
double
rectangleLength = scan.nextDouble();
double
rectangleArea = (rectangleWidth * rectangleLength);
System.out.println("The area of your rectangle is "+ rectangleArea
+ ".");
break;
case "Triangle":
System.out.println("Please enter the height of the
trangle.");
double
triangleHeight = scan.nextDouble();
System.out.println("Please enter the base of the trangle.");
double
triangleBase = scan.nextDouble();
double
triangleArea = (triangleHeight * triangleBase) / 2;
System.out.println("The area of your triangle is " + triangleArea +
".");
break;
default:
System.out.println("Please choose
an option.");
break;
}
System.out.println("Press c to continue and or q to
quit.");
option = scan.next().charAt(0);
}while(option=='c');
}
}
// I fixed all the error and correct the code please check
// If any doubt please comment