2

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



}

}

}

ユーザーが必要以外の何かを入力すると、デフォルトのケースが発生しますが、ループの最初に戻る必要があるため、ユーザーは他のケースのいずれかを入力できます。

4

5 に答える 5

7

このステートメントを使用しcontinueて、次の反復に進むことができます。

とはいえ、サンプル コードにループは見当たりません。forwhileまたはでループできますdo/whiledo/whileループは少なくとも 1 回実行されます。これは通常、ユーザーに質問するときに実行したいことです。

分岐ステートメントに関するこのJava チュートリアルでは、ループ内のcontinueステートメントの例を示します。for

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

        // process p's
        numPs++;
    }
于 2013-07-15T18:08:33.573 に答える
3

continue;たとえば、無条件ループで使用します

while(true){/* your code*/}
于 2013-07-15T18:08:48.027 に答える
0

あなたの質問は間違った仮定をしています。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 チュートリアルに移動します。)

于 2013-07-15T18:10:17.753 に答える
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.  
  }  
}

これが役立つことを願っています、乾杯。

于 2015-09-02T21:33:22.160 に答える