Java中的Goto语句 [英] Goto statements in Java

查看:103
本文介绍了Java中的Goto语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I executed the below code in Eclipse, but the GOTO statements in it are not effective. How do I use it?

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

How do I rewrite the above code using the Break and Continue statements without using the goto statement?

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

通过以下代码建议其中一个堆栈  ;溢出成员让我写下面的代码:但这也是错误的..我在想为什么用Java删除了GOTO语句?

By the following code as suggested by one of the Stack Overflow members made me to write the following code:BUT that is also wrong.. I am thinking why the deleted the GOTO statements in Java?

这个也没用。

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


推荐答案

那里在Java中还没有 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.

可能的等效代码:

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

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.

这篇关于Java中的Goto语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆