问题
I wrote a program in C++ with some goto statements. Now, I need to write the same program in Java. Is there any goto option in Java? If so, how it can be implemented? Is it same as the C++?
My program is:
#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();
}
回答1:
No, Java does not have a goto
operator, by design. It's considered harmful.
Your code could be rewritten with continue repeat
in place of the goto repeat
, if the repeat:
label was placed just before the for loop.
E.g.
repeat: for(i=13;i<=k;i++)
and then
continue repeat;
instead of goto repeat
回答2:
No Java doesn't have goto
in active state (but in reserved state). You cannot use it in your program for now (it's a reserved keyword).
And avoid using it in C++ either. You can write your program using smartly placed continue
and/or break
for both the languages.
回答3:
Short answer, No.
You can also refer this question
回答4:
Although goto is a reserved word in Java it is not used in the Java language. But there is a label, an identifier that can be used in conjunction with the break or continue. The purpose of the label it to let an iteration to jump outside of the iteration, it is a bit like goto statement.
Code:
labelA:
// some loop {
continue labelA;
}
回答5:
I wrote a program in C++ with some goto statements.
No, you didn't. C++ requires that main()
returns int
, and <stdio.h>
is a C library (and conio.h
a platform-specific C library). In C++, we spell it <cstdio>
, and we don't normally use it anyway (because <iostream>
is much more powerful and type-safe). However, your program is valid C.
Now,i need it to write the same program in java.
Good heavens, why? To the extent that I can figure out what your program is actually intended to do, it isn't anything useful at all. If this is for homework, your teacher is doing an incredibly bad job of explaining good coding style, from what I can see.
Is there any goto option in java ?
No, goto
is not implemented in Java. They did this because you don't have a reason to use it. No, really. The fact that it's all over the Linux kernel doesn't mean you have a reason. (It doesn't mean they have a real reason, either.)
Your program can be written more simply, for example:
#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();
}
And the same basic approach will work in Java, too.
Although you really need to work on several other style issues. Please try to use real variable names, and restrict variable scope.
回答6:
I do condone the use of goto on occasion. This is not one of those occasions. This particular problem can be solved without any kind of goto (break and continue are a goto, just a restricted form).
#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;
}
There are times where the clearest way to write a chunk of code involves break or continue. There are times where the clearest way to write a chunk of code involves a multi-level break or continue. While Java does provide such a mechanism, neither C nor C++ does.
As I said at the onset, I do condone the use of goto on occasion. The occasions are very, very rare:
- To emulate a multi-level break or continue when that truly is the clearest way to write the code.
- To deal with errors in a stupid programming environment that mandates single point of entry, single point of return.
- To deal with errors in a programming environment that makes try, catch, and throw forbidden keywords.
- To implement a finite state machine (but a loop around a switch usually quite nicely in this case).
回答7:
You can do anything with label that you could do with goto. Labels as an Anti-Pattern This doesn't make it a good idea.
What you are trying to do here doesn't need nested loops which avoids the need to use labels at all.
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);
}
来源:https://stackoverflow.com/questions/6620840/is-there-any-goto-statement-in-java