Java中有goto语句吗?

Is there any goto statement in Java?

本文关键字:语句 goto 中有 Java      更新时间:2023-10-16

我用c++写了一个程序,里面有一些goto语句。现在,我需要用Java编写相同的程序。Java中有goto选项吗?如果可以,如何实施?它和c++一样吗?

我的程序是:

#include<stdio.h>
#include<conio.h>
void main()
{
    int i,j,k,w,l,q,d;
    clrscr();
    printf("nEnter the limit:");
    scanf("%d",&k);
    for(i = 13; i <= k ; i++)
    {
        repeat:
        j = i%10;
        if (j != 0)
        {
            if(i < 99)
            {
                for(w = 1; w <= 9; w++)
                {
                    l = 11*w;
                    if (l == i){
                        i++;
                        goto repeat;
                    }
                }
            }
            if(i > 99)
            {
                for(q = 1; q<=9 ; q++)
                {
                    d = 111*q;
                    if (d == i){
                        i++;
                        goto repeat;
                    }
                }
            }
            printf("%d,",i);
        }
    }
    getch();
}

不,Java在设计上没有goto操作符。它被认为是有害的

你的代码可以重写用continue repeat代替goto repeat,如果repeat:标签被放置在for循环之前。

repeat: for(i=13;i<=k;i++)

然后

continue repeat;

代替goto repeat

No Java没有goto活动状态(但在保留状态)。你现在不能在你的程序中使用它(它是一个保留关键字)。

也不要在c++中使用它。您可以使用适合这两种语言的continue和/或break来编写程序。

简短的回答,不

你也可以参考这个问题

虽然goto在Java中是保留字,但在Java语言中不使用它。但是有一个标签,一个可以与break或continue一起使用的标识符。标签的作用是让迭代跳转到迭代之外,有点像goto语句。

代码:

labelA:
// some loop {
    continue labelA;
}

我用c++写了一个程序,里面有一些goto语句。

不,你没有。c++要求main()返回int,而<stdio.h>是一个C库(conio.h是一个平台特定的 C库)。在c++中,我们将其拼写为<cstdio>,并且我们通常不使用它(因为<iostream>更强大且类型安全)。但是,您的程序是有效的c。

现在,我需要它在java中编写相同的程序。

天哪,为什么?在某种程度上,我可以弄清楚您的程序实际上打算做什么,它根本没有任何有用的东西。如果这是作业,在我看来,你的老师在解释良好的编码风格方面做得非常糟糕。

java有goto选项吗?

不,goto在Java中没有实现。他们这样做是因为你没有理由使用它。不,真的。它遍布Linux内核的事实并不意味着您有理由这样做。(这也不意味着他们有真正的理由。)

你的程序可以写得更简单,例如:

#include<stdio.h>  
#include<conio.h>  
void main()  
{  
    int i,j,k,w,l,q,d;  
    clrscr();  
    printf("nEnter the limit:");  
    scanf("%d",&k);  
    for(i=13;i<=k;i++)  
    {   
        j=i%10;  
        if (j == 0) continue;
        if (i<99)  
        {  
            for(w=1;w<=9;w++)  
            {  
                l=11*w;  
                if (l==i) continue;
            }  
        } 
        else
        {  
            for(q=1;q<=9;q++)  
            {  
                d=111*q;  
                if(d==i) continue; 
            }  
        }  
        printf("%d,",i);  
    }  
    getch();  
}

同样的基本方法也适用于Java。

虽然你真的需要处理一些其他的风格问题。请尽量使用真实的变量名,并限制变量范围

我偶尔允许使用goto。这不是那种情况。这个特殊的问题可以在没有任何goto的情况下解决(break和continue都是goto,只是一种限制形式)。

#include <iostream>
int main()
{
    unsigned int lim;
    std::cout << "Enter the limit: ";
    std::cin >> lim;
    std::cout << "n";
    if (lim > 999) {
        std::cout << lim << " is too large. Truncating to 999.n";
        lim = 999;
    }
    // Why start at 13? Oh well.
    for (unsigned int ii=13; ii <= lim; ii++) {
        if (((ii % 10) != 0) &&
            ((ii < 100) ? (ii % 11) != 0 : (ii % 111 != 0))) {
            std::cout << ii << ",";
        }
    }
    std::cout << "n";
    return 0;
}
有时候,写一段代码最清晰的方式是中断或继续。有时候,编写一段代码的最清晰的方式包括多级中断或继续。虽然Java提供了这样的机制,但C和c++都没有。

正如我在开头所说的,我偶尔会原谅使用goto。这种情况非常非常罕见:

  • 模拟多级中断或继续,当这真的是最清晰的编写代码的方式。
  • 处理愚蠢的编程环境中的错误,要求单点进入,单点返回。
  • 处理编程环境中的try、catch和throw禁止关键字的错误。
  • 实现一个有限状态机(但在这种情况下,一个围绕开关的循环通常很好)。

你可以用label做任何你可以用goto做的事情。标签作为一种反模式,这并不是一个好主意。

你在这里尝试做的不需要嵌套循环,这完全避免了使用标签的需要。

Scanner in = new Scanner(System.in);
System.out.print("nEnter the limit:");
int limit = in.nextInt();
for (int i = 12; i <= limit; i++) {
    if (i % 10 == 0) continue;
    if (i <= 99) {
        // two digits the same.
        if (i % 10 == i / 10 % 10) continue;
    } else if (i <= 999) {
        // three digits the same.
        if (i % 10 == i / 10 % 10 && i % 10 == i / 100) continue;
    } else {
        System.err.println("Value " + i + " too large to check");
        break;
    }
    System.out.printf("%d,", i);
}