Goto statements in Java [closed]

断了今生、忘了曾经 提交于 2019-12-01 02:00:45
cHao

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.

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.

According to this:

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

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

Lukas Eder

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 ;-)

Joshua

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

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);
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!