java第一程序标准
刚开始学习Java,编写了一个计算用户选择形状面积的基本程序。我能得到关于我做的对与错的批评和评论吗。我猜很多都是糟糕的编程,但这就是我来这里的原因 还有一个问题,当调用我的方法时,我需要输入完整路径,即areaprog.areacode。。你知道这是为什么吗?以下两个类别的代码: 主程序java第一程序标准,java,Java,刚开始学习Java,编写了一个计算用户选择形状面积的基本程序。我能得到关于我做的对与错的批评和评论吗。我猜很多都是糟糕的编程,但这就是我来这里的原因 还有一个问题,当调用我的方法时,我需要输入完整路径,即areaprog.areacode。。你知道这是为什么吗?以下两个类别的代码: 主程序 package areaprog; import java.util.Scanner; public class mainprog { public static void main (Str
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);例如,不要每次都键入包
编辑/提问:如果在文件开头导入包,则不需要写入整个路径。这是上面的一句话。使用java的编码约定
要养成写aValue的习惯,而不是一个值。你的问题似乎属于这里:谢谢,这将确保我在那里添加代码以供审阅。这正是我需要的,谢谢:)关于导入它?它们是同一个包的一部分,难道不应该这样做吗?如果我尝试导入areaprog,则无法解决此问题。就像我说的,对这些愚蠢的问题还是陌生的。我看到它们确实是和你说的一样的一部分。今天下午我没注意。如果您只调用Areaprog.getRectangleArea(5,10),它应该可以工作;(我已经使用了调整后的名称)。你犯了什么错误,或者出了什么问题?不要写一个值,要养成写aValue的习惯。。。有道理,谢谢:)