Scanner scan = new Scanner(System.in);loop:while (true) { String input = scan.nextLine(); switch (input) { case "attack": System.out.println("You attacked the enemy!"); break loop; case "defend": System.out.println("You blocked the enemy!"); break loop; default: System.out.println("This is not an option!"); break; }}while (true)可能会造成无限循环。在switch语句中,如果输入是攻击或防御,我们将跳出循环。如果两者都不是,我们只会跳出switch语句。
while循环用
loop:标签标记。这样我们就可以告诉它
break loop;打破循环。另一方面
break只打破了switch语句。



