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

什么是最简单的方法来改变数组中单词的字母? 我在数组中有一些单词,我随机选择一个单词,但我也想把它的字母洗牌。

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 } 

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

 //GET RANDOM LETTER for (int i = 0; i< word.length(); i++ ) { char c = (word.charAt(random.nextInt(word.length()))); System.out.print(c); } } 

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

 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 

您可以使用Collections.shuffle

 List 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(); 

我不应该使用那个列表的东西。

然后,您可以创建两个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或其他东西。 我不能用它

在这里你可以找到一些很好的实用程序方法,允许洗牌一个数组。

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

下次明确说明,你可以使用/不使用什么。

这样的事怎么样?

 // 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. }