【问题标题】:How to return values without using multiple returns如何在不使用多个返回的情况下返回值
【发布时间】:2016-08-23 12:43:24
【问题描述】:

我正在构建一个程序,我们明确不允许在子模块中使用多个返回。

我想知道如何从子模块areaCalc 中传递jimAreaashtynAreasteveArea,以便它们可以在主模块中使用。这是我指的子模块,后面是完整的代码。

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

非常感谢任何帮助,谢谢。 约翰

【问题讨论】:

  • 可能值得考虑将面积计算方法更改为仅计算一个面积并使用不同的输入调用 3 次。然后就可以轻松的捕获调用方法中计算出来的三个区域了。
  • @duffymo 我知道面向对象,但我们的讲师还没有教过这门课,所以我们注定要在没有它的情况下做到这一点。

标签: java return


【解决方案1】:

这里最快的解决方法可能是只返回一个 collection 区域。例如,您可以返回 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
}

【讨论】:

  • Map 数据结构怎么样?
  • 方法areaCalc的参数没有使用。
【解决方案2】:

返回一个对象或数据结构。新程序员倾向于在原语方面考虑太多。您需要将它们组合成对象。

这段代码还能编译吗?

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

易于添加更多或按名称查找。

【讨论】:

    【解决方案3】:

    另一种选择是返回一个包含所有值的数组。但我怀疑这会解决你所有的问题,正如 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;
    }
    

    【讨论】:

      【解决方案4】:

      解决方案是使用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);
      }
      

      【讨论】:

      • DTO 是一种 J2EE 反模式。
      • duffymo,抱歉,我不同意这里的 DTO 是反模式。一般来说它可能是,但不是在这里。如果您愿意,您可以命名为“AreaResult”并删除“DTO”。但它解决了任务。
      猜你喜欢
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      相关资源
      最近更新 更多