如何正确使用goto语句

Ung*_*uer 27 java loops goto

我正在上高中AP计算机科学课.

我决定向goto我们的一个实验室发表声明,只是为了玩,但我得到了这个错误.

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)
Run Code Online (Sandbox Code Playgroud)

goto在Stackoverflow上找到了一个问题,以了解如何正确地完成它,并且我完全按照其中一个答案进行了演示.我真的不明白为什么编译器需要一个assert语句(至少这是我想的它),我也不知道如何使用assert.它似乎希望重启部分goto restart;是一个变量,但重启只是一个标签,将程序拉回到第10行,以便用户可以输入有效的int.如果它想重新启动变量,我该怎么做?

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

Tir*_*ath 70

正如所有答案已经指出的那样goto- Java在语言中使用了一个保留字并且未在该语言中使用.

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

如果您希望实现similar行为,您需要注意以下几点:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定我是否应该similar像往常一样说.

  • 这是一个很好的答案,+ 1,但如果你有这样做的愿望,你做错了,期间 - 重构,而是打破一些其他有名的方法.如果你使用这个构造,那么你最好避免使用所需的重构,让你的代码变得混乱并且你用一个很少程序员理解的结构来混淆它.更有可能的是,由于团队的其他成员无法制作更清晰的代码,您将被视为一名糟糕的程序员.这样做的愿望应该被认为是非常糟糕的代码味道. (5认同)
  • 这非常有帮助.我将不得不打印出这个答案并将其放入我的活页夹中进行学习和使用.+1.你值得+100,但我不能这样做.:) (3认同)
  • @BillK 好吧,我认为通过基本评论,您可以清楚地做到这一点。只要团队中的人能够理解这一点,我觉得就可以了。也就是说,我对团队缺乏经验,可能不知道我在说什么。 (3认同)

Adi*_*ngh 12

Java的关键字列表指定跳转关键字,但它被标记为"未使用".

这可能是为了将它添加到更高版本的Java中.

如果goto不在列表中,并且稍后将其添加到语言中,则使用单词goto作为标识符(变量名,方法名等)的现有代码将会中断.但是因为goto是一个关键字,这样的代码甚至不能在当前编译,并且仍然可以使它实际上稍后执行某些操作,而不会破坏现有代码.

  • 行.谢谢.我希望他们添加`goto`它是如此方便. (2认同)
  • 我希望他们永远不会使用它.在使用Java语言(和早期的C/C++)超过10年后,我总是找到更好的方法来设计函数/方法,而不是使用"bug创建者"的方式.请关注[苹果ssl实施安全漏洞](http://www.theguardian.com/technology/2014/feb/25/apples-ssl-iphone-vulnerability-how-did-it-happen-and-接下来是什么).好吧,他们也忘记了另一种最佳做法:即使它使代码更加冗长,也总是使用花括号... (2认同)
  • 已编程30多年.开始使用C.`goto`从一开始就可以使用,并且在整个过程中都被完全避免.最初,其他程序员让我不再使用它.随着时间的推移,我看到了"goto"的足够用法,给了我自己的信念和厌恶感.但是,我已经看到了一些(很少)使用(由其他程序员,可怜的灵魂)使用`goto`比使用它更清晰而不是它.这些用途可能会在以后被删除. (2认同)

Bil*_*l K 10

如果你继续查看并打破他们接受"标签".试验一下.转到本身是行不通的.

public class BreakContinueWithLabel {

    public static void main(String args[]) {

        int[] numbers= new int[]{100,18,21,30};

        //Outer loop checks if number is multiple of 2
        OUTER:  //outer label
        for(int i = 0; i<numbers.length; i++){
            if(i % 2 == 0){
                System.out.println("Odd number: " + i +
                                   ", continue from OUTER label");
                continue OUTER;
            }

            INNER:
            for(int j = 0; j<numbers.length; j++){
                System.out.println("Even number: " + i +
                                   ", break  from INNER label");
                break INNER;
            }
        }      
    }
}
Run Code Online (Sandbox Code Playgroud)

阅读更多


小智 5

Java不支持goto,它被保留为关键字,以防他们想将其添加到更高版本中

  • 他们应该添加它。它让生活变得更加轻松。 (4认同)