java返回if前的指令_Java中是否有命令使程序返回循环的开头

我正在尝试用Java进行打字冒险类游戏,但是我至少需要一个与标题中的命令相似的命令,这是代码

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");

}

}

}

每当用户键入除要求之外的其他内容时,就会发生默认情况,但是我需要它返回到循环的开始,因此用户可以键入其他情况之一。

您的代码中的循环在哪里? 开关不循环。

将代码包装在while (true)中,然后找出希望用户键入的内容才能退出,并使用break关键字(如果他们键入该内容)

您可以使用continue语句继续进行下一个迭代。

就是说,我没有在示例代码中看到循环。您可以使用for,while或do/while循环。 do / while循环至少执行一次-这通常是在询问用户问题时要执行的操作。

本Java分支语句教程在for循环中提供了continue语句的示例。

for (int i = 0; i < max; i++) {

// interested only in p's

if (searchMe.charAt(i) != 'p')

continue;

// process p's

numPs++;

}

例如,将continue;与无条件循环一起使用

while(true){/* your code*/}

不太详细,更直接。我已经解释了简单的答案。

for(int i = 0; i <= 10; i++){

if(i == 4){

continue;

}

//Should skip 4 and print out 1,2,3,5,6,7,8,9,10

System.out.println(i);

}

我的CS课也遇到了同样的问题:这就是我所做的。首先,您需要使用循环。我使用了一个" while"循环。

因此,假设您在对话中的某个时刻,有人要问别人(例如武术大师问玩家),而答案选择是" y"或" n"。但是玩家输入" m"。这将产生一个错误。要解决此问题,您需要进行一些操作(循环),以继续检查用户是否键入了有效的响应,并相应地继续执行程序。现在的代码...

//Ask if they want to see the rules.

System.out.println("Welcome Player One");

System.out.println("Would you like to read the rules of the gem?");

System.out.println("[y]Yes, please / [n]I know the rules."); //So you made it clear that the choices are 'y' or 'n' (yes or no).

//Decide if they hit yes or no.

char Response; //Variable to hold user response.

Scanner Console = new Scanner(System.in); //I don't know why, exactly, but you need this.

Response = Console.next().charAt(0); //Set response to the user input. A input field will appear.

while(true){  //Making the parameter 'true' makes the flow of the loop depend on the parameters in the if statements (I think).

if(Response == 'y' || Response == 'Y'){

System.out.println("Here are the rules:");

break; //break will make the flow exit the while loop (so you can continue adding the rest of your code).

}

else if(Response == 'n' || Response == 'N'){

System.out.println("Then let us continue:");

break;

}

else{

System.out.println("Would you like to read the rules of the gem?"

System.out.println("You have to enter (y)yes or (n)no...");

Response = Console.next().charAt(0); //Just like before, input field will  appear to give them the chance to change their response to a valid one.

continue; //This has to be 'continue' so that the loop will continue to"check" for the value that needs to be input by the user.

}

}

希望这会有所帮助,欢呼。

您的问题做出了错误的假设。 switch()语句不是循环。这是一堆if / else-if语句的简写:

if(sc1.equals("Eat coconut")) {

System.out.println("You ate the coconut and got poisoned");

System.out.println("You died...");

}

else if(sc1.equals("Throw coconut")) {

//and so on

若要执行所需的操作,您将需要一个实际的while()或for()循环。 (这些链接将带您进入解释这两种构造的Java教程。)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值