[英]How to replace a random letter in a triple with a random character from the alphabet, if the letters in the triple are repeated?
如果三重奏中的字母重復,如何用字母表中的隨機字符替換三重奏中的隨機字母? 像這樣IIImmmpppooorrrtttaaannnttt ----> I1ImQmOppooT0rruttaJannQ
tt。 在我的代碼中,我將所有字母替換為三元組。
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
LinkedList<String> list = new LinkedList<>();
System.out.println(str);
StringBuilder full1 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String text0 = "";
char s = str.charAt(i);
//String s = str.substring(i, i + 3);
text0 += s;
text0 += s;
text0 += s;
list.add(text0);
full1.append(text0);
}
System.out.println(full1);
StringBuilder full = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
Random random = new Random();
String text = list.get(i);
int select = random.nextInt(text.length());
String text2 = text.replace(text.charAt(select), alphabet[random.nextInt(alphabet.length)]);
full.append(text2);
}
System.out.println(full);
}
}
您應該能夠通過使用字符數組來執行以下操作:
String input = "ttteeesss";
Character[] arr = input.toCharArray();
arr[randomNumberInTriplet] = alphabet[randomAlphabet];
String ans = new String(arr);
如果替換字符,它將替換字符串中的所有字符。
public static void main(String[] args) {
Random random = new Random();
char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
List<String> list = new LinkedList<>();
System.out.println(str);
StringBuilder full1 = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
String text0 = "";
char s = str.charAt(i);
text0 += s;
text0 += s;
text0 += s;
list.add(text0);
full1.append(text0);
}
System.out.println(full1);
System.out.println(list);
StringBuilder full = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
String text = list.get(i);
int select = random.nextInt(text.length());
char[] text2 = text.toCharArray();
text2[select] = alphabet[random.nextInt(alphabet.length)];
full.append(text2);
}
System.out.println(full);
}
你不能像這樣只replace
一個特定的索引。 您需要將其轉換為array
,在index
上replace
,然后將其作為string
存儲回來。
無需每次都創建一個new Random
object。 你可以重復使用。
寫這個:
text0 += s;
text0 += s;
text0 += s;
像這樣:
text0 += s + s + s;
將循環轉換為 stream:
list.stream().map(String::toCharArray).forEach(c -> {
c[random.nextInt(c.length)] = alphabet[random.nextInt(alphabet.length)];
full.append(c);
});
您沒有在任何地方使用full1
。 將上面的循環轉換為 stream,如下所示:
str.chars().mapToObj(c -> String.valueOf(c + c + c)).forEach(list::add);
代碼越少,bug 潛伏的地方就越少。
嘗試使用StringBuilder
。 它具有在特定 position 處替換字符的便捷方法,因此您無需處理 char arrays。 (我重命名了一些變量以使其更清晰)
Random rand = new Random();
StringBuilder replacer;
String triple;
for (int i = 0; i < list.size(); i++) {
triple = list.get(i);
int indexToReplace = rand.nextInt(triple.length());
replacer = new StringBuilder(triple);
replacer.setCharAt(indexToReplace, alphabet[rand.nextInt(alphabet.length)]);
full.append(replacer);
}
與其使用 LinkendList、StringBuilder 和 String class 中的各種方法使事情復雜化,不如想一想您將如何使用筆和紙來完成它。 如果我是你,我會選擇以下方法:
例子:
public class Example {
static char[] alphabet = {' ', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z',};
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String original = scan.nextLine();
String[] splited = original.split("(?<=\\G...)");
System.out.println(original);
String result = "";
for (String triple : splited) {
if(contains3RepeatedChars(triple)){
triple = replaceARandomIndexWithARandomChar(triple);
}
result += triple;
}
System.out.println(result);
}
static boolean contains3RepeatedChars(String str){
return str.matches("(.)\\1{2}");
}
static String replaceARandomIndexWithARandomChar(String str){
Random r = new Random();
int randIndex = r.nextInt(3);
char randChar = alphabet[r.nextInt(alphabet.length)];
while (randChar == str.charAt(0)) {
randChar = alphabet[r.nextInt(alphabet.length)];
}
char[] arr = str.toCharArray();
arr[randIndex] = randChar;
return new String(arr);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.