【问题标题】:Is there a command in java to make the program go back to the beginning of a loopjava中有没有让程序回到循环开头的命令
【发布时间】:2013-07-15 18:07:09
【问题描述】:

我正在尝试用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 关键字

标签: java loops command switch-statement


【解决方案1】:

您可以使用continue 语句继续下一次迭代。

也就是说,我在您的示例代码中没有看到循环。您可以使用forwhiledo/while 循环。 do/while 循环至少执行一次——这通常是您在向用户提问时想要执行的操作。

Java tutorial for Branching Statements 提供了for 循环中continue 语句的此示例。

   for (int i = 0; i < max; i++) {
        // interested only in p's
        if (searchMe.charAt(i) != 'p')
            continue;

        // process p's
        numPs++;
    }

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      您的问题做出了不正确的假设。 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 教程。)

      【讨论】:

        【解决方案4】:

        我在 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.  
          }  
        }
        

        希望这会有所帮助,干杯。

        【讨论】:

          【解决方案5】:

          不那么详细,更直截了当。我已经过度解释了简单的答案。

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-12-13
            • 1970-01-01
            • 2021-04-06
            • 1970-01-01
            • 2021-12-18
            • 2016-05-08
            • 1970-01-01
            相关资源
            最近更新 更多