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

查看:382
本文介绍了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天全站免登陆