如何正确返回此程序中的语句?

时间:2014-03-02 21:17:00

标签: java

如果条件为真,我希望我的程序执行第一个return语句,否则按正常方式返回。

import java.util.Scanner;
public class Untitled {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int answer = x + y;

        if (x == y) {
            System.out.println("These two numbers are the same and they equal: " + answer);     
            return answer;
        }

        System.print.ln("These two numbers are not the same, but they equal: " + answer);
        return answer;
    }
}

4 个答案:

答案 0 :(得分:2)

好吧,在您的代码中,您不需要返回任何内容。 但是,如果你想要用错误的方法返回一些东西。

main是一个“特殊”方法,它不能返回任何东西(好吧,在其他语言中它可能返回一个int,这是退出状态,但在这里忽略它),并且是你的代码开始的地方。 p>

如果你想要归还某些东西,你应该写一个method

方法是一个代码块,在您调用它时执行,并且可以将值返回给调用它的人。在这种情况下,您的方法将是静态的,因为您将从main方法(静态

)调用它
public static int methodName()
{
    int X, Y;
    int answer;

    if (x == y)
    {
        // Do something
        return returnSomethingBackTomain;
    }
    else
    {
        return returnSomethingOfDifferentTomain;
    }
}

嗯..这可能是你的方法

public static int methodName()
{
    Scanner input = new Scanner(System.in);
    int x = input.nextInt();
    int y = input.nextInt();
    int answer = x + y;

    if (x == y) {
        System.out.println("These two numbers are the same and they equal: " + answer);
    }
    else {
        System.out.println("These two numbers are not the same, but they equal: " + answer);
    }
    return answer;
}

但请更具体地说明你想做什么。 因为我没有理由写出这样的方法。

答案 1 :(得分:0)

如果您的主要功能无效,则无需返回任何内容,您只需使用

即可
return;

这给出了:

import java.util.Scanner;
public class Untitled {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int answer = x + y;

        if (x == y) {
            System.out.println("These two numbers are the same and they equal: " + answer);     
            return;
        }

        System.print.ln("These two numbers are not the same, but they equal: " + answer);
    }
}

但你也可以这样做:

    ...
        if (x == y) {
            System.out.println("These two numbers are the same and they equal: " + answer);     
        } else {
            System.print.ln("These two numbers are not the same, but they equal: " + answer);
        }
    }
}

答案 2 :(得分:0)

你犯了一些错误。

永远不应该使用main方法返回值 主要方法是程序的入口点。 在这里,您声明要在程序开始时运行的方法。在GUI项目中,这用于加载初始GUI(表单)。

此外,虚空方法无法返回某些内容 在初始化方法时,您必须声明返回值将具有的类型。 Void意味着没有返回数据。

您必须编写一个可以调用以接收数据的方法。

import java.util.Scanner;
public class Untitled {

public int scanForEqualNumbers() {
    Scanner input = new Scanner(System.in);
    int x = input.nextInt();
    int y = input.nextInt();
    int answer = x + y;

    if (x == y) {
        System.out.println("These two numbers are the same and they equal: " + answer);     
        return answer;
    }

    return answer;
}

现在您可以将您的课程(例如扫描程序)初始化,并调用方法 scanForEqualNumbers

Scanner scanner = new Scanner();
int result = scanner.scanForEqualNumbers();

如果您不想初始化类以调用该方法,则可以将 scanForEqualNumbers 声明为静态。

public static int scanForEqualNumbers() { ...

然后你可以直接调用这个方法:

int result = Scanner.scanForEqualNumbers();
再多注意一点。
您不需要IF声明中的回报 无论如何,当您返回答案时,您可以将打印保留在IF语句中并在其后使用返回 您还可以在ELSE语句中返回-1作为答案,以便您可以在其他方法中使用此答案。这样,您无需读取控制台输出即可检查方法的结果。

我甚至建议为此单独上课 您需要使用Main方法来运行程序。这也是你的方法类的位置。

public class MyApp {

    public static void main(String[] args) {
        MyScanner myScanner = new MyScanner();
        int result = myScanner.scanForEqualNumbers();
        System.out.format("The scanned numbers %s equal", result > 0 ? "ARE" : "are NOT");
    }

}

public class MyScanner {
    import java.util.Scanner;
    public class Untitled {

    public int scanForEqualNumbers() {
        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        /* This can be replaced, see below
        int answer = x + y;

        if (x != y) {
            answer = -1;
        }

        return answer;
        */

        // IF X equals Y ? return X + Y : ELSE return -1
        return (x == y ? x + y : -1);
    }
}

答案 3 :(得分:0)

import java.util.Scanner;
public class Untitled {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int x = input.nextInt();
        int y = input.nextInt();
        int answer = x + y;

        if (x == y)
            System.out.println("These two numbers are the same and they equal: " + answer);     
        else
            System.print.ln("These two numbers are not the same, but they equal: " + answer);
    }
}

main有一个void返回类型,因此无需返回任何内容。

顺便说一句,试着符合CQS原则 实际上,打印某些内容是一种副作用,因此除了void之外无需返回任何内容。