在下面的代码中,我试图插入方法并调用将使程序运行的代码。我试图将第一种方法放入程序中。
public class My_Geometry {
public static void printMenu() {
System.out.println("This is a geometry calculator, Choose what you would like to calculate: ");
System.out.println("1) Find the area of a rectangle");
System.out.println("2) Find the perimeter of a rectangle");
System.out.println("3) Find the perimeter of a triangle");
}
public static void main(String[] args) {
int choice; //the user's choice
double value; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
double radius; //the radius of the circle
double length; //the length of the rectangle
double width; //the width of the rectangle
double height; //the height of the triangle
double base; //the base of the triangle
double side1; //the first side of the triangle
double side2; //the second side of the triangle
double side3; //the third side of the triangle
//create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
//do loop was chose to allow the menu to be displayed first
letter = 'N';
while (letter != 'Y' && letter != 'y') {
printMenu();
choice = keyboard.nextInt();
value = 0.0;
switch (choice) {
case 1:
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
rectangleArea();
System.out.println("The area of the rectangle is " + value);
break;
case 2:
System.out.print("Enter the length of the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of the rectangle: ");
width = keyboard.nextDouble();
// add call to rectanglePerimeter method here
System.out.println("The perimeter of the rectangle is " + value);
break;
case 3:
System.out.print("Enter the length of side 1 of the triangle: ");
side1 = keyboard.nextDouble();
System.out.print("Enter the length of side 2 of the triangle: ");
side2 = keyboard.nextDouble();
System.out.print("Enter the length of side 3 of the triangle: ");
side3 = keyboard.nextDouble();
// add call to trianglePerimeter method here
System.out.println("The perimeter of the triangle is " + value);
break;
default:
System.out.println("You did not enter a valid choice.");
}
keyboard.nextLine(); //consumes the new line character after the number
System.out.println("Do you want to exit the program (Y/N)?: ");
String answer = keyboard.nextLine();
letter = answer.charAt(0);
}
}
}
public static void rectangleArea() {
value = length * width;
}
// add call to rectanglePerimeter method here
// add call to trianglePerimeter method here
public static rectangleArea(){
value = length * width;}
是我试图插入程序的方法。
这是我被要求放置它的位置,但我不知道它是否写得正确。我对此方法的调用是rectangleArea()
,它位于代码的中间,我相信这是调用此方法的正确方法。我只是不知道方法本身有什么问题,或者写它的正确方法是什么。
答案 0 :(得分:0)
选项1:为返回类型声明添加void
public static void rectangleArea(){
value = length * width;
}
并将您的变量提升到类的字段(通过移出main方法)。
选项2:将操作数移交给计算方法,让它们返回计算值并将返回值分配给main
方法的局部变量:
public static double rectangleArea(double lenght, double width){
return length * width;
}
public static void main(String[] args) {
// ..
double area = rectangleArea(l, w);
System.out.println("Area: " + area);
}
答案 1 :(得分:0)
您遇到了变量范围的问题。您已在main方法中声明了变量,因此它们仅在那里可见。使用正确的缩进格式化代码,我认为这会有所帮助。您可以考虑定义范围的每个{}。
答案 2 :(得分:0)
rectangleArea
没有返回类型。如果它什么都没有返回,那么它应该用void
例如......
public static void rectangleArea(){
该方法也在类之外,在Java中是非法的。您需要在最后}
value
,width
和length
在方法中没有上下文,因为它们在main
中被声明为局部变量。
您可能需要将length
和width
传递给该方法并返回value
,例如......
public static double rectangleArea(double length, double width) {
return length * width;
}
<强>更新... 强>
调用该方法需要三件事。它要求您将length
和width
传递给方法并将返回值赋给变量(假设您对返回结果感兴趣)...
例如......
value = rectangleArea(length, width);
答案 3 :(得分:0)
变量长度和宽度在main方法的范围内。 您可以在方法外部声明长度宽度和值变量并将它们设置为静态,或者您可以将值作为方法参数传递并返回计算值。 如果选择第一个建议的解决方案,则必须将void声明为方法的类型。