在下面的代码中,我试图插入方法并调用将使程序运行的代码。 我试图将第一种方法放入程序中。
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 herepublic static rectangleArea(){ value = length * width;}是我尝试插入程序的方法。
这是我被要求放置它的位置,但我不知道它是否写得正确。 我对这个方法的调用是rectangleArea() ,它放在代码的中间,我相信这是调用这个方法的正确方法。 我只是不知道方法本身有什么问题,或者编写它的正确方法是什么。
In the following code I'm attempting to insert methods and call to's that will make the program operate. I've attempted to place the first method into the program.
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 herepublic static rectangleArea(){ value = length * width;} is the method that I am attempting to insert into the program.
This is the location that I was asked to place it, but I have no idea if it is written correctly or not. My call to this method is rectangleArea() which is placed in the middle of the code and I believe that is the correct way to call to this method. I just don't know what is wrong with the method itself or what the proper way to write it would be.
最满意答案
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);rectangleArea has no return type. If it returns nothing then it should be declared with void
For example...
public static void rectangleArea(){The method is also outside of the class, which is illegal in Java. You need to move it up before the last }
value, width and length have no context within the method, as these are declared as local variables within main.
You will probably need to pass length and width to the method and return the value, for example...
public static double rectangleArea(double length, double width) { return length * width; }Updated...
Calling the method requires three things. It requires you to pass the length and width to the method and assign the return value back to a variable (assuming you're interested in the return result)...
For example...
value = rectangleArea(length, width);更多推荐
发布评论