Java 如何将自定义方法中的计算调用到main方法
我有一个main方法和4个其他函数类型方法,其中包括计算,但是,我如何将每个方法调用到main中并继续打印计算结果。另外,我目前收到了很多语法错误 我曾尝试在需要时放置括号和大括号,但这只会导致更多错误。此外,我还尝试在其他地方初始化字符串和整数,但似乎仍然失败。任何帮助都将不胜感激 一些语法错误包括:“;”预计在第60行 插入“;”在第60行完成localVariableDelcartion 每行都会重复这些错误Java 如何将自定义方法中的计算调用到main方法,java,methods,return,Java,Methods,Return,我有一个main方法和4个其他函数类型方法,其中包括计算,但是,我如何将每个方法调用到main中并继续打印计算结果。另外,我目前收到了很多语法错误 我曾尝试在需要时放置括号和大括号,但这只会导致更多错误。此外,我还尝试在其他地方初始化字符串和整数,但似乎仍然失败。任何帮助都将不胜感激 一些语法错误包括:“;”预计在第60行 插入“;”在第60行完成localVariableDelcartion 每行都会重复这些错误 import java.io.*; //create the class p
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;
}
}
}
}
大括号{和}字符不匹配。我已经修复了问题中代码的缩进,这样您就可以更好地看到问题从哪里开始——在方法
circlemethods
中。此外,circlemethods4
缺少大括号
在整个程序中保持一致的缩进级别会使这些类型的错误更容易发现。编译错误是由以下原因引起的:
if (numInt == 1)
{
circlemethod1(radius);
} else if (numInt == 2) {
circlemethod2(radius);
}
在你的主要方法中
您还可以更改每个circlemethod的参数名称,因为我知道它总是半径。参数的当前名称是方法名称的最佳候选名称。以下是解决问题的输入:
|e、 g calculatecRClearea()、calculatecRClearume()等