我该如何改变一个单词的字母?

时间:2013-12-14 22:00:20

标签: java arrays random

对数组中某个单词的字母进行随机播放的最简单方法是什么?我在数组中有一些单词,我随机选择一个单词,但我也想把它的字母随机化。

public static void main (String[] args ) {
    String [] animals = { "Dog" , "Cat" , "Dino" } ;    
    Random random = new Random();
    String word = animals [random.nextInt(animals.length)];

    System.out.println ( word ) ;
    //I Simply want to shuffle the letters of word     
}

我不应该使用那个List的东西。我想出了类似的东西,但是用这个代码打印随机字母,它不会随机播放。如果那封信已经打印出来,也许我可以编写类似不打印的东西?

//GET RANDOM LETTER
for (int i = 0; i< word.length(); i++ ) {

char c = (word.charAt(random.nextInt(word.length())));
System.out.print(c); } 
  }

3 个答案:

答案 0 :(得分:7)

真的不需要收集以及以下内容:

public static void main(String[] args) {

    // Create a random object
    Random r = new Random();

    String word = "Animals";

    System.out.println("Before: " + word );
    word = scramble( r, word );
    System.out.println("After : " + word );
}

public static String scramble( Random random, String inputString )
{
    // Convert your string into a simple char array:
    char a[] = inputString.toCharArray();

    // Scramble the letters using the standard Fisher-Yates shuffle, 
    for( int i=0 ; i<a.length ; i++ )
    {
        int j = random.nextInt(a.length);
        // Swap letters
        char temp = a[i]; a[i] = a[j];  a[j] = temp;
    }       

    return new String( a );
}

答案 1 :(得分:3)

您可以使用Collections.shuffle

List<Character> l = new ArrayList<>();
for(char c :  word.toCharArray()) //for each char of the word selectionned, put it in a list
    l.add(c); 
Collections.shuffle(l); //shuffle the list

StringBuilder sb = new StringBuilder(); //now rebuild the word
for(char c : l)
  sb.append(c);

word = sb.toString();

  

我不应该使用那个List事物。

然后您可以创建两个StringBuilder个对象。一个人将保留原始单词,一个人将创建一个洗牌的单词:

StringBuilder s = new StringBuilder(word);
StringBuilder wordShuffled = new StringBuilder();
while(s.length() != 0){
    int index = random.nextInt(s.length());
    char c = s.charAt(index);
    wordShuffled.append(c);
    s.deleteCharAt(index);
}
System.out.println(wordShuffled.toString());

  

我找到了类似deleteCharAt的东西,但我想它可以使用   StringBuilder什么的。我不能用那个

Here你可以找到一些很好的实用程序方法,允许对数组进行混洗。

public static char[] shuffleArray(char[] x) {    
       for ( int i = x.length; i > 0; i-- ) {
            int rand = (int)(Math.random()*(i));
            char temp = x[i-1];
            x[i-1] = x[rand];
            x[rand] = temp;
        }
       return x;
}

然后只需调用此方法并使用构造函数String(char[] value)

System.out.println(new String(shuffleArray(word.toCharArray())));

下次明确说明你可以使用/不使用的东西。

答案 2 :(得分:0)

这样的事情怎么样?

// Shuffle an array of characters.
public static void shuffleArray(char[] a) {
  int n = a.length; // the length of the array.
  for (int i = 0; i < n; i++) {
    int t = random.nextInt(n); // pick a random number 0 - the length.
    if (t == i) {              // if the random number is the loop counter
      if (i > 0) {             // check if we're at the first element.
        t = random.nextInt(i); // pick another number between 0 - and the loop counter.
      } else {
        t = a.length - 1;      // the end of the loop.
      }
    }
    a[i] ^= a[t];              // swap a[i] and a[t]
    a[t] ^= a[i];
    a[i] ^= a[t];
  }
}

private static Random random = new Random(); // the shared random.

public static void main(String[] args) {
  String[] animals = { "Dog", "Cat", "Dino" };
  String word = animals[random
      .nextInt(animals.length)];

  System.out.println(word);            // the random word.
  char[] arr = word.toCharArray();     // the char[] from the word.
  shuffleArray(arr);                   // shuffle it.
  System.out.println(new String(arr)); // print it.
}