Java 执行用户定义的方法时出现问题

Java 执行用户定义的方法时出现问题,java,switch-statement,user-defined,Java,Switch Statement,User Defined,代码不再运行,因为我不知道如何让扫描仪运行,但如何让方法执行? 感谢您的提示和帮助。 提前谢谢 为了澄清,我希望这段代码像一个几何计算器一样运行,但是我真的不知道如何调用方法(我认为这是这里的问题) 我假设您只想将每个案例封装在方法中。只需为每种方法创建一个新方法 案例2的示例: private static double rectanglePerimeter(Scanner keyboard) { System.out.print("Enter the length of the r

代码不再运行,因为我不知道如何让扫描仪运行,但如何让方法执行? 感谢您的提示和帮助。 提前谢谢

为了澄清,我希望这段代码像一个几何计算器一样运行,但是我真的不知道如何调用方法(我认为这是这里的问题)


我假设您只想将每个案例封装在方法中。只需为每种方法创建一个新方法

案例2的示例:

private static double rectanglePerimeter(Scanner keyboard) {
     System.out.print("Enter the length of the rectangle:  ");
     double length = keyboard.nextDouble();
     System.out.print("Enter the width of the rectangle:  ");
     double width = keyboard.nextDouble();
     double value= (2*length)+(2*width);
     System.out.println("The perimeter of the rectangle is " + value);
     return value;
}
在您的案例块中,您可以像以下那样调用它:

switch (choice)
   {
    case 1:
     value = rectangleArea(keyboard);
     break;
    case 2:
     value = rectanglePerimeter(keyboard);
     break;
    case 3:
     value = trianglePerimeter(keyboard);
     break;
}
然后可以重构main之外的一些变量,例如长度和宽度示例方法:

public static double calculateRectangleArea(Scanner keyboard){
    System.out.print("Enter the length of the rectangle:  ");
    double length = keyboard.nextDouble();
    System.out.print("Enter the width of the rectangle:  ");
    double width = keyboard.nextDouble();
    double value= length*width;
    System.out.println("The area of a rectangle is " + value);
    return value;
}
示例调用:

case 1:
    value = calculateRectangleArea(keyboard);
    break;
确保通过
扫描仪
。你可以为另外两种情况做这些

因此,最终看起来应该更像这样:

import java.util.Scanner;
import java.lang.Math;

public class Geometry
{
    private static void printMenu()
    {
        System.out.println("This is a geometry calculator");
        System.out.println("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");
        System.out.println("Enter the number of your choice:");
    }

    public static double calculateRectangleArea(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = length*width;
        System.out.println("The area of a rectangle is " + value);
        return value;
    }

    public static double calculateRectanglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = (2*length)+(2*width);
        System.out.println("The perimeter of the rectangle is " + value);
        return value;
    }

    public static double calculateTrianglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of side 1 of the triangle:  ");
        double side1 = keyboard.nextDouble();
        System.out.print("Enter the length of side 2 of the triangle:  ");
        double side2 = keyboard.nextDouble();
        System.out.print("Enter the length of side 3 of the triangle:  ");
        double side3 = keyboard.nextDouble();
        //call the perimeter method and store the result in the value variable
        double value = (side1 + side2 + side3);
        System.out.println("The perimeter of the triangle is " + value);
        return value;
    }

    public static void main (String [] args)
    {
        int choice;   //the user's choice
        char letter;  //the Y or N from the user's decision to exit
        double value;
        Scanner keyboard = new Scanner (System.in);

        do
        {
            //call the printMenu method
            printMenu();
            choice = keyboard.nextInt();

            switch (choice)
            {
                case 1:
                    value = calculateRectangleArea(keyboard);
                    break;
                case 2:
                    value = calculateRectanglePerimeter(keyboard);
                    break;
                case 3:
                    value = calculateTrianglePerimeter(keyboard);
                    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);
        }while (letter != 'Y' && letter != 'y');
    }
}

请解释一下用户定义的含义好吗?我想OP希望将整个
开关
/
案例
语句移动到另一个
静态
方法中。是的,对不起,我希望所有3个数学开关都使用单独的静态方法。如果您希望添加其他功能,我强烈建议您开始为每个形状使用单独定义的类和父“形状”类(或接口)。您将学习如何格式化代码并完成您要求的基本操作。这在它成为有效解决方案之前就已经被升级了:/I我将发布更新版本的code@GiannaCaruso关于您编辑的代码,您的方法不是嵌套在main中,而是在switch语句中调用它们。你仍然需要包括你的swtich/case结构。我不能在main中调用方法,同时避免切换@Grice@GiannaCaruso您需要一个开关,以便它知道要调用哪些方法。您只需将计算移出单独的方法。好吧,现在调用应该在单独的switch语句中?@GiannaCaruso updated这很有帮助,但我不想使用void方法,我希望它们返回一个value@GiannaCaruso现在应该好了
import java.util.Scanner;
import java.lang.Math;

public class Geometry
{
    private static void printMenu()
    {
        System.out.println("This is a geometry calculator");
        System.out.println("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");
        System.out.println("Enter the number of your choice:");
    }

    public static double calculateRectangleArea(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = length*width;
        System.out.println("The area of a rectangle is " + value);
        return value;
    }

    public static double calculateRectanglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of the rectangle:  ");
        double length = keyboard.nextDouble();
        System.out.print("Enter the width of the rectangle:  ");
        double width = keyboard.nextDouble();
        double value = (2*length)+(2*width);
        System.out.println("The perimeter of the rectangle is " + value);
        return value;
    }

    public static double calculateTrianglePerimeter(Scanner keyboard){
        System.out.print("Enter the length of side 1 of the triangle:  ");
        double side1 = keyboard.nextDouble();
        System.out.print("Enter the length of side 2 of the triangle:  ");
        double side2 = keyboard.nextDouble();
        System.out.print("Enter the length of side 3 of the triangle:  ");
        double side3 = keyboard.nextDouble();
        //call the perimeter method and store the result in the value variable
        double value = (side1 + side2 + side3);
        System.out.println("The perimeter of the triangle is " + value);
        return value;
    }

    public static void main (String [] args)
    {
        int choice;   //the user's choice
        char letter;  //the Y or N from the user's decision to exit
        double value;
        Scanner keyboard = new Scanner (System.in);

        do
        {
            //call the printMenu method
            printMenu();
            choice = keyboard.nextInt();

            switch (choice)
            {
                case 1:
                    value = calculateRectangleArea(keyboard);
                    break;
                case 2:
                    value = calculateRectanglePerimeter(keyboard);
                    break;
                case 3:
                    value = calculateTrianglePerimeter(keyboard);
                    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);
        }while (letter != 'Y' && letter != 'y');
    }
}