防止 Java 中的 Orphaned Case 错误
- Java 中的 Orphaned Case 错误
-
Java 中避免
switch大小写错误的最佳实践 -
使用 Java
Scanner输入流获取用户输入 -
在 Java 中使用
Scanner获取用户输入并应用switch案例 -
Java 中在
switch中使用while循环重复获取用户数据
本文解释了如何防止 Java switch 语句中的孤立大小写错误。它将在示例中演示 switch 案例,并使用 Scanner 输入流使示例更加动态。
Java 中的 Orphaned Case 错误
在 Java 中,孤立错误并不常见。它与 switch 语句一起出现。
常见错误及解决方法:
Scanner 无法解析为类型。- 如果你的编译器抛出此错误,则意味着你必须导入:import java.util.Scanner;来使用这个功能。java.util.InputMismatchException- 如果检索到的令牌与指定类型的序列不匹配,或者如果令牌超出预期类型的范围,Scanner将抛出此错误。
Java 中避免 switch 大小写错误的最佳实践
我们将语法示例放在以下代码块中,以避免所有 switch 语句的错误。
不正确:
Switch(demo);
{
Case(demo);
Case;
switch 情况时,切勿使用大写字母。始终使用小写字母以避免最常见的错误。正确的:
switch (demo) {
case 1: // for integer type
case "String": // for string type
; 在 switch() 和 case 语句之后。在 case 后始终使用冒号 :,在按钮后不使用任何内容。最佳实践:
- 你的
case应该在你的switch语句中。 - 永远不要在
case语句中应用条件。 - 声明一个类型,然后将它传递给你的
switch。不要指望switch自行确定你的类型。
代码:
// Int Example
int num = 0;
switch (num) {
case 1: // your code
break; // if the condition is true, the case breaks
}
// String Example
String str = "A";
switch (str) {
case "str": // our code
// break;
}
让我们用 switch 案例制作出色的程序,同时避免罕见的孤立错误,如果现在很清楚的话。
使用 Java Scanner 输入流获取用户输入
以下是你应该了解的一些其他方法和参数,以充分理解此示例。
Scanner- 创建一个新的Scanner扫描提供的输入流并返回值。next();- 此Scanner查找并返回下一个完整标记("")。nextInt();- 它将输入的下一个标记扫描为int。
参数:要扫描的数据流。
语法:
import java.util.Scanner;
public static void main(String[] args) {
// Extend Scanner in Java
Scanner demo = new Scanner(System.in);
System.out.println("Print Something");
String a = demo.next(); // used for string
int b = demo.nextInt(); // used for integers
}
//(System.in): Theinput stream.
我们只是在 Scanner 的帮助下获取输入流并将其分配给 demo 变量。
在 Java 中使用 Scanner 获取用户输入并应用 switch 案例
示例 1:
// example1
package AvoidError;
import java.util.Scanner;
public class PreventOrphanedCaseErrorExample1 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner demo = new Scanner(System.in);
System.out.println("A");
System.out.println("B");
System.out.println("C");
System.out.println("D");
String a = demo.next();
switch (a) {
case "A":
System.out.println("You chose A");
break;
case "B":
System.out.println("You chose B");
break;
case "C":
System.out.println("You chose C");
break;
case "D":
System.out.println("You chose A");
break;
default:
System.out.println("You did not choose any value!");
break;
}
}
}
输出:

Java 中在 switch 中使用 while 循环重复获取用户数据
这个例子的不同之处只是一种更动态的方法,我们还将使用 equals(); 方法,它比较字符串。
如果 true,则返回给定对象;否则,它返回 false。仔细检查以下代码。
示例 2:
// Example 2
package AvoidError;
import java.util.Scanner;
public class PreventOrphanedCaseErrorExample2 {
public static void main(String[] args) {
try (Scanner nxt = new Scanner(System.in)) {
while (true) {
System.out.println("1: Fruits");
System.out.println("2: Vegies");
System.out.println("3: Chicken");
System.out.println("Please select your choice!");
String i = nxt.next();
switch (i) {
case "1":
System.out.println("You chose Fruits");
break;
case "2":
System.out.println("You chose Vegies");
break;
case "3":
System.out.println("You chose Chicken");
break;
default:
System.out.println("Sorry! You did not choose from given options");
}
System.out.println("Do you want to repeat this operation?");
String REPEAT = nxt.next();
if (!REPEAT.equals("YES")) {
break;
}
}
}
}
}
输出:

equals(); 之外,此示例中的所有内容都与前面的程序相同。我们存储了 nxt.next(); 在字符串 REPEAT 中,然后设置一个简单的条件来检查 equals(); 函数返回 true。在这种情况下,它返回字符串的 YES;因此,我们在 switch 语句中使用点得到了一个更加动态的程序。
通过这种理解,你可以使用一些 Java 和 switch 语句函数来做任何事情。
Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.
LinkedIn