Is there a command in java to make the program go back to the beginning of a loop(java中有没有让程序回到循环开头的命令)
问题描述
我正在尝试用java制作一种打字冒险游戏,但是我需要一个至少类似于标题中的命令,这是代码
I am trying to make a typing adventure kind of game in java, however i need a command at least similar to the one in the title, here is the code
import java.util.Scanner;
public class MyFirstGameInJava {
public static void main(String[] args) {
System.out.println("Greetings, Enter your name and you may start your quest!");
Scanner Username = new Scanner(System.in);
String name = Username.nextLine();
System.out.println("Greetings " + name );
System.out.println("Welcome to an Unnamed Typing Advanture");
System.out.println("You find yourself on an island with very few trees, you can either hit a tree, or walk along");
String sc = Username.nextLine();
switch(sc){
case "Hit tree":
System.out.println("A coconut falls from the tree");
System.out.println("You can either eat the coconut or throw it");
break;
case "Walk":
System.out.println("You walk for a mile and find a village");
System.out.println("The village appears empty, you can either scream to see if anybody is there, or you can keep walking");
break;
default :
System.out.println("Nothing happens...");
}
String sc1 = Username.nextLine();
switch(sc1){
case "Eat coconut":
System.out.println("You ate the coconut and got poisoned");
System.out.println("You died...");
break;
case "Throw coconut":
System.out.println("By throwing the coconut, you awaken a tiger and he eats you");
System.out.println("You are dead");
break;
case "Scream":
System.out.println("As soon as you scream, a man shoots you down from a window from one of the houses");
System.out.println("You died...");
break;
case "Walk":
System.out.println("You walked through the village, and you find a boat and leave the island");
System.out.println("You win! Updates coming soon!");
break;
default:
System.out.print("Nothing happend");
}
}
}
每当用户键入非必需的内容时,就会发生默认情况,但我需要它返回到循环的开头,以便用户可以键入其他情况之一.
Whenever the user types something else than required, the default case happens, but i need it to go back to the start of the loop, so the user can type one of the other cases.
推荐答案
可以使用continue
语句继续下一次迭代.
You can use the continue
statement to continue to the next iteration.
也就是说,我在您的示例代码中没有看到循环.您可以使用 for
、while
或 do/while
循环.do/while 循环至少执行一次——即通常在向用户提问时您想要做什么.
That said, I don't see a loop in your sample code. You can loop with a for
, while
or do/while
. The do/while loop executes at least once -- which is typically what you want to do when asking the user a question.
这个Java 分支语句教程提供了这个例子for
循环中的 continue
语句.
This Java tutorial for Branching Statements provides this example of a continue
statement in a for
loop.
for (int i = 0; i < max; i++) {
// interested only in p's
if (searchMe.charAt(i) != 'p')
continue;
// process p's
numPs++;
}
这篇关于java中有没有让程序回到循环开头的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:java中有没有让程序回到循环开头的命令


- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01