java中有没有让程序回到循环开头的命令 [英] Is there a command in java to make the program go back to the beginning of a loop

查看:35
本文介绍了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.

也就是说,我在您的示例代码中没有看到循环.您可以使用 forwhiledo/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中有没有让程序回到循环开头的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆