Java中的Goto语句

时间:2010-12-28 16:46:38

标签: java goto

我在Eclipse中执行了以下代码,但其中的GOTO语句无效。我该如何使用它?

如何在不使用goto语句的情况下使用Break和Continue语句重写上述代码?

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

/**
 *
 */

/**
 * @author Home
 *
 */
public class student
{
    /**
    * @param args
    */
    String average(float sub1,float sub2,float sub3)
    {
        float average = (sub1+sub2+sub3)/3;
        if( average > 50)
            return "PASS";
        else
            return "FAIL";
    }

    String addName(String name)
    {
        return name;
    }

    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();
        int loop_option = 0;
        do
        {
            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:");

            int option = Integer.parseInt(br.readLine());
            switch(option)
            {
                case 1:
                    System.out.println("Enter the name");
                    String name = br.readLine();
                    System.out.println("The Inserted student name is " +stu.addName(name));
                break;

                case 2:
                    outsideloops:
                    System.out.println("Enter the marks (in 100):");
                    System.out.println("Subject 1:");
                    float sub1 = Float.parseFloat(br.readLine());
                    if (sub1 >= 101)
                        goto outsideloops;
                    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");
                    break;

                case 3:
                    System.exit(0);

                default:
                    System.out.println("Please choose the valid option");
                    //break;
            }
            System.out.println("if U want 2 use further press 1 to continue...");
           loop_option=Integer.parseInt(br.readLine());
        }
        while (loop_option == 1);
        System.out.println("The STUDENT program is terminating now.");
    }
}

通过以下代码,其中一个Stack Overflow成员建议我编写以下代码:但这也是错误的..我在想为什么在Java中删除了GOTO语句?

这也不起作用。

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

public class Main {
    String average(float sub1,float sub2,float sub3)
    {
        float average=(sub1+sub2+sub3)/3;
        if( average>50)
            return "PASS";
        else
            return "FAIL";
    }

    String addName(String name)
    {
        return name;
    }

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

        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;
                        }
                        else {goThere = 3;}
                    }
                    continue;

                case 3:
                    System.out.println("Subject 2:");
                    float sub2=Float.parseFloat(br.readLine());
                    goThere =5;
                    continue;

                case 5:
                    {
                        if( sub2 >= 101)
                        {
                            goThere = 3;
                        }
                        else {
                            goThere = 6;
                        }
                    }
                    continue;

                case 6:
                    System.out.println("Subject 3:");
                    float sub3 = Float.parseFloat(br.readLine());
                    goThere = 7;
                    continue;

                case 7:
                    {
                        if( sub3 >= 101)
                        {
                            goThere = 6;
                        }

                    }
                    continue;

                    System.out .println("The Student is " + stu.average(sub1,sub2,sub3) + "in the examinations");
                    goThere = -1;
                    continue;
            }
            break;
        } while(true);
    }
}

7 个答案:

答案 0 :(得分:21)

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将如何使用。

答案 1 :(得分:6)

你不必使用goto(已经没有)Ok。让我们考虑一下这个问题。我认为这可能有用

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

试试这个。也许你可以扩展那段代码。

答案 2 :(得分:5)

根据this

  

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

答案 3 :(得分:5)

正如其他人指出的那样,Java中没有goto语句。我想补充说labels是一个轻微的选择。

答案 4 :(得分:5)

向前跳

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

不得在任何合理的软件中使用; - )

答案 5 :(得分:2)

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

答案 6 :(得分:1)

重写代码就在这里,

将“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);
    }
}