2013-07-15 65 views
0

我想在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"); 



} 

} 

} 

每當用戶類型別的東西比要求,默認情況下會發生,但我需要它返回到循環的開始,所以用戶可以鍵入的其他情形的。

+2

代碼中的循環在哪裏?開關不是循環的。 – kosa

+0

將你的代碼包裝在'while(true)'中,然後找出你希望用戶輸入的內容以便能夠退出,並使用'break'關鍵字 –

回答

5

您可以使用continue語句繼續下一次迭代。

這就是說,我沒有看到您的示例代碼中的循環。您可以循環使用for,whiledo/whiledo/while循環至少執行一次 - 這通常是在詢問用戶問題時想要執行的操作。

This​​3210在for循環中提供了continue語句的示例。

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

     // process p's 
     numPs++; 
    } 
3

使用continue;無條件循環例如

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

你的問題作出了不正確的假設。 A 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

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

希望這有助於,歡呼聲。

0

不那麼詳細,更直接點。我已經解釋了簡單的答案。

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