-1

So I'm supposed to make a "scramble" game that asks the user for a word, then the program scrambles that word for the second user to guess.

import java.util.*;

class scrabble {
public static void main(String args[]){
    Scanner input = new Scanner(System.in);

    System.out.println("Enter a word: ");
    String abc = input.nextLine();

    String scrambled = "";
    Random randGen = new Random();

    while(scrambled.length() != abc.length())
    {
       char rand = abc.charAt(randGen.nextInt(abc.length()));
       if(!scrambled.contains("" + rand))
          scrambled += rand;
    }

    System.out.println(scrambled);


}
}

Output: Enter a word: computer emotucrp

But if I enter the word "continue", the program goes into an infinite loop.

name123
  • 773
  • 3
  • 9
  • 9

1 Answers1

1

The word continue has two ns in it, so your program never adds the second n to the scrambled string and therefore never stops. This question here has various code samples to scramble words like you are trying to do.

Community
  • 1
  • 1
TheSuccessor
  • 124
  • 7