Java 中的 Goto 语句

发布于 2024-10-09 03:28:29 字数 1432 浏览 0 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

爱情眠于流年 2024-10-16 03:28:29

到目前为止,Java 中还没有 goto。这是一个保留词,以防万一最终需要它,但据我所知,他们还没有使用过它。

可能的等效代码:

case 2:
    float sub1 = 0.0;
    do {
        System.out.println("Enter the marks (in 100):");
        System.out.println("Subject 1:");
        sub1 = Float.parseFloat(br.readLne());
    } while (sub1 >= 101);

    ... rest of the code ...

请注意,此代码对于这种特定情况是等效的。 goto 没有通用的替代品;如果有的话,他们只需调用goto并完成它。每种情况都会有所不同,并且替换将完全取决于 goto 的使用方式。

There is no goto in Java as of yet. It's a reserved word, in case there ends up being the need for it, but as far as I know, they haven't used it yet.

Probable equivalent code:

case 2:
    float sub1 = 0.0;
    do {
        System.out.println("Enter the marks (in 100):");
        System.out.println("Subject 1:");
        sub1 = Float.parseFloat(br.readLne());
    } while (sub1 >= 101);

    ... rest of the code ...

Note, this code would be equivalent for this particular situation. There's no universal replacement for goto; if there were, they'd just call it goto and be done with it. Each case will be different, and the replacement will depend entirely on how the goto would have been used.

深海夜未眠 2024-10-16 03:28:29

你不必使用 goto (已经没有了)好吧。对于这个问题我们来思考一下。我认为这可能有用

public class Goto
{

    public static void main(String[] args)
    {
        int goThere = 0;

        do
        {
            switch(goThere)
            {
                case 0:
                case 1:
                    System.out.println("Foo");
                    goThere = 3;
                    continue;

                case 2:
                    System.out.println("Baz");
                    goThere = -1;
                    continue;
                case 3:
                    System.out.println("Bar");
                    goThere = 2;
                    continue;
             }
        } while(false);
    }
}

试试这个。也许您可以扩展该代码。

You don't have to use goto (already there isn't) Ok. Let's think for this problem. I think this is may be useful

public class Goto
{

    public static void main(String[] args)
    {
        int goThere = 0;

        do
        {
            switch(goThere)
            {
                case 0:
                case 1:
                    System.out.println("Foo");
                    goThere = 3;
                    continue;

                case 2:
                    System.out.println("Baz");
                    goThere = -1;
                    continue;
                case 3:
                    System.out.println("Bar");
                    goThere = 2;
                    continue;
             }
        } while(false);
    }
}

Try this. And may be you can extend that code.

无人接听 2024-10-16 03:28:29

根据

在Java中,goto是保留字,但不可用。

According to this:

In Java, goto is a reserved word, but is unusable.

柏拉图鍀咏恒 2024-10-16 03:28:29

正如其他人指出的,Java 中没有 goto 语句。我想补充一点, 标签 是一个小小的替代方案。

As others pointed, there is no goto statement in Java. I want to add that labels are a slight alternative.

梦归所梦 2024-10-16 03:28:29

向前跳转

label: if (true) {
    // Do stuff
    if (check)
        break label;
    // Do more stuff
}

向后跳转

label: do {
    // Do stuff
    if (check)
        continue label;
    // Do more stuff
    break;
} while(true);

不应在任何明智的软件中使用;-)

Jumping forward

label: if (true) {
    // Do stuff
    if (check)
        break label;
    // Do more stuff
}

Jumping backward

label: do {
    // Do stuff
    if (check)
        continue label;
    // Do more stuff
    break;
} while(true);

It is not to be used in any sensible piece of software ;-)

牵强ㄟ 2024-10-16 03:28:29

虽然goto是Java中的保留关键字,但没有goto语句。

While goto is a reserved keyword in Java, there is no goto statement.

梦纸 2024-10-16 03:28:29

在这里重写您的代码,

将您的“Student”类放在与 Main.java 相同的包中;

package MyPackage

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Student stu = new Student();

        float sub1 = 0;
        int goThere = 0;

        do {
            switch(goThere){
                case -1:
                    System.out.println("if U want 2 use further press 0 to continue...");
                    goThere = Integer.parseInt(br.readLine());
                continue;

                case 0:
                    System.out.println("--------------STUDENT DETAILS---------------");
                    System.out.println("Choose the operation from the following options.");
                    System.out.println(" 1.ADDNAME");
                    System.out.println(" 2.AVERAGE_RESULT");
                    System.out.println(" 3.EXIT");
                    System.out.println("CHOOSE THE OPERATION U WANT:");

                    goThere = Integer.parseInt( br.readLine() );
                continue;

                case 1:
                    System.out.println("Enter the name");
                    String name = br.readLine();
                    System.out.println("The Inserted student name is " + stu.addName(name));
                    goThere = -1;
                continue;

                case 2:
                    System.out.println("Enter the marks (in 100):");
                    System.out.println("Subject 1:");
                    sub1 = Float.parseFloat(br.readLine());
                    goThere = 4;
                continue;

                case 4:
                    if( sub1 >= 101){
                        goThere = 2;
                        continue;
                    }

                    System.out.println("Subject 2:");
                    float sub2=Float.parseFloat(br.readLine());
                    System.out.println("Subject 3:");
                    float sub3=Float.parseFloat(br.readLine());
                    System.out.println("The Student is " + stu.average(sub1,sub2,sub3) + "in the examinations");
                    goThere = -1;
                continue;
             }
            break;
        } while(true);
    }
}

Rewrite for your code is here,

Put your "Student" class in the same package then Main.java;

package MyPackage

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws NumberFormatException, IOException {
        // TODO Auto-generated method stub
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Student stu = new Student();

        float sub1 = 0;
        int goThere = 0;

        do {
            switch(goThere){
                case -1:
                    System.out.println("if U want 2 use further press 0 to continue...");
                    goThere = Integer.parseInt(br.readLine());
                continue;

                case 0:
                    System.out.println("--------------STUDENT DETAILS---------------");
                    System.out.println("Choose the operation from the following options.");
                    System.out.println(" 1.ADDNAME");
                    System.out.println(" 2.AVERAGE_RESULT");
                    System.out.println(" 3.EXIT");
                    System.out.println("CHOOSE THE OPERATION U WANT:");

                    goThere = Integer.parseInt( br.readLine() );
                continue;

                case 1:
                    System.out.println("Enter the name");
                    String name = br.readLine();
                    System.out.println("The Inserted student name is " + stu.addName(name));
                    goThere = -1;
                continue;

                case 2:
                    System.out.println("Enter the marks (in 100):");
                    System.out.println("Subject 1:");
                    sub1 = Float.parseFloat(br.readLine());
                    goThere = 4;
                continue;

                case 4:
                    if( sub1 >= 101){
                        goThere = 2;
                        continue;
                    }

                    System.out.println("Subject 2:");
                    float sub2=Float.parseFloat(br.readLine());
                    System.out.println("Subject 3:");
                    float sub3=Float.parseFloat(br.readLine());
                    System.out.println("The Student is " + stu.average(sub1,sub2,sub3) + "in the examinations");
                    goThere = -1;
                continue;
             }
            break;
        } while(true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文