用户定义的方法执行问题

时间:2014-10-27 15:01:18

标签: java switch-statement user-defined

代码不再运行,因为我无法弄清楚如何让扫描程序运行,但如何让方法执行? 提示和帮助表示赞赏。 提前谢谢。

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

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 void main (String [] args)
 {
  int choice;   //the user's choice
  double value = 0; //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

  Scanner keyboard = new Scanner (System.in);
 }

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

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

    public static double trianglePerimeter(double side1, double side2, double side3, double value, Scanner keyboard) {
     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();
     //call the perimeter method and store the result in the value variable
     value = (side1 + side2 + side3);
     System.out.println("The perimeter of the triangle is " + value);
     return value;

    //default:
     //System.out.println("You did not enter a valid choice.");
    }
}

2 个答案:

答案 0 :(得分:3)

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

案例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;
}

然后,您可以重构一些主要变量,例如长度和宽度

答案 1 :(得分:0)

示例方法:

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;

确保通过Scanner。您可以针对其他两种情况执行此操作。

所以它最终看起来应该更像这样:

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');
    }
}