获取一个返回两个整数的函数

发布于 2024-09-17 23:44:23 字数 476 浏览 6 评论 0原文

我正在编写一个函数,我希望它返回两个整数作为结果。但是,我无法让它做到这一点。有人可以帮助我吗?这是我最好的镜头

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

飘落散花 2024-09-24 23:44:24

您可以让该方法返回一个 int 数组:

public static int[] calc (int s, int b, int c, int d, int g)

You could have the method return an array of int:

public static int[] calc (int s, int b, int c, int d, int g)
2024-09-24 23:44:24

创建一个“pair”类并将其返回。

public class Pair<T,Y>
{
    public T first;
    public Y second;
    public Pair(T f, Y s)
    {
        first = f;
        second = s;
    }
}

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;
    }
}
铜锣湾横着走 2024-09-24 23:44:24

创建一个包含两个整数的小型内部类。

private static class TwoNumbers {
    private Integer a;
    private Integer b;

    private TwoNumbers(Integer a, Integer b) {
        this.a = a;
        this.b = b;
    }
}

您创建该类的一个实例并返回该实例。

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.

人│生佛魔见 2024-09-24 23:44:24

对于这个特定问题,由于答案总是返回 s

....
    return s;
....
    return s && b;
....
    return s && c;
....
    return s && d;
....

您可以只返回第二个值。我使用 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");
    }
}

如果没有抛出异常,则 s 始终是第一个结果:

try {
    int result1 = s;
    int result2 = calc(s, b, c, d, g);
} catch (IndexOutOfBoundsException ex) {
    System.out.println("No Answer");
}

For this specific problem, since the answer always returns 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.

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:

try {
    int result1 = s;
    int result2 = calc(s, b, c, d, g);
} catch (IndexOutOfBoundsException ex) {
    System.out.println("No Answer");
}
无妨# 2024-09-24 23:44:24
package calcultor;

import java.util.*;
import java.util.Scanner;

public class Calcultor{
    
    public static void main(String args[]){
        input();
    }
    
    public static void input(){
        
        Scanner FirstNum = new Scanner(System.in);
        System.out.print("Enter the First number: ");
        int num01 = FirstNum.nextInt();
        
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Enter the second number: ");
        int num02 = secondNum.nextInt();
        
        output(num01, num02);
    }
    
    public static void output(int x ,int y){
        int sum = x + y;
        System.out.println("Sum of Two Number: "+sum);
        //return sum;
    }
}
package calcultor;

import java.util.*;
import java.util.Scanner;

public class Calcultor{
    
    public static void main(String args[]){
        input();
    }
    
    public static void input(){
        
        Scanner FirstNum = new Scanner(System.in);
        System.out.print("Enter the First number: ");
        int num01 = FirstNum.nextInt();
        
        Scanner secondNum = new Scanner(System.in);
        System.out.print("Enter the second number: ");
        int num02 = secondNum.nextInt();
        
        output(num01, num02);
    }
    
    public static void output(int x ,int y){
        int sum = x + y;
        System.out.println("Sum of Two Number: "+sum);
        //return sum;
    }
}
三月梨花 2024-09-24 23:44:24

你为什么要这样做?如果您有这样的需要,您不能将返回类型更改为字符串,因为在字符串的情况下,您可以在两个值之间使用分隔符,这将帮助您提取值....比如说 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文