Gibt es einen Befehl in java, um das Programm zurück zum Anfang der Schleife

Ich versuche, eine Eingabe-adventure-Spiel in java, aber ich brauche einen Befehl, mindestens ähnlich wie in dem Titel, hier ist der 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");



}

}

}

Immer, wenn der Benutzer etwas anderes als erforderlich, den Standard-Fall passiert, aber ich brauche es zu gehen zurück an den Anfang der Schleife, so kann der Benutzer geben Sie einen der anderen Fälle.

  • Wo ist die Schleife in deinem code? switch ist keine Schleife.
  • Wickeln Sie Ihren code in einer while (true) dann, herauszufinden, was Sie möchten, dass der Benutzer geben, um in der Lage zu beenden, und verwenden Sie die break Schlüsselwort, wenn Sie der Typ, der
InformationsquelleAutor user2581790 | 2013-07-15
Schreibe einen Kommentar