简体   繁体   English

使用Java中的多种方法计算几何图形的面积

[英]Calculating Areas of Geometric Figures using multiple methods in Java

My program will calculate the area but I need to have each different shape in it's own method and I'm having difficulty understanding how to first separate them all into their own method and link them all together. 我的程序将计算面积,但是我需要在自己的方法中使用每种形状,而且我很难理解如何首先将它们全部分离为自己的方法并将它们链接在一起。 Any help is much appreciated. 任何帮助深表感谢。

import java.util.Scanner;
public class Project4 {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String s = "Please choose which area/volume to calculate: 1-Circle 2-Triangle 3-Cone 4-Cylinder 5-Sphere 6-Quit: ";
        System.out.print(s);
        System.out.println(" ");
        int num = keyboard.nextInt();
        if (num < 1 || num > 6){
            System.out.print("Please choose a number between 1 and 6: ");
            num = keyboard.nextInt();
        }
        if(num == 1){
            System.out.print("Please enter the radius: ");
            double r = keyboard.nextDouble();
            double a;
            a = 3.14 * r * r;
            System.out.print("The area of the circle is: " + a);
            System.out.println(" ");
            System.out.print(s);
            num = keyboard.nextInt();
        }
        if(num == 2){
            System.out.print("Please enter the base: ");
            double b = keyboard.nextDouble();
            double at;
            System.out.print("Please enter the height: ");
            double h = keyboard.nextDouble();
            at = .5 * b * h;
            System.out.print("The area of the triangle is: " + at);
            System.out.println(" ");
            System.out.print(s);
            num = keyboard.nextInt();
        }
        if(num == 3){
            System.out.print("Please enter the radius: ");
            double r = keyboard.nextDouble();
            System.out.print("Please enter the height: ");
            double h = keyboard.nextDouble();
            double v1;
            v1 = (1/3) * 3.14 * r * r * h;
            System.out.print("The volume of the cone is: " + v1);
            System.out.println(" ");
            System.out.print(s);
            num = keyboard.nextInt();
        }
        if(num == 4){
            System.out.print("Please enter the radius: ");
            double r = keyboard.nextDouble();
            System.out.print("Please enter the height: ");
            double h = keyboard.nextDouble();
            double vc;
            vc = 3.14 * r * r * h;
            System.out.print("The volume of the cylinder is: " + vc);
            System.out.println(" ");
            System.out.print(s);
            num = keyboard.nextInt();
        }
        if(num == 5){
            System.out.print("Please enter the radius: ");
            double r = keyboard.nextDouble();
            double vs;
            vs = (4/3) * 3.14 * r * r * r;
            System.out.print("The volume of the sphere is: " + vs);
            System.out.println(" ");
            System.out.print(s);
            num = keyboard.nextInt();
        }
        if(num == 6){
            System.out.print("Thank you for using this program");
        }

}
}

You can use switch..case 您可以使用switch..case

switch(num) {
  case 1: calculateCircleArea();
          break;
  case 2: calculateTriangleArea();
          break;
  case 3: calculateConeVolume();
          break;
  case 4: calculateCylinderVolume();
          break;
  case 5: calculateSphereVolume();
          break;
  case 6: System.out.print("Thank you for using this program");
          break;
  default: System.out.print("Please choose a number between 1 and 6: ");
           break;
}

You can create new private/public methods to do the calculations and seperate them. 您可以创建新的私有/公共方法来进行计算并将其分开。

Take this part 参加这部分

if(num == 1){
    System.out.print("Please enter the radius: ");
    double r = keyboard.nextDouble();
    double a;
    a = 3.14 * r * r;
    System.out.print("The area of the circle is: " + a);
    System.out.println(" ");
    System.out.print(s);
    num = keyboard.nextInt();
}

You can create a new method for the calculation like so 您可以像这样创建一个新的计算方法

public static double getArea(double radius)
{
return 3.14*radius*radius;
}

Then, inside your if statement you can say 然后,在您的if语句中,您可以说

if(num == 1){
        System.out.print("Please enter the radius: ");
        double r = keyboard.nextDouble();
        double a;
        a = getArea(r);
        System.out.print("The area of the circle is: " + a);
        System.out.println(" ");
        System.out.print(s);
        num = keyboard.nextInt();
    }

you see in this case, you get a one-line method which isn't very usefull. 在这种情况下,您会看到一种不是很有用的单行方法。 But you can split off more extensive calculations as well, this is just as an example. 但是,您也可以拆分出更广泛的计算,这仅是示例。 In addition, instead of all the if-blocks, you can use a switch statement like the other answer shows you. 另外,除了所有的if块,您还可以像其他答案所示那样使用switch语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM