我正在构建一个程序,我们特别不允许在子模块中使用多个返回。如何在不使用多个返回的情况下返回值
我想知道如何通过jimArea
,ashtynArea
和steveArea
从子模块areaCalc
,使他们能够在主要使用。这里是我所指的子模块,后面是完整的代码。
public static int areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
}
这是完整的代码。本来我只是回来了,但事实证明我需要3个领域,所以我不知道如何做到这一点,而不做多次回报。
import java.util.*;
public class PoolStorageCalculation
{
public static void main(String [] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Please enter the Depth of Steve's Pool in Metres.");
double steveDepth = sc.nextDouble();
System.out.println("Please enter the Width of Steve's Pool in Metres.");
double steveWidth = sc.nextDouble();
System.out.println("Please enter the Length of Steve's Pool in Metres.");
double steveLength = sc.nextDouble();
System.out.println("Please enter the Depth of Jim's Pool in Metres.");
double jimDepth = sc.nextDouble()
System.out.println("Please enter the Width of Jim's Pool in Metres.");
double jimWidth = sc.nextDouble();
System.out.println("Please enter the Length of Jim's Pool in Metres.");
double jimLength = sc.nextDouble;
System.out.println("Please enter the Depth of Ashtyn's Pool in Metres.");
double ashtynDepth = sc.nextDouble();
System.out.println("Please enter the Width of Ashtyn's Pool in Metres.");
double ashtynWidth = sc.nextDouble();
Systemm.out.println("Please enter the Length of Ashtyn's Pool in Metres.");
double ashtynLength = sc.nextDouble();
int area = areaCalc(steveDepth,steveWidth,steveLength,jimDepth,jimWidth,jimLength,ashtynDepth,ashtynLength,ashtynWidth);
int numRays = rayCalc(steveArea);
int numSharks = sharkCalc(jimArea);
int numTurtles = turtleCalc(ashtynArea);
System.out.println("Steve can store " + numRays + " Sting Rays in his " + steveArea + " Metres Cubed Pool.");
System.out.println("Jim can store " + numSharks + " Sharks in his " + jimArea + " Metres Cubed Pool.");
System.out.println("Ashtyn can store " + numTurtles + " Turtles in her " + ashtynArea + " Metres Cubed Pool.");
}
public static int areaCalc(double depth, double width, double length)
{
int jimArea = (int)(jimDepth * jimWidth * jimLength);
int steveArea = (int)(steveDepth * steveWidth * steveLength);
int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength);
return area;
}
public static int rayCalc(int steveArea)
{
int numRays = (int)(steveArea * 0.5);
return numRays;
}
public static int sharkCalc(int jimArea)
{
int numSharks = (int)(jimArea * 0.1);
return numSharks;
}
public static int turtleCalc(int ashtynArea)
{
int numTurtles = (int)(ashtynArea * 1.2);
return numTurtles;
}
}
任何帮助,非常感谢,谢谢。 约翰
将区域计算方法改为只计算一个区域并用不同的输入调用它三次可能是值得考虑的。然后您可以轻松地捕获调用方法中计算的三个区域。 –
@duffymo我了解面向对象,但我们的讲师确实没有教过课程,所以我们打算在没有它的情况下做。 – John