【问题标题】:How to call up calculations in custom methods to main Method如何将自定义方法中的计算调用到主方法
【发布时间】:2013-03-25 01:31:38
【问题描述】:

我有一个 main 方法和 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;
                }
        }


    }
}

【问题讨论】:

    标签: java methods return


    【解决方案1】:

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

    在整个程序中保持一致的缩进级别会使这类错误更容易被发现。

    【讨论】:

      【解决方案2】:

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

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

      2. 您的 circlemethods 看不到 numInt 局部变量。它在 main 方法中声明,并且仅在该方法中可见。

      我相信您不需要在每个循环方法的开头使用 if 语句。你宁愿需要这样的东西:

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

      等等。在你的主要方法中。

      您还可以更改每个圆形方法的参数名称,据我所知,它始终是半径。参数的当前名称是方法名称的理想候选者。

      【讨论】:

        【解决方案3】:

        以下是解决问题的输入:

        1. 您不能在方法中声明方法,这不是 JAVA 语法。只需正确检查支撑。使用任何 IDE 来做同样的事情。
        2. 将 numInt、num 设为静态(类)变量。正如您在静态方法中使用的那样。
        3. 使用专有名称和 camelCasing 命名法来命名任何方法。
          |例如 calculateCircleArea()、calculateCircleVolume() 等。

        希望这能解决您的问题。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-07-30
          • 2015-03-02
          • 1970-01-01
          • 1970-01-01
          • 2013-12-14
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多