我有一個任務,我們被指示創建一個方法,該方法接受一個字串,對字串的內容進行加擾,然后回傳加擾的字串(即“Hello”回傳“elloH”)。但是,我們只能通過回圈和基本的字串函式來做到這一點(不能使用陣列)。
我的老師給我們留下了以下建議:
最好的 shuffle 方法是生成 2 個亂數(索引號)并根據隨機索引號交換數字(內容,而不是索引)。繼續做 100 次,它們被洗牌,所有字符保持不變,但位置不同
這將如何完成?作為參考,這是我對解決方案的嘗試,但是它不起作用,我不確定從這里去哪里:
public void shuffle(String s1) {
int i1 = 0;
int i2 = 0;
String s2 = "";
for(int a2 = 0; a2 < s1.length(); a2 ) {
char c1 = s1.charAt(i1);
s2 = c1;
for(int a1 = 0; a1 < 100; a1 ) {
double d1 = Math.random();
d1 *= (s1.length()-1);
i1 = (int) d1;
}
}
System.out.println(s2);
}
uj5u.com熱心網友回復:
您的代碼的問題是您沒有交換字串中的字符,而是在字串中隨機選擇,這會導致某些字符被多次使用,而有些則根本不使用。
只要它本身是不可變的(從它呼叫任何方法都不會改變前一個物件,而是回傳一個新物件),我會使用它來做這樣的事情StringBuilder,它適用于操作。StringString
// it's a good practice to delcare formal parameters in a method as final
public void shuffle(final String string) {
// store the string length to a variable for sake of comfort
final int length = string.length();
// use mutable StringBuilder for easy String manipulations
final StringBuilder stringBuilder = new StringBuilder(string);
// initialize the Random object generator once and use many times
// as you don't want to initialize with each loop and throw it away
final Random random = new Random();
// define the number of iterations
// to repeat as many times as the string is long is more than enough
for (int i=0; i<length; i ) {
// pick two random indices to be swapped
final int firstIndex = random.nextInt(length);
final int secondIndex = random.nextInt(length);
// remember the swapped characters
// otherwise it would end up in a mess and duplicated characters
final char firstChar = stringBuilder.charAt(firstIndex);
final char secondChar = stringBuilder.charAt(secondIndex);
// perform the swap: basically set the characters to their positions
stringBuilder.setCharAt(firstIndex, secondChar);
stringBuilder.setCharAt(secondIndex, firstChar);
}
// and see the result
System.out.println(stringBuilder);
}
運行 3 次(看起來很好交換,字符保持不變):
WoodeHl rl!l Wer lldooHl! HW! llrloeod
uj5u.com熱心網友回復:
您好,這是符合您要求的更新代碼
如果您想查看所有 100 次迭代,還可以使用 Math.random() 生成亂數,只需在 for 回圈中列印 s1 即可。
static String swap(String str, int i, int j) {
// if both indexes are the same change nothing
if (i == j)
return str;
// if the second index is last then there will be no substring from j 1 to last;
if (j == str.length() - 1)
return str.substring(0, i) str.charAt(j) str.substring(1 i, j) str.charAt(i);
return str.substring(0, i) str.charAt(j) str.substring(1 i, j) str.charAt(i)
str.substring(j 1, str.length());
}
public static void shuffle(String s1) {
// iterate the same logic 100 times
for (int a1 = 0; a1 < 100; a1 ) {
// generate 2 random numbers in the range of the length of string
int randomNumber1 = (int) (Math.random() * s1.length());
int randomNumber2 = (int) (Math.random() * s1.length());
// setting the lower random number to randomnumber1 (it is swapping two number without temp)
if (randomNumber1 > randomNumber2) {
randomNumber1 = randomNumber1 randomNumber2;
randomNumber2 = randomNumber1 - randomNumber2;
randomNumber1 -= randomNumber2;
}
// calling the swap method to swap the chars in string.
s1 = swap(s1, randomNumber1, randomNumber2);
}
System.out.println(s1);
}
uj5u.com熱心網友回復:
由于您不能使用陣列,請嘗試使用 StringBuilder。
private String shuffleUtil(String s) {
final StringBuilder result = new StringBuilder(s);
for (int rep = 1; rep <= 100; rep ) {
final int randomOne = ThreadLocalRandom.current().nextInt(0, s.length());
final int randomTwo = ThreadLocalRandom.current().nextInt(0, s.length());
final char c = result.charAt(randomOne);
result.setCharAt(randomOne, result.charAt(randomTwo));
result.setCharAt(randomTwo, c);
}
return result.toString();
}
public void shuffle(String s) {
final String result = shuffleUtil(s);
System.out.println(s);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/512316.html
標籤:爪哇细绳随机的
