在 Java 中使用 Return 和 Switch 来计算形状的面积

问题描述 投票:0回答:3

尝试编写一个程序,提示用户选择圆形、矩形或三角形,然后询问相应的尺寸,然后将面积显示给用户。

我使用了一些返回和开关的实例,我认为这就是我搞砸的地方。

package shapearea;

import java.util.Scanner; //global scanner


public class ShapeArea {     
    public static void main(String[] args) {
       int menuOption;
       double area;

   menuOption = showMenu();
   area = processShapeOption(menuOption); 
   displayArea(area, menuOption);


}//end main

public static void displayArea(double area, int menuOption) {
   switch (menuOption) {
       case 1: System.out.printf("The area of the circle is: %.3f\n ", area);
          break; 
       case 2: System.out.printf("The area of the rectangle is: ", area);
           break;
       case 3: System.out.printf("The area of the triangle is: ", area);
           break;
   }//end switch



   }//end displayArea
   public static int showMenu() {
       Scanner scan = new Scanner(System.in);
       int menuOption;

       System.out.println("1. circle");
       System.out.println("2. rectangle");
       System.out.println("3. triangle");
       System.out.print("enter choice:");
       menuOption = scan.nextInt();

       return menuOption;
   }//end showMenu

   public static double processShapeOption(int menuOption) {
       double area = 0;
       switch(menuOption){
           case 1: double radius = getDimension("radius");
                   area = calculateCircleArea(radius);

              break;

           case 2: double length = getDimension("length"),
                      width = getDimension("width");
                   area = areaOfRectangle(length, width);
           break;

           case 3: double base = getDimension("base"),
                      height = getDimension("height");
                   area = areaOfTriangle(base, height);

           break;


   }//end switch

   return area;     
}//end processShapeOption

public static double getDimension(String dimensionType) {
   double dimension;
   Scanner scan = new Scanner(System.in);
   System.out.print("Enter the " + dimensionType + ":");
   dimension = scan.nextDouble();

   return dimension;

   }//end getDimension

   public static double calculateCircleArea(double radius) {
   double area;

   area = Math.PI * Math.pow(radius, 2);

   return area;
}//end calculateCircleArea

public static double areaOfRectangle(double width, double length) {
   double area;

   area = width * length;

   return area;
}//end areaOfRectangle

public static double areaOfTriangle(double height, double base) {
   double area;

   area = .5 * base * height;

   return area;
}

}//end class

计算圆形面积的部分工作正常,但是当我选择矩形时,除了矩形尺寸之外,它还继续询问三角形尺寸,并且无法显示面积。

编辑:添加了休息时间

java return switch-statement
3个回答
3
投票

您需要在每个案例后添加

break
声明。

case 1: {
    doStuff();
    break;
}

你还忘了添加

"%.3f\n"

查看案例 2 和 3 的 printf 语句


2
投票

您必须在每个

break;
的末尾添加
case
语句。请参阅教程这里


0
投票
public static double getUserInput( ) {
        double inp;
        do {
            System.out.println("Enter input");
             inp=scan.nextInt();
        }while(inp<=0);
        return inp;
    }

将其用于所有肯定编号的用户输入。这是完整的代码

导入java.util.Scanner;

public class AreaCalculator {
  static  Scanner scan;
    
    public static void main(String[] args) {
        int choice;
        int counter=0;
        do{
        System.out.println("1) Triangle\n" +
"2) Square\n" +
"3) Rectangle\n" +
"4) Circle\n" +
"5) Quit\n" );
        
       // System.out.println("Enter your choice from the menu:");
 scan=new Scanner(System.in);
        do{
            System.out.println("Enter your choice from the menu:");
         choice=   scan.nextInt(); 
          //  System.out.println("Enter valid choice");
        } while(choice<1||choice>5);
        switch(choice)
        {
            case 1:System.out.println("u chose Triangle");
                System.out.println("Enter base");
         // double   base=scan.nextInt();
                double base=getUserInput();
                          System.out.println("Enter height");
        //  double  height=scan.nextInt();
                          double  height=getUserInput();
         // boolean valid=true;
      // boolean valid=    errorTrap(base, height);
       
   double area=        triangleArea(base,height);
   System.out.print("Area is "+area);
       
            continue;
            case 2:System.out.println("u chose Square.Enter Side of square");
        double side=getUserInput();
        double SquareArea=squareArea(side);
                System.out.println("Area of square is"+SquareArea);
     
            continue;
            case 3:System.out.println("u chose Rectange.Enter Length of rect");
            double length=getUserInput();
            System.out.println("Enter width of rect");
            double width=getUserInput();
         double rectArea=   rectangleArea(length,width);
         System.out.println("Area of rectangle is"+rectArea);

            continue;
            case 4:System.out.println("u chose Circle.Enter circle radius");
            double radius=getUserInput();
            double circleArea= circleArea(radius);
                System.out.println("Area of Circle is "+circleArea);
            continue;
            case 5:System.out.print("u chose exit"); scan.close();System.exit(0);
            default:System.out.print("Invalid");
            
        }
        counter++;
        }while(counter<1);
        
        scan.close();
        
    }//main
    public static double triangleArea(double base, double height){
        double area=0.5 * base *height;
        return area;
    }
    
     public static double squareArea(double side){
        double area=side*side;
        return area;
    }
     
     public static double rectangleArea(double length, double breadth){
        double area=length*breadth;
        return area;
    }
     
     public static double circleArea(double radius){
        double area=Math.PI*radius;
        return area;
    }


    public static double getUserInput( ) {
        double inp;
        do {
            System.out.println("Enter input");
             inp=scan.nextInt();
        }while(inp<=0);
        return inp;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.