Java:标记为中断
Java: Labeled Break
我正在做在线学习的练习 Java,我很困惑!
我的程序的要点是我通过输入单个字符让用户 select 有一个选项,然后程序根据该值继续处理案例。如果执行默认情况,则意味着输入无效,然后我想 return 到用户输入提示。
我最初的想法是使用'goto',但是据我了解,除了我阅读代码之外,任何人都可能会被石头砸死。然后是 Java 中不存在 goto 的事实......所以在谷歌搜索时,我发现 'labeled breaks'。它看起来正是我所需要的。但是,我插入标签的位置无法识别,即使它与案例相同 class 。我应该怎么做?
String newLine = System.getProperty("line.separator");
restart:
System.out.println("Please select the type of shape you wish to calcuate information for: "
+ newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
//do something
break;
case "b":
//do something
break;
case "c":
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
break restart;
}
Java 允许您标记一个循环构造(例如 for
、while
),然后跳出其中一个循环的内部到外部级别。
该语言不允许您标记任意行和 "goto" 它们。
更新:显然我错了。 Java 支持标记任意块(但不支持单个语句)。参见
我相信你想标记一个块。像
restart: {
System.out.println("Please select the type of shape you wish to calculate "
+ "information for: " + newLine + "A: Square" + newLine + "B: Rectangle"
+ newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
//do something
break;
case "b":
//do something
break;
case "c":
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
break restart;
}
}
我想一个简单的方法是使用 do-while
循环。如果不满足条件(无效input/character),继续循环,否则设置flag为false
,出来
boolean inputFlag;
do {
System.out.println("Please select the type of shape you wish to calcuate information for: "
+ newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
inputFlag = false;
//do something
break;
case "b":
inputFlag = false;
//do something
break;
case "c":
inputFlag = false;
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
inputFlag = true;
}
} while (inputFlag);
出于类似原因,标记块不受欢迎goto
不受欢迎:这不是自然流。
话虽如此,您可能想知道如何管理您想要的行为,这非常简单:使用循环
//pseudo-code
while(something) {
repeatCode
}
在你的情况下,你会做类似的事情:
boolean choosing = true;
while(choosing) {
switch(...) {
case "a":
choosing = false;
break;
case "b":
choosing = false;
break;
}
}
您可能会觉得这有点冗长。相反,您可以在进入循环后立即将 choosing 设置为 false,如果用户没有输入正确的名称,则将其设置回 true。
或者更好的是,使用 do-while 循环:
boolean choosing = false;
do {
switch(...) {
case "a":
break;
default:
choosing = true;
break;
}
} while(choosing);
我正在做在线学习的练习 Java,我很困惑!
我的程序的要点是我通过输入单个字符让用户 select 有一个选项,然后程序根据该值继续处理案例。如果执行默认情况,则意味着输入无效,然后我想 return 到用户输入提示。
我最初的想法是使用'goto',但是据我了解,除了我阅读代码之外,任何人都可能会被石头砸死。然后是 Java 中不存在 goto 的事实......所以在谷歌搜索时,我发现 'labeled breaks'。它看起来正是我所需要的。但是,我插入标签的位置无法识别,即使它与案例相同 class 。我应该怎么做?
String newLine = System.getProperty("line.separator");
restart:
System.out.println("Please select the type of shape you wish to calcuate information for: "
+ newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
//do something
break;
case "b":
//do something
break;
case "c":
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
break restart;
}
Java 允许您标记一个循环构造(例如 for
、while
),然后跳出其中一个循环的内部到外部级别。
该语言不允许您标记任意行和 "goto" 它们。
更新:显然我错了。 Java 支持标记任意块(但不支持单个语句)。参见
我相信你想标记一个块。像
restart: {
System.out.println("Please select the type of shape you wish to calculate "
+ "information for: " + newLine + "A: Square" + newLine + "B: Rectangle"
+ newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
//do something
break;
case "b":
//do something
break;
case "c":
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
break restart;
}
}
我想一个简单的方法是使用 do-while
循环。如果不满足条件(无效input/character),继续循环,否则设置flag为false
,出来
boolean inputFlag;
do {
System.out.println("Please select the type of shape you wish to calcuate information for: "
+ newLine + "A: Square" + newLine + "B: Rectangle" + newLine + "C: Circle");
char typeShape = input.next().charAt(0);
String shape = Character.toString(typeShape);
switch (shape.toLowerCase()) {
case "a":
inputFlag = false;
//do something
break;
case "b":
inputFlag = false;
//do something
break;
case "c":
inputFlag = false;
//do something
break;
default:
System.out.println("Invalid selection. Please re-enter shape.");
inputFlag = true;
}
} while (inputFlag);
出于类似原因,标记块不受欢迎goto
不受欢迎:这不是自然流。
话虽如此,您可能想知道如何管理您想要的行为,这非常简单:使用循环
//pseudo-code
while(something) {
repeatCode
}
在你的情况下,你会做类似的事情:
boolean choosing = true;
while(choosing) {
switch(...) {
case "a":
choosing = false;
break;
case "b":
choosing = false;
break;
}
}
您可能会觉得这有点冗长。相反,您可以在进入循环后立即将 choosing 设置为 false,如果用户没有输入正确的名称,则将其设置回 true。
或者更好的是,使用 do-while 循环:
boolean choosing = false;
do {
switch(...) {
case "a":
break;
default:
choosing = true;
break;
}
} while(choosing);