java 1st 程序暴击

标签 java

<分区>

刚开始学习Java,写了一个计算用户所选形状面积的基本程序。我可以对我做对和做错的事情进行批评和评论吗?我猜很多都是糟糕的编程,但这就是我来这里的原因。

还有 1 个问题,当调用我的方法时,我需要输入完整路径,即 areaprog.areacode.. 你知道这是为什么吗?以下两个类的代码:

主程序

package areaprog;

import java.util.Scanner;

public class mainprog {


    public static void main (String [] args){

        //Area Menu Selection
        System.out.println("What shape do you need to know the area of?\n" +
        "1: Square?\n" +
        "2: Rectangle?\n" +
        "3: Triangle?\n" +
        "4: Circle? \n" +
        "5: Exit\n"     
        );

        //User input for menu
        Scanner reader = new Scanner(System.in);
        System.out.println("Number: ");
        int input = reader.nextInt();
        reader.nextLine();

        //Depending on user selection, depends on what method is called using switch.
    Scanner scan = new Scanner(System.in);

        //Square selection
        if (input == 1){
            System.out.println("What is a length of 1 side of the Square?\n");
                double s1 = scan.nextInt();
                double SqAns = areaprog.areacode.square(s1);
            System.out.println("The area of you square is: " + SqAns);
        }

        //Rectangle selection    
            if (input == 2){
            System.out.println("What is the width of your rectangle?.\n");
                double r1 = scan.nextInt();
            System.out.println("What is the height of your rectangle?\n");
                double r2 = scan.nextInt();
                double RecAns = areaprog.areacode.rect(r1, r2);
            System.out.println("The area of your rectangle is: " + RecAns);    
            }
        //Triangle selection
        if (input == 3){
            System.out.println("What is the base length of the triangle?.");
                double t1 = scan.nextInt();
            System.out.println("What is the height of your triangle?");
                double t2 = scan.nextInt();
                double TriAns = areaprog.areacode.triangle(t1, t2);
            System.out.println("The area of your triangle is " + TriAns);
        }
        //Circle selection
        if (input == 4){
            System.out.println("What is the radius of your circle?.");
                double c1 = scan.nextInt();
                double CircAns = areaprog.areacode.circle(c1);
            System.out.println("The area of your circle is " + CircAns);    

        }
        //Exit application
        if (input == 5){
            System.out.println("Goodbye.");
        System.exit(0);
        }


    }

}

面积计算

package areaprog;


public class areacode {

    public static double rect(double width, double height) {
        double a_value = width * height;

        return a_value;


    }

    public static double circle(double radius){
        double PI = Math.PI;
        double a_value = PI * Math.pow(radius, 2);

        return a_value;


    }

    public static double square(double side) {
        double a_value = Math.pow(side, 2);

        return a_value;


    }

    public static double triangle(double base , double height) {
        double a_value = (base/2)* height;

        return a_value;


    }
}

最佳答案

正如本杰明所说,您的问题属于别处。但这里有几点说明。

  • 您的类(class)应该使用大写字母。

  • 命名事物很重要。 Areacode 是一个奇怪的名字,您应该描述其功能,例如 AreaCalculator。

  • 函数名应该描述它的作用,所以你应该使用类似 getCircleArea 之类的东西而不是 circle

  • 在您的主要方法中,导入包可能更好,因此您只需说 AreaCalculator.getCircle(5);例如,而不是每次都输入包。

但是如果你从整体上看,这是一个非常好的第一个程序。如果你知道你为什么做你所做的一切(例如,为什么第二类是静态的是一个很好的做法),你应该很快就会做得很好。

您可以将在 main 方法中执行的几项操作放在单独的方法中,以获得更好的程序结构和更轻松的调试。

编辑/你的问题:如果你在文件的开头导入包,你不需要写整个路径。这是上面的评论之一。

关于java 1st 程序暴击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14315182/

相关文章:

java - GWT 获取应用程序的路径

java - XQuery 使用 Saxon 9-HE 执行 Java 函数

windows - 找不到 Glassfish 安装 JRE

java - 一个关于如何解决Java中字符串问题的问题

java - 为什么我的 char 打印为数字而不是字符?

java - REQUIRES_NEW 在 spring+hibernate 中不创建新事务

java - 通过调用 DAO.getAll() 避免收集大量 ID

java - 如何为 JAVA Swing 做最好的查找文本字段

java - 如何使生成的类包含来自 XML 模式文档的 Javadoc

java - 任意精度数字和 Javascript,Google Web Toolkit