我有一个程序可以计算用户选择的形状的面积。我提供了 4 种形状,在它们离开后,我想提示他们继续或退出。但是,在程序要求形状和所有内容之后,它会停止并且在第一个 switch 语句之后不会进入任何代码。
我尝试使代码成为 switch 语句的一部分,但由于错误,它无法运行。我也试图做另一个开关,如你所见,但什么都没有。
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.");
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.");
}
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;
}
}
}
程序只运行到第一个 switch 语句,之后的所有内容都没有发生。
首先,您需要将switch case
放入while loop
中,这样,当option
等于"c"时,程序将执行。 其次,上面的人放了一个参考Possible duplicate of Scanner is skipping nextLine() after using next() or nextFoo()?
。因此,当您使用nextDouble
读取值,然后尝试使用nextLine
读取时,scan 将跳过下一个nextLine
。在此之后,您需要放置另一个nextLine
供程序识别它。
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String option = "c";
while(option.equals("c")){
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.");
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;
}
scan.nextLine();
System.out.println("Press c to continue and or q to quit.");
option = scan.nextLine();
}
}