Java 为什么赢了';t代码转到下一行,提示用户退出或继续
我有一个程序,可以计算用户选择形状的面积。我提供了4种形状,在它们消失后,我想提示它们继续或退出。但是,在程序请求shape和all之后,它会停止,并且在第一个switch语句之后不会进入任何代码 我尝试将代码作为switch语句的一部分,但由于错误,它无法运行。我还试着做了另一个开关,你可以看到,但什么也没有Java 为什么赢了';t代码转到下一行,提示用户退出或继续,java,Java,我有一个程序,可以计算用户选择形状的面积。我提供了4种形状,在它们消失后,我想提示它们继续或退出。但是,在程序请求shape和all之后,它会停止,并且在第一个switch语句之后不会进入任何代码 我尝试将代码作为switch语句的一部分,但由于错误,它无法运行。我还试着做了另一个开关,你可以看到,但什么也没有 import java.util.Scanner; import java.lang.Math; public class area { public static void main(
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语句,之后的一切都不会发生。首先,您需要将
开关盒
放入while循环
,这样,当选项
等于“c”时,程序将执行。
第二,上面的家伙在使用next()或nextFoo()后,放置了一个参考扫描仪的可能副本跳过nextLine()?
。因此,当您使用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();
}
}