如何在自定义方法中调用主方法的计算

时间:2013-03-25 01:31:38

标签: java methods return

我有一个主方法和其他4种函数类型方法,其中包括计算,但是,如何将每个方法调用到main中并继续打印出计算结果。此外,我目前收到很多语法错误。

我已经尝试在需要时放置括号和括号,但这会导致更多错误。此外,我尝试在其他地方初始化字符串和整数,但似乎仍无法正常工作。任何帮助将不胜感激!

一些语法错误包括:';'预计在第60行

插入';'在第60行完成localVariableDelcartion

每行重复这些错误

import java.io.*;
//create the class


public class CirclemethodsFixedagain

{
    //main method
    public static void main(String[] args) throws IOException
    {


        BufferedReader myInput = new BufferedReader(new InputStreamReader(System.in));

        String numInput;
        String reqInput;
        String amountStr;
        double numInt = 0;
        double num = 0;


        System.out.println("This program will ask for a given user radius, then proceed to calculate the user input");
        System.out.println("The program will use four methods to achieve this, all calling back to the main method");
        System.out.println("Press any key to continue");
        numInput = myInput.readLine();

        // more user questions
        System.out.println("First, what would you like to calculate?");
        System.out.println("Enter '1' for Circumference, '2' for area, '3' for volume, or '4' for surface area");
        System.out.println("*NOTE* Pressing a key outside of this range or a regular character will re-prompt the original message");
        reqInput = myInput.readLine();
        numInt = Double.parseDouble(reqInput);

        // more user questions
        System.out.println("Now enter the radius of the required shape(Half of diameter)");
        System.out.println("*NOTE* Pressing a regular character will re-prompt the original message");
        numInput = myInput.readLine();
        num = Double.parseDouble(numInput);



    }
    //user created method, with each 
    public static int circlemethods(double circumference) throws IOException {

        {

            if (numInt == 1)
            {
                System.out.println("You chose to calculate circumference, given the radius :" + num);
                circumference = (Math.PI) * (2) * (num);
                System.out.print("The circumference of that sphere is :");
                return circumference;


            } 

            public static double circlemethods2 (double area)  throws IOException
            {   
                if (numInt == 2)
                {
                    System.out.println("You chose to calculate area, given the radius :" + num);
                    area = (Math.PI * num * num);
                    System.out.print("The area of the circle is :");

                    return area;   
                }   
            }     
            public static double circlemethods3 (double volume) throws IOException
            {
                if (numInput == 3)
                {
                    System.out.println("You chose to calculate volume, given the radius :" + num);
                    volume = (4 * Math.PI * num * num * num) / 3  ;
                    System.out.print("The volume of that sphere is : cm³");

                    return volume;
                }  
            }  
            public static double circlemethods4 (double surfaceArea) throws IOException  
                if (numInput == 4)
                {
                    System.out.println("You chose to calculate surface area, given the radius :" + num);
                    surfaceArea = 4 * Math.PI * num * num;
                    System.out.print("The Surface area of that sphere is :");

                    return surfaceArea;
                }
        }


    }
}

3 个答案:

答案 0 :(得分:0)

你的大括号 - {和}字符 - 不匹配。我已经修复了问题中代码的缩进,以便您可以更好地查看问题的起始位置 - 在方法circlemethods中。此外,circlemethods4缺少括号。

在整个程序中保持一致的缩进级别会使这些错误更加明显。

答案 1 :(得分:0)

编译错误由以下原因引起:

  1. 你不能将方法放在其他方法中,在circlemethod1之外移动circlemethods 2,3,4。

  2. 您的circlemethods没有看到numInt局部变量。它在main方法中声明,只在那个中可见。

  3. 我相信你不需要在每个圈子方法的开头发表声明。你宁愿需要这样的东西:

    if (numInt == 1)
    { 
        circlemethod1(radius);
    } else if (numInt == 2)  { 
        circlemethod2(radius);
    }
    

    等。在你的主要方法中。

    你也可以改变每个圆形方法的参数名称,因为我知道它总是半径。参数的当前名称是方法名称的良好候选者。

答案 2 :(得分:0)

以下是解决问题的输入:

  1. 你不能在方法中声明方法,它不是JAVA语法。只需正确检查支撑。使用任何IDE来执行相同的操作。
  2. 将numInt,num作为静态(类)变量。当你在静态方法中使用它们时。
  3. 使用专有名称和camelCasing命名法来命名任何方法 |例如calculateCircleArea(),calculateCircleVolume()等。
  4. 希望这能解决你的问题。