如何在java中使用goto语句? [英] How to use goto statement in java?
本文介绍了如何在java中使用goto语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经用java编写了代码。并需要在其中使用goto语句。我已经在不同的论坛检查了解决方案并找到了使用break标签的解决方案:声明,但即使我在程序中描述标签也会出错。
Hi, I've written a code in java. and need to use goto statement in it. I've checked for the solutions at different forums and came to solutions of using break label: statement but it gives error even I describe the label in my program like.
break label:
..........
.........
label:
我正在尝试的代码是
The code I'm trying is
import javax.swing.JOptionPane;
/**
*
* @author hitman
*/
public class TestApp {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int tries = 3;
String[] loginData;
loginData = new String[100];
short loginDataIndex = 0;
do{
String name;
name = JOptionPane.showInputDialog("Enter Username");
String pass = JOptionPane.showInputDialog("Enter Paswword");
String captcha = JOptionPane.showInputDialog("3 * 6 = ");
int captchaAnswer = Integer.parseInt(captcha);
switch(captchaAnswer){
case 18:
JOptionPane.showMessageDialog(null,"You are a Human","Login Sucessfull",JOptionPane.INFORMATION_MESSAGE);
break label: // instead of goto
break;
default:
JOptionPane.showMessageDialog(null,"You are not a Human", "Login Failed",JOptionPane.ERROR_MESSAGE);
}
loginData[loginDataIndex] = name;
loginDataIndex++;
--tries;
JOptionPane.showMessageDialog(null,"You are Left with "+tries+" tries.","Tries Left",JOptionPane.WARNING_MESSAGE);
}while(tries>0);
label: //catches the break label statement
JOptionPane.showMessageDialog(null,"Thanks for Visiting","Disabled",JOptionPane.INFORMATION_MESSAGE);
if(loginData != null){
short i = 0;
String failedUsers = "";
int n=0;
while(loginData[n]!=null){
failedUsers = failedUsers +" , "+loginData[n];
n++;
}
JOptionPane.showMessageDialog(null,"Users "+failedUsers+" are not Human.","Captcha Error",JOptionPane.ERROR_MESSAGE);
}
System.exit(0);
}
}
推荐答案
不,你不需要它。如果您觉得自己需要它,请尝试训练自己,以提高您的代码编写技巧。 Java缺少goto声明:
http://docs.oracle .com / javase / tutorial / java / nutsandbolts / _keywords.html [ ^ ]。
即使没有使用const和goto,它们也是保留的。这意味着语法会阻止使用它作为标识符。
-SA
No, you don't need it. If you feel you need it, please try to train yourself to improve your code-writing skills just a bit more. Java lacks "goto" statement:
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html[^].
Not that even though "const" and "goto" are not used, they are reserved. It means that syntax prevents using then even as identifiers.
—SA
设置标签在你的do-while循环之上:
Set the label above your do-while loop:
label: //catches the break label statement
do{
String name;
name = JOptionPane.showInputDialog("Enter Username");
String pass = JOptionPane.showInputDialog("Enter Paswword");
String captcha = JOptionPane.showInputDialog("3 * 6 = ");
int captchaAnswer = Integer.parseInt(captcha);
switch(captchaAnswer){
case 18:
JOptionPane.showMessageDialog(null,"You are a Human","Login Sucessfull",JOptionPane.INFORMATION_MESSAGE);
break label; // instead of goto
default:
JOptionPane.showMessageDialog(null,"You are not a Human", "Login Failed",JOptionPane.ERROR_MESSAGE);
}
loginData[loginDataIndex] = name;
loginDataIndex++;
--tries;
JOptionPane.showMessageDialog(null,"You are Left with "+tries+" tries.","Tries Left",JOptionPane.WARNING_MESSAGE);
}while(tries>0);
这篇关于如何在java中使用goto语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文