【问题标题】:Calculating Areas of Geometric Figures using multiple methods in Java在 Java 中使用多种方法计算几何图形的面积
【发布时间】:2014-11-17 19:32:37
【问题描述】:

我的程序将计算面积,但我需要在它自己的方法中拥有每个不同的形状,我很难理解如何首先将它们全部分离到它们自己的方法中并将它们全部链接在一起。非常感谢任何帮助。

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");
        }

}
}

【问题讨论】:

    标签: java eclipse methods


    【解决方案1】:

    你可以使用 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;
    }
    

    【讨论】:

      【解决方案2】:

      您可以创建新的私有/公共方法来进行计算并将它们分开。

      参加这部分

      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();
      }
      

      你可以像这样创建一个新的计算方法

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

      然后,在你的 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();
          }
      

      你看到在这种情况下,你得到一个不是很有用的单行方法。但是你也可以拆分更广泛的计算,这只是一个例子。此外,您可以使用 switch 语句来代替所有 if 块,就像其他答案显示的那样。

      【讨论】:

        猜你喜欢
        • 2016-08-16
        • 1970-01-01
        • 2016-12-13
        • 2013-11-13
        • 1970-01-01
        • 2011-01-09
        • 2014-09-05
        • 2013-10-24
        • 1970-01-01
        相关资源
        最近更新 更多