Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java第一程序标准_Java - Fatal编程技术网

java第一程序标准

java第一程序标准,java,Java,刚开始学习Java,编写了一个计算用户选择形状面积的基本程序。我能得到关于我做的对与错的批评和评论吗。我猜很多都是糟糕的编程,但这就是我来这里的原因 还有一个问题,当调用我的方法时,我需要输入完整路径,即areaprog.areacode。。你知道这是为什么吗?以下两个类别的代码: 主程序 package areaprog; import java.util.Scanner; public class mainprog { public static void main (Str

刚开始学习Java,编写了一个计算用户选择形状面积的基本程序。我能得到关于我做的对与错的批评和评论吗。我猜很多都是糟糕的编程,但这就是我来这里的原因

还有一个问题,当调用我的方法时,我需要输入完整路径,即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;


    }
}

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

  • 你应该在课堂上使用大写字母

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

  • 函数名应该描述它的功能,所以不应该使用圆圈,而应该使用getCircleArea之类的东西

  • 在main方法中,导入包可能更好,因此只需说出AreaCalculator.getCircle(5);例如,不要每次都键入包

但是如果你看一下整个事情,这是一个非常好的第一个节目。如果你知道你为什么要做你所做的一切(例如,为什么这第二节课是静态的,这是一个很好的实践),你应该在任何时候都做得很好

您可以将在main方法中执行的几项操作放在不同的方法中,以获得更好的程序结构和更容易的调试


编辑/提问:如果在文件开头导入包,则不需要写入整个路径。这是上面的一句话。

使用java的编码约定

  • 类名以大写字母开头,在类名中,每个新名词和动词等的第一个字母都是大写字母
  • 若类名是mynewbook,那个么它应该写为mynewbook

  • 变量名总是以小写字母开头,在变量名中,每个新名词和动词等的首字母都是大写字母

    如果变量名为mynewbook,则应将其写入mynewbook

  • 对于常数。。。[最终变量]所有字母都将大写,您应该在所有新动词、名词等之间加上“uu”下划线

    若最后一个变量名是mynewbook,那个么它应该写为MY_NEW_BOOK



  • 要养成写aValue的习惯,而不是一个值。你的问题似乎属于这里:谢谢,这将确保我在那里添加代码以供审阅。这正是我需要的,谢谢:)关于导入它?它们是同一个包的一部分,难道不应该这样做吗?如果我尝试导入areaprog,则无法解决此问题。就像我说的,对这些愚蠢的问题还是陌生的。我看到它们确实是和你说的一样的一部分。今天下午我没注意。如果您只调用Areaprog.getRectangleArea(5,10),它应该可以工作;(我已经使用了调整后的名称)。你犯了什么错误,或者出了什么问题?不要写一个值,要养成写aValue的习惯。。。有道理,谢谢:)