猿问

如何在Java中使用'goto'的替代方法

我怎样才能在 Java 中使用任何替代“goto”的方法?


我尝试使用中断标签。但是由于我没有跳出任何循环,所以它给出了未定义的标签错误。


import java.io.*;


class $08_02_Total_Avg_Marks

{

    public static void main(String args[]) throws IOException

    {

        //declare and initialize variables

        int subNo = 0, totalMarks = 0;

        float avg = 0.0F;



    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   

label1:

    System.out.println("Enter no. of subjects");


    //check if input is integer

    try

    {

        subNo = Integer.parseInt(br.readLine().trim());

    }

    catch(NumberFormatException e)

    {

        System.out.println("Please enter a whole number.");

        //goto label1

    }


    int[] marksArray = new int[subNo];


    for(int i=0; i<marksArray.length; i++)

    {label2:

        System.out.println("Enter marks for subject " + (i+1));

        try

        {

            marksArray[i] = Integer.parseInt(br.readLine().trim());

        }

        catch(NumberFormatException e)

        {

            System.out.println("Please enter a whole number.");

            //goto label2

        }

    }


}

}

我正在终止无效输入的程序。但是我需要在无效输入上执行相同的行。


回首忆惘然
浏览 175回答 4
4回答

斯蒂芬大帝

与其明确地转到特定点,不如将您可能想要在循环中重复的位包装起来。如果您不想再次执行循环,break.对于第一个:while (true) {&nbsp; System.out.println("Enter no. of subjects");&nbsp; //check if input is integer&nbsp; try&nbsp; {&nbsp; &nbsp; &nbsp; subNo = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; &nbsp; break;&nbsp; }&nbsp; catch(NumberFormatException e)&nbsp; {&nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; // Nothing required to continue loop.&nbsp; }}对于第二个,将循环体包裹在循环中:for(int i=0; i<marksArray.length; i++){&nbsp; while (true) {&nbsp; &nbsp; System.out.println("Enter marks for subject " + (i+1));&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; marksArray[i] = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; catch(NumberFormatException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; }&nbsp; }}或者,写一个包装这个循环的方法可能更好:int getInt(BufferedReader br) throws IOException {&nbsp; while (true) {&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; return Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; } catch(NumberFormatException e) {&nbsp; &nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; }&nbsp; }}然后调用此方法:System.out.println("Enter no. of subjects");int subNo = getInt(br);for(int i=0; i<marksArray.length; i++) {&nbsp; &nbsp; System.out.println("Enter marks for subject " + (i+1));&nbsp; &nbsp; marksArray[i] = getInt(br);}

慕码人2483693

我已经稍微重新格式化了您的代码。我的基本想法是:所有的 goto 语句都可以写成等价的循环。第一个现在是用一个 while 循环制作的,它终止了一个没有例外。至于第二个标签,它是用相同的机制完成的(所以是一个 while 循环),但是,标签可以用“break + nameOfYourLable” - 语句退出/终止。import java.io.*;class $08_02_Total_Avg_Marks{public static void main(String args[]) throws IOException{&nbsp; &nbsp; //declare and initialize variables&nbsp; &nbsp; int subNo = 0, totalMarks = 0;&nbsp; &nbsp; float avg = 0.0F;BufferedReader br = new BufferedReader(new InputStreamReader(System.in));&nbsp; &nbsp;boolean goToLabel1 = true;while (goToLabel1) {System.out.println("Enter no. of subjects");//check if input is integertry{&nbsp; &nbsp; subNo = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; goToLabel1 = false;&nbsp; //parsing succeeded, no need to jump to label1}catch(NumberFormatException e){&nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; //goto label1}}int[] marksArray = new int[subNo];for(int i=0; i<marksArray.length; i++){&nbsp; &nbsp; label2: while (true) {&nbsp; &nbsp; System.out.println("Enter marks for subject " + (i+1));&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; marksArray[i] = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; &nbsp; &nbsp; break label2;&nbsp; &nbsp; }&nbsp; &nbsp; catch(NumberFormatException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; }}}}}

摇曳的蔷薇

此代码片段将循环直到插入正确的数字,在本例中(它解决了您的第一个 goto 问题)BufferedReader br = new BufferedReader(new InputStreamReader(System.in));&nbsp;&nbsp;boolean noNumberEntered; //Default on falseSystem.out.println("Enter no. of subjects");//TODO: check if input is integerwhile(!noNumberEntered){&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; subNo = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; &nbsp; &nbsp; noNumberEntered = true;&nbsp; &nbsp; }&nbsp; &nbsp; catch(NumberFormatException e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; }}

翻阅古今

您可以改用do while循环和布尔值,如下所示:class $08_02_Total_Avg_Marks{&nbsp; &nbsp; public static void main(String args[]) throws IOException&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //declare and initialize variables&nbsp; &nbsp; &nbsp; &nbsp; int subNo = 0, totalMarks = 0;&nbsp; &nbsp; &nbsp; &nbsp; float avg = 0.0F;&nbsp; &nbsp; BufferedReader br = new BufferedReader(new InputStreamReader(System.in));&nbsp;&nbsp;&nbsp; &nbsp; boolean goodEntry = true;&nbsp;&nbsp; &nbsp; do {&nbsp; &nbsp; &nbsp; &nbsp; goodEntry = true;&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Enter no. of subjects");&nbsp; &nbsp; &nbsp; &nbsp; //check if input is integer&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subNo = Integer.parseInt(br.readLine().trim());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch(NumberFormatException e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Please enter a whole number.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; goodEntry = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } while(!goodEntry);}你可以对你的第二个做同样的事情goto。有很多方法可以做到这一点(使用while循环和布尔值,使用breaks...),但循环比goto.
随时随地看视频慕课网APP

相关分类

Java
我要回答