如何正确使用goto语句 [英] How to use goto statement correctly

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

问题描述

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



我决定把一个 goto 语句扔进我们的一个实验室,只是为了玩,但我得到了这个错误。

 线程main中的异常java.lang.Error:未解决的编译问题:
令牌上的语法错误goto ,断言预期
重启无法解析为变量
在Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)

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

  import java.util。*; 

公共类Factorial
{
public static void main(String [] args)
{
int x = 1;
int factValue = 1;
Scanner userInput = new Scanner(System.in);
restart:
System.out.println(请输入非零,非负值以进行析因化。);
int factInput = userInput.nextInt();

while(factInput< = 0)
{
System.out.println(输入非零,非负值的因子化。);
factInput = userInput.nextInt();
}

if(x< 1)//这是执行上述while循环的另一种方式,我只是想玩得开心。
{
System.out.println(您输入的号码无效。请再试一次。);
goto restart;
}
while(x< = factInput)
{
factValue * = x;
x ++;
}
System.out.println(factInput +!=+ factValue);
userInput.close();
}
}


解决方案

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 Stackoverflow 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.

这篇关于如何正确使用goto语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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