Goto-Anweisungen in Java

Ich ausgeführt den untenstehenden code in Eclipse, aber die GOTO Aussagen sind nicht wirksam. Wie kann ich es verwenden?

Wie kann ich schreiben Sie den obigen code mit der Break-und Continue-Anweisungen ohne Verwendung der goto-Anweisung?

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

Durch den folgenden code wie vorgeschlagen, von einem der Stapel Overflow Mitgliedern machte mir den folgenden code schreiben:ABER das ist auch falsch.. ich bin am überlegen, warum die gelöscht die GOTO-Anweisungen in Java?

Dies ist nicht arbeiten entweder.

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);
    }
}
  • bitte aktualisieren Sie Ihre Frage mit dem code, den wir erstellen können[Lesen]
  • bitte formatieren Sie Ihre post
  • plz, verweisen auf diese. in diesem link ein Mitglied geschrieben hat code for tat GOTO-Anweisung also plz finden Sie unter diesem .. . stackoverflow.com/questions/2545103/...
  • gibt es eine entsprechende Möglichkeit , das verwendet werden kann anstelle von GOTO-Anweisung in meinem java-Programm zu implementieren, die auf Ihr Konzept.
  • Was ist mit jeder plötzlich wünschte für das SPRINGEN in jeder Sprache, eh? Eine groß angelegte Ausbruch von Masochismus?
  • Dijkstra ' s verderben ist schließlich gereinigt wird von der Erde.
  • wie usin abt in dieser situation
  • btw seine splld bitte nt plz
  • Das ist nur völliger Quatsch von oben nach unten. Die Abstimmung zu schließen.

Schreibe einen Kommentar