2016-08-23 106 views
2

我正在構建一個程序,我們特別不允許在子模塊中使用多個返回。如何在不使用多個返回的情況下返回值

我想知道如何通過jimAreaashtynAreasteveArea從子模塊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; 
} 
} 

任何幫助,非常感謝,謝謝。 約翰

+1

將區域計算方法改爲只計算一個區域並用不同的輸入調用它三次可能是值得考慮的。然後您可以輕鬆地捕獲調用方法中計算的三個區域。 –

+0

@duffymo我瞭解面向對象,但我們的講師確實沒有教過課程,所以我們打算在沒有它的情況下做。 – John

回答

5

這裏最快的修復可能只是返回一個集合的區域。例如,你可以返回一個List<Integer>而不是單個int,如:

public static List<Integer> areaCalc(double depth, double width, double length) 
{ 
    // Note: you don't actually use the method parameters in the assignments below. 
    // This is probably a typo. 
    int jimArea = (int)(jimDepth * jimWidth * jimLength); 
    int steveArea = (int)(steveDepth * steveWidth * steveLength); 
    int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength); 

    List<Integer> areaList = new ArrayList<>(); 
    areaList.add(jimArea); 
    areaList.add(steveArea); 
    areaList.add(ashtynArea); 

    return areaList; 
} 

如果你想獲得比這發燒友,那麼你可以考慮建立一個善意,善意POJO包裝了三倍區的值:

public class AreaClass { 
    private int jimArea; 
    private int steveArea; 
    private int ashtynArea; 

    // constructor, getters and setters 
} 
+0

怎麼樣地圖數據結構? – Vaseph

+0

方法areaCalc的參數未使用。 –

3

返回一個對象或數據結構。新程序員傾向於在原語方面考慮太多。你需要將它們組合成對象。

這段代碼是否可以編譯?

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; 
} 

哪裏有jimDepth其餘定義?我在你的main()方法中看到它們,但它們都不在areaCalc()的範圍內。

瞭解數據結構。事情是這樣的:

public class Cube { 
    private final double length; 
    private final double width; 
    private final double depth; 

    public Cube(double l, double w, double d) { 
     // Question: Do negative or zero dimensions make physical sense? 
     // What would you do about them here? 
     // Hint: Programming by contract and preconditions. 
     this.length = l; 
     this.width = w; 
     this.depth = d; 
    } 

    public double volume() { 
     return this.length*this.width*this.depth; 
    } 
} 

現在你可以有多個不同的每個人:

Map<String, Cube> volumes = new HashMap<>(); 
volumes.put("jim", new Cube(1, 2, 3)); 
volumes.put("steve", new Cube(4, 5, 6)); 
volumes.put("ashtyn", new Cube(7, 8, 9)); 

輕鬆添加更多或名稱查找。

-1

的解決方案是使用DTO - 數據傳送對象:類型來包裝乘法對象:

public static class AreaDto { 
    private int jimArea; 
    private int steveArea; 
    private int ashtynArea; 

    public AreaDto(int jimArea, int steveArea, int ashtynArea) { 
     this.jimArea = jimArea; 
     this.steveArea = steveArea; 
     this.ashtynArea = ashtynArea; 
    } 

    public int getJimArea() { 
     return this.jimArea; 
    } 

    public int getSteveArea() { 
     return this.steveArea; 
    } 

    public int getAshtynArea() { 
     return this.ashtynArea; 
    } 
} 

public static AreaDto areaCalc(int steveDepth, int steveWidth, int steveLength, int jimDepth, int jimWidth, int jimLength, int ashtynDepth, int ashtynLength, int ashtynWidth) 
{ 
    int jimArea = (int)(jimDepth * jimWidth * jimLength); 
    int steveArea = (int)(steveDepth * steveWidth * steveLength); 
    int ashtynArea = (int)(ashtynDepth * ashtynWidth * ashtynLength); 

    return new AreaDto(jimArea, steveArea, ashtynArea); 
} 
+0

DTO是一種J2EE反模式。 – duffymo

+0

duffymo,抱歉,但我不同意在這裏DTO是反模式。總的來說,它可能是,但不是在這裏。如果你想要,你可以命名爲'AreaResult',並刪除'DTO'。但它解決了任務。 –

0

另外一個選擇是返回一個數組保持所有的值。但我懷疑這會解決你的所有問題,正如duffymo在他的回答中指出的那樣。

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); 
int[] area = {jimArea,steveArea,ashtynArea}; 

return area; 
} 
相關問題