简体   繁体   中英

Is there a command in java to make the program go back to the beginning of a loop

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.

You can use the continue statement to continue to the next iteration.

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.

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

use continue; with unconditional loop for example

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

Your question makes an incorrect assumption. A switch() statement is not a loop. It's shorthand for a bunch of if/else-if statements like this:

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

To do what you want, you will need an actual while() or for() loop. (The links will take you to Java tutorials explaining both constructions.)

I had this same problem for my CS class: Here's what I did. First off, you NEED to use a loop. I used a 'while' loop.

So say you are at a point in your dialogue where someone asks some one else (like the martial arts master asks the player) for something, and the answer choices are "y" or "n". But the player types in a "m". This would produce an error. To fix it, you need to make something (a loop) that will keep checking whether or not the user typed a valid response, and continue the program accordingly. Now the code...

//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.  
  }  
}

Hope this helps, cheers.

Less detailed and more straight to the point. I have over explained simple answers.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM