1

I'm having a problem. I'm currently doing a school project for a ChatBot. This is a game section for the chatbot. It's a word scramble game. I can enter the wrong answer and the program would prompt me to retype the answer. But, if I type the correct answer, the program just stops there, the program didn't stop.

What I would like to do is to repeat the code, like keeping playing the game, until I type the word "end", but I don't know how to implement it. Also, most of the words kept repeating. For example, the word [animal] would usually be scrambled into [anialm].

So I would like to get some help on improving the code too. Thank you very much!! I am still a Year 1 who just started learning to program.

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"));
    }
}
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
UD DOE
  • 19
  • 3

3 Answers3

1

I think that the first thing to do is to look to restructure your code, this will make it easier to debug.

I would say that your code is basically asking to be separated into two man parts: one to run the game and one to contain the data:

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

}

Then we will have much less to worry about and our running loop basically becomes like:

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

This uses while(true) which could easily be replaced with some other kind of loop.

Additionally you would probably benefit by extracting your set up code to another method.

MartinByers
  • 1,240
  • 1
  • 9
  • 15
0

Use break; statement to end loop.

if(guess.equalsIgnoreCase("end")){
     break;
  }
Chetan Joshi
  • 5,582
  • 4
  • 30
  • 43
  • You should also mention that the outer loop should be removed with this (and that this code fragment belongs into the inner loop, and where). – Njol Jul 07 '17 at 13:51
-1
if(guess.equalsIgnoreCase("end")){
      System.exit(0);
  }
Mohit Tyagi
  • 2,788
  • 4
  • 17
  • 29