2017-07-07 16 views
0

問題が発生しています。私は現在、チャットボットのための学校プロジェクトをやっています。これはチャットボットのゲームセクションです。それは単語スクランブルゲームです。私は間違った答えを入力することができ、プログラムは答えを再入力するよう促します。しかし、私が正解を入力すると、プログラムはそこで停止し、プログラムは停止しませんでした。私は特定の単語を入力すると、私の場合 "終了"でプログラムを終了するにはどうすればいいですか?

私がしたいのは、単語 "end"を入力するまで、ゲームを続けるようなコードを繰り返すことですが、実装方法はわかりません。また、言葉のほとんどは繰り返していました。例えば、[動物]という言葉は、通常、[anialm]にスクランブルされます。

私はコードを改善するための助けも受けたいと思います。どうもありがとうございました!!私はまだプログラムを学ぶことを始めたばかりの1年生です。

public static void main(String[] args) { 
     Scanner userinput = new Scanner(System.in); 
     String guess = ""; 
     String scramble = ""; 
     String[] questions = {"animal", "duck", "dinosaur", "butterfly", "dog", "horse", "cow", "cat", "elephant", "crocodile", "alligator"}; 
     char charArr[] = null; 
     String wordToGuess = questions[new Random().nextInt(questions.length)];// questions[0] 

     for (int a = 0; a < wordToGuess.length(); a++) { 
      charArr = wordToGuess.toCharArray(); //0a 1n 2i 3m 4a 5l 6s 
      //generating a random number from the length of word to guess. Example: Animal, generated no = "1" 
      int rdm = new Random().nextInt(charArr.length); 
      //shuffling the words 
      char temp = charArr[a];  //charArr[0] is the letter "A" which is moved to temp 
      charArr[a] = charArr[rdm]; //A is then moved to charArr[rdm] which is index 1, so the word is currently "AAimals' 
      charArr[rdm] = temp;  //charArr[1] contains n which is then moved to temmp, which is 0, becoming nAimal 
     } 
     scramble = new String(charArr); 
     System.out.println("Your word is: " + scramble); 

     do { 
      while (!guess.equals(wordToGuess)) { 
       System.out.print(">>> "); 
       guess = userinput.nextLine(); 
       if (!guess.equals(wordToGuess)) { 
        System.out.print(">>> "); 
        System.out.println("Please try again!"); 
       } else { 
        System.out.println(">>> Great Job getting the answer!"); 
       } 
      } 
     }while (!guess.equalsIgnoreCase("end")); 
    } 
} 

答えて

-1

私は(それはまだ実行しているかどうか)は、ゲームの状態を表しbooleanを、定義することができお勧めします。上記のbooleanを条件として、whileループ内に希望のアクションを入れることができます。

while(isRunning) { 
    //actions repeated in here 
} 

ゲームが終了したら、booleanの状態を変更してください。

-1
if(guess.equalsIgnoreCase("end")){ 
     System.exit(0); 
    } 
0

break;を使用してループを終了します。

if(guess.equalsIgnoreCase("end")){ 
    break; 
    } 
+0

また、外側のループはこれで削除する必要があります(このコードフラグメントは内部ループに属しています)。 – Njol

1

まず、コードを再構成することをお勧めします。これにより、デバッグが容易になります。

私はあなたのコードは基本的に2つの男の部分に分離する求めていると言うでしょう:一つは、データを格納するためのゲームと1を実行するために:

public class Anagram { 

    private String original= null; 
    private String scrambled = null; 

    public Anagram(String originalWord) { 
     this.original = originalWord; 
     this.scrambled = scramble(originalWord); 
    } 

    private String scramble(String original) { 
     //scramble logic here 
     return null;//your scrambled String 
    } 

    public boolean guess(String guess) { 
     return original.equals(guess); 
    } 

} 

その後、我々は心配するあまりを持っていますし、私たちのランニングループは基本的にのように次のようになります。

Anagram anagram = new Anagram(wordToGuess); 

String input = null; 
while (true) { 
    System.out.print(">>> "); 
    input = userinput.nextLine(); 
    if (input.equalsIgnoreCase("end")) { 
     System.out.println("Unlucky, please come back again"); 
     System.exit(0); 
    } 

    if (anagram.guess(input)) { 
     System.out.println(">>> Great Job getting the answer!"); 
     System.exit(0); 
    } 
    else { 
     System.out.print(">>> "); 
     System.out.println("Incorrrect, please try again!"); 
    } 
} 

これは、簡単にループのいくつかの他の種類に置き換えることができwhile(true)を使用しています。

さらに、セットアップコードを別の方法に抽出すると効果があります。

関連する問題