我正在構建一個程序,我們特別不允許在子模塊中使用多個返回。如何在不使用多個返回的情況下返回值
我想知道如何通過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