gpt4 book ai didi

java - 如何在不使用多次返回的情况下返回值

转载 作者:行者123 更新时间:2023-12-02 15:18:12 24 4
gpt4 key购买 nike

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

我想知道如何从子模块 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;
}
}

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

最佳答案

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

关于java - 如何在不使用多次返回的情况下返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101569/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com