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中有没有让程序回到循环开头的命令


基础教程推荐
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 多个组件的复杂布局 2022-01-01
- Java Swing计时器未清除 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 从 python 访问 JVM 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01