Java 如果三元组中的字母重复,如何用字母表中的随机字符替换三元组中的随机字母?
如果三元组中的字母重复,如何用字母表中的随机字符替换三元组中的随机字母?像这样Java 如果三元组中的字母重复,如何用字母表中的随机字符替换三元组中的随机字母?,java,Java,如果三元组中的字母重复,如何用字母表中的随机字符替换三元组中的随机字母?像这样iimmpppooorrtttaannnttt-->i1imqmoppoottorruttajannqtt。在我的代码中,我将所有字母替换为三元组 import java.util.LinkedList; import java.util.Random; import java.util.Scanner; public class Main { public static void main(String[]
iimmpppooorrtttaannnttt-->i1imqmoppoottorruttajannq
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);
}
}
import java.util.LinkedList;
导入java.util.Random;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args){
字符[]字母表={'','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’、};
扫描仪扫描=新扫描仪(System.in);
String str=scan.nextLine();
LinkedList=新建LinkedList();
系统输出打印项次(str);
StringBuilder full1=新的StringBuilder();
对于(int i=0;i
您应该能够通过使用字符数组执行类似操作:
String input = "ttteeesss";
Character[] arr = input.toCharArray();
arr[randomNumberInTriplet] = alphabet[randomAlphabet];
String ans = new String(arr);
如果替换该字符,它将替换字符串中的所有字符 您应该能够通过使用字符数组执行类似操作:
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);
}
如果替换该字符,它将替换字符串中的所有字符
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);
}
像这样:
text0 += s + s + s;
将循环转换为流:
list.stream().map(String::toCharArray).forEach(c -> {
c[random.nextInt(c.length)] = alphabet[random.nextInt(alphabet.length)];
full.append(c);
});
您没有在任何地方使用
full1
。将上面的循环转换为流,如下所示:
str.chars().mapToObj(c -> String.valueOf(c + c + c)).forEach(list::add);
代码越少,潜伏bug的地方就越少
像这样:
text0 += s + s + s;
将循环转换为流:
list.stream().map(String::toCharArray).forEach(c -> {
c[random.nextInt(c.length)] = alphabet[random.nextInt(alphabet.length)];
full.append(c);
});
您没有在任何地方使用
full1
。将上面的循环转换为流,如下所示:
str.chars().mapToObj(c -> String.valueOf(c + c + c)).forEach(list::add);
代码越少,潜伏bug的地方就越少。尝试使用
StringBuilder
。它有在特定位置替换字符的方便方法,所以您不需要处理字符数组。(我重命名了一些变量以使其更清晰)
Random rand=new Random();
StringBuilder替换器;
弦乐三重奏;
对于(int i=0;i
尝试使用StringBuilder
。它有在特定位置替换字符的方便方法,所以您不需要处理字符数组。(我重命名了一些变量以使其更清晰)
Random rand=new Random();
StringBuilder替换器;
弦乐三重奏;
对于(int i=0;i
与其使用LinkedList、StringBuilder和String类中的各种方法使事情变得复杂,不如考虑一下如何使用笔和纸来完成。
如果我是你,我会选择以下方法:
- 在每三个字符处拆分原始字符串(使用正则表达式、子字符串..)
- 如果字符重复(使用正则表达式、循环…),请检查每个子字符串(三元组)
- 如果是,则用随机字符替换随机索引中的字符,否则不执行任何操作
- 将子字符串连接到一个结果字符串
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);
}
}
公共类示例{
静态字符[]字母表={'','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’、};
公共静态void main(字符串[]args){
扫描仪扫描=新扫描仪(System.in);
String original=scan.nextLine();
String[]splited=original.split((?)与其使用LinkedList、StringBuilder和String类中的各种方法使事情复杂化,不如考虑一下如何使用笔和纸来完成。
如果我是你,我会选择以下方法:
- 在每三个字符处拆分原始字符串(使用正则表达式、子字符串..)
- 如果字符重复(使用正则表达式、循环…),请检查每个子字符串(三元组)
- 如果是,则用随机字符替换随机索引中的字符,否则不执行任何操作
- 将子字符串连接到一个结果字符串
例如:
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);
}
}
公共类示例{
静态字符[]字母表={'','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”,