提示用户在形状之间进行选择。有关所选形状的正确信息。计算形状的面积到小数点后四位。询问用户是否要选择其他形状;继续,直到用户选择停止为止。请帮助我理解我的错误。
import java.util.Scanner;
public class Project1_CSC110 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String PlayAgain= "";
int circle = 1;
int square = 2;
int rectangle = 3;
int righttriangle = 4;
int amount;
do {
Scanner scan = new Scanner(System.in);
System.out.println("Circle \t\t 1\nSquare \t\t 2\n rectangle \t\3\rightAngle \t4\n");
System.out.print("Enter number to choose shape");
amount = scan.nextInt();
if (amount == circle) {
double radius;
double areacircle;
System.out.println("Shape chose = Circle");
System.out.print("Enter Radius");
radius = scan.nextDouble();
areacircle = Math.PI * (radius * radius);
System.out.println("Area=" + areacircle);
}
if (amount == square){
double sidelength;
double areasquare;
System.out.println("\nShape chosen = Square");
System.out.print("Enter width");
sidelength = scan.nextDouble();
areasquare = sidelength * sidelength;
System.out.println("Area =" + areasquare);
}
if (amount == rectangle) {
double width;
double length;
double arearectangle;
System.out.println("\nShape chosen = Rectangle");
System.out.print("Enter width:");
width = scan.nextDouble();
System.out.print("Enter length:");
length = scan.nextDouble();
arearectangle = length * width;
System.out.println("Area =" + arearectangle);
}
if (amount == righttriangle) {
double Aleg;
double Bleg;
double arearighttriangle;
System.out.println("\nShape chosen = Right Triangle");
System.out.print("Enter leg A of triangle");
Aleg = scan.nextDouble();
System.out.print("Enter leg B of triangle");
Bleg = scan.nextDouble();
arearighttriangle = .5 * Aleg * Bleg;
System.out.println("Area =" + arearighttriangle);
}
System.out.println("\nWould you like to chose another shape? Yes/No");
PlayAgain = scan.next();
While (PlayAgain.equalsIgnoreCase("Yes"));
if (PlayAgain.equalsIgnoreCase("No"));{
System.out.println("Bye!");
}}
private static void While(boolean equalsIgnoreCase) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
您在do ... while循环结束时弄糟了。应该是:
do
{
//....
System.out.println("\nWould you like to chose another shape? Yes/No");
PlayAgain = scan.next();
}while(PlayAgain.equalsIgnoreCase("Yes"));
System.out.println("Bye!");