java 获取一个返回两个整数的函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3648509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a function to return two integers
提问by Spencer
I am writing a function and I want it two return two integers as results. However, I cannot get it to do this. Could someone help me? Here is my best shot
我正在编写一个函数,我希望它返回两个整数作为结果。但是,我无法做到这一点。有人可以帮助我吗?这是我最好的镜头
public static int calc (int s, int b, int c, int d, int g)
{
if (s==g)
return s;
else if (s+b==g)
return s && b;
else if (s + c==g)
return s && c;
else if (s+d==g)
return s && d;
else
System.out.println("No Answer");
}
回答by akf
You could have the method return an array of int
:
您可以让该方法返回一个数组int
:
public static int[] calc (int s, int b, int c, int d, int g)
回答by JH.
Make a "pair" class and return it.
创建一个“配对”类并返回它。
public class Pair<T,Y>
{
public T first;
public Y second;
public Pair(T f, Y s)
{
first = f;
second = s;
}
}
回答by Jes
Make a small inner class that has two integers.
制作一个包含两个整数的小型内部类。
private static class TwoNumbers {
private Integer a;
private Integer b;
private TwoNumbers(Integer a, Integer b) {
this.a = a;
this.b = b;
}
}
You create a instance of the class and return that instead.
您创建一个类的实例并返回它。
回答by Bert F
For this specific problem, since the answer always returns s
:
对于这个特定问题,因为答案总是返回s
:
....
return s;
....
return s && b;
....
return s && c;
....
return s && d;
....
you could just return the 2nd value. I use 0 to indicate "just s
" since the first case (if (s==g)
) could be thought of as if (s+0==g)
. Use a different sentinel value than 0 for this, if necessary.
你可以只返回第二个值。我使用 0 来表示“只是s
”,因为第一种情况 ( if (s==g)
) 可以被认为是if (s+0==g)
. 如有必要,为此使用不同于 0 的标记值。
public static int calc (int s, int b, int c, int d, int g)
{
if (s==g)
return 0;
else if (s+b==g)
return b;
else if (s+c==g)
return c;
else if (s+d==g)
return d;
else {
// System.out.println("No Answer");
// Probably better to throw or return a sentinel value of
// some type rather than print to screen. Which way
// probably depends on whether "no answer" is a normal
// possible condition.
throw new IndexOutOfBoundsException("No Answer");
}
}
If no exception is thrown, then s
is always the first result:
如果没有抛出异常,那么s
总是第一个结果:
try {
int result1 = s;
int result2 = calc(s, b, c, d, g);
} catch (IndexOutOfBoundsException ex) {
System.out.println("No Answer");
}
回答by Saket Bansal
why do you want to do this? and if you have some need like this can't you change your return type to string, because in case of string you can have separator between two values which will help you in extracting values.... say 10&30 ,
你为什么要这样做?如果您有这样的需要,您不能将返回类型更改为字符串,因为在字符串的情况下,您可以在两个值之间使用分隔符,这将有助于您提取值.... 说 10&30 ,
I agree this is a wrong way of solving...i assumed that there is limitation of sticking to primitive datatype
我同意这是一种错误的解决方法……我认为坚持原始数据类型是有限制的