获取一个返回两个整数的函数
我正在编写一个函数,我希望它返回两个整数作为结果。但是,我无法让它做到这一点。有人可以帮助我吗?这是我最好的镜头
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");
}
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以让该方法返回一个
int
数组:You could have the method return an array of
int
:创建一个“pair”类并将其返回。
Make a "pair" class and return it.
创建一个包含两个整数的小型内部类。
您创建该类的一个实例并返回该实例。
Make a small inner class that has two integers.
You create a instance of the class and return that instead.
对于这个特定问题,由于答案总是返回
s
:您可以只返回第二个值。我使用 0 来表示“只是
s
”,因为第一种情况 (if (s==g)
) 可以被认为是if (s+0= =g)
。如有必要,请使用不同于 0 的标记值。如果没有抛出异常,则
s
始终是第一个结果:For this specific problem, since the answer always returns
s
: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 asif (s+0==g)
. Use a different sentinel value than 0 for this, if necessary.If no exception is thrown, then
s
is always the first result:你为什么要这样做?如果您有这样的需要,您不能将返回类型更改为字符串,因为在字符串的情况下,您可以在两个值之间使用分隔符,这将帮助您提取值....比如说 10&30 ,
我同意这一点是一种错误的解决方法...我认为坚持原始数据类型是有限制的
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 ,
I agree this is a wrong way of solving...i assumed that there is limitation of sticking to primitive datatype