且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何正确使用goto语句

更新时间:2023-02-11 09:29:25

As所有答案已经指出 goto - Java 中的保留字,并且未在该语言中使用。



restart:被称为标识符后跟冒号。



类似的行为,那么您需要注意以下几点:
  outer://应该放在循环之前
loopingConstructOne {//我们可以在外部之前有语句但不在标签和循环之间的语句
inner:
loopingConstructTwo {
继续; //这将循环到loopingConstructTwo的顶部并继续。
休息; //这会打破循环的构造2。
继续外围; //这将转到外部标签并重新进入loopingConstructOne。
突破外部; //这会打破循环的构造方法。
继续内在; //这将表现得类似于继续。
打破内心; //这将与break类似
}
}

我不确定是否应该说类似,因为我已经拥有。


I am taking my high school AP Computer Science class.

I decided to throw a goto statement into a one of our labs just to play around, but I got this error.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "goto", assert expected
    restart cannot be resolved to a variable
at Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)

I went to a goto question on *** to find out how to do it properly, and I did exactly as was demonstrated in one of the answers. I really don't understand why the compiler wants an assert statement (at least that's what I assume it wants), nor do I have any idea how to use assert. It seems to want the restart part of goto restart; to be a variable, but restart is just a label that pulls the program back up to line 10 so that the user can enter a valid int. If it wants restart to be a variable, how do I do that?

import java.util.*;

public class Factorial 
{
    public static void main(String[] args) 
    {
        int x = 1;
        int factValue = 1;
        Scanner userInput = new Scanner(System.in);
        restart:
        System.out.println("Please enter a nonzero, nonnegative value to be factorialized.");
        int factInput = userInput.nextInt();

        while(factInput<=0)
        {
            System.out.println("Enter a nonzero, nonnegative value to be factorialized.");
            factInput = userInput.nextInt();
        }

        if(x<1)//This is another way of doing what the above while loop does, I just wanted to have some fun.
        {
            System.out.println("The number you entered is not valid. Please try again.");
            goto restart;
        }
        while(x<=factInput)
        {
            factValue*=x;
            x++;
        }
        System.out.println(factInput+"! = "+factValue);
        userInput.close();
    }
}

As already pointed out by all the answers goto - a reserved word in Java and is not used in the language.

restart: is called an identifier followed by a colon.

Here are a few things you need to take care of if you wish to achieve similar behavior -

outer:                  // Should be placed exactly before the loop
loopingConstructOne  {  // We can have statements before the outer but not inbetween the label and the loop          
    inner:
    loopingConstructTwo {
        continue;       // This goes to the top of loopingConstructTwo and continue.
        break;          // This breaks out of loopingConstructTwo.
        continue outer; // This goes to the outer label and reenters loopingConstructOne.
        break outer;    // This breaks out of the loopingConstructOne.
        continue inner; // This will behave similar to continue.
        break inner;    // This will behave similar to break.
    }
}

I'm not sure of whether should I say similar as I already have.