如何生成随机字母表,以便用户输入的字符串中的每个字符都被随机字符替换。一旦选择了随机字母,就将其从随机化可能性中移除。以下是我到目前为止的情况:
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
/**
*
* @author Dan
*/
public class k {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence: ");
String msg = in.nextLine();
ArrayList<Character> al = new ArrayList<Character> ();
Random gen = new Random();
int num1, num2, num3, num4, num5,num6,num7,num8,num9,num10,num11,num12,num13,num14,num15,num16,num17,num18,num19,num20,num21,num22,num23,num24, num25, num26;
int digit1, digit2;
int length;
for (int i = 65; i <=90; i++){
al.add((char)i);
}
num1 = gen.nextInt(al.size());
length = String.valueOf(num1).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num1 = Integer.valueOf(result);
}
al.remove(num1);
num2 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num2 = Integer.valueOf(result);
}
al.remove(num2);
num3 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num3 = Integer.valueOf(result);
}
al.remove(num3);
num4 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num4 = Integer.valueOf(result);
}
al.remove(num4);
num5 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num5 = Integer.valueOf(result);
}
al.remove(num5);
num6 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num6 = Integer.valueOf(result);
}
al.remove(num6);
num7 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num7 = Integer.valueOf(result);
}
al.remove(num7);
num8 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num8 = Integer.valueOf(result);
}
al.remove(num8);
num9 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num9 = Integer.valueOf(result);
}
al.remove(num9);
num10 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num10 = Integer.valueOf(result);
}
al.remove(num10);
num11 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num11 = Integer.valueOf(result);
}
al.remove(num11);
num12 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num12 = Integer.valueOf(result);
}
al.remove(num12);
num13 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num13 = Integer.valueOf(result);
}
al.remove(num13);
num14 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num14 = Integer.valueOf(result);
}
al.remove(num14);
num15 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num15 = Integer.valueOf(result);
}
al.remove(num15);
num16 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num16 = Integer.valueOf(result);
}
al.remove(num16);
num17 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num17 = Integer.valueOf(result);
}
al.remove(num17);
num18 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num18 = Integer.valueOf(result);
}
al.remove(num18);
num19 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num19 = Integer.valueOf(result);
}
al.remove(num19);
num20 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num20 = Integer.valueOf(result);
}
al.remove(num20);
num21 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num21 = Integer.valueOf(result);
}
al.remove(num21);
num22 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num22 = Integer.valueOf(result);
}
al.remove(num22);
num23 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num23 = Integer.valueOf(result);
}
al.remove(num23);
num24 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num24 = Integer.valueOf(result);
}
al.remove(num24);
num25 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num25 = Integer.valueOf(result);
}
al.remove(num25);
num26 = gen.nextInt(al.size());
length = String.valueOf(num2).length();
if (length == 1){
System.out.print(al.get(num1));
}
else if (length == 2){
digit1 = (num1 % 10);
digit2 = num1 / 10;
String result = "" + digit1 + digit2;
num26 = Integer.valueOf(result);
}
al.remove(num26);
msg = msg.toLowerCase();
ArrayList<Character> bl = new ArrayList<Character> ();
for (int i = 0; i <=26; i++){
bl.add((char)i);
}
String newmsg = msg.replaceAll("a", String.valueOf((int) num1));
bl.remove(num1);
newmsg = newmsg.replaceAll("b", String.valueOf((int) num2));
bl.remove(num2);
newmsg = newmsg.replaceAll("c", String.valueOf((int) num3));
bl.remove(num3);
newmsg = newmsg.replaceAll("d", String.valueOf((int) num4));
bl.remove(num4);
newmsg = newmsg.replaceAll("e", String.valueOf((int) num5));
bl.remove(num5);
newmsg = newmsg.replaceAll("f", String.valueOf((int) num6));
bl.remove(num6);
newmsg = newmsg.replaceAll("g", String.valueOf((int) num7));
bl.remove(num7);
newmsg = newmsg.replaceAll("h", String.valueOf((int) num8));
bl.remove(num8);
newmsg = newmsg.replaceAll("i", String.valueOf((int) num9));
bl.remove(num9);
newmsg = newmsg.replaceAll("j", String.valueOf((int) num10));
bl.remove(num10);
newmsg = newmsg.replaceAll("k", String.valueOf((int) num11));
bl.remove(num11);
newmsg = newmsg.replaceAll("l", String.valueOf((int) num12));
bl.remove(num12);
newmsg = newmsg.replaceAll("m", String.valueOf((int) num13));
bl.remove(num13);
newmsg = newmsg.replaceAll("n", String.valueOf((int) num14));
bl.remove(num14);
newmsg = newmsg.replaceAll("o", String.valueOf((int) num15));
bl.remove(num15);
newmsg = newmsg.replaceAll("p", String.valueOf((int) num16));
bl.remove(num16);
newmsg = newmsg.replaceAll("q", String.valueOf((int) num17));
bl.remove(num17);
newmsg = newmsg.replaceAll("r", String.valueOf((int) num18));
bl.remove(num18);
newmsg = newmsg.replaceAll("s", String.valueOf((int) num19));
bl.remove(num19);
newmsg = newmsg.replaceAll("t", String.valueOf((int) num20));
bl.remove(num20);
newmsg = newmsg.replaceAll("u", String.valueOf((int) num21));
bl.remove(num21);
newmsg = newmsg.replaceAll("v", String.valueOf((int) num22));
bl.remove(num22);
newmsg = newmsg.replaceAll("w", String.valueOf((int) num23));
bl.remove(num23);
newmsg = newmsg.replaceAll("x", String.valueOf((int) num24));
bl.remove(num24);
newmsg = newmsg.replaceAll("y", String.valueOf((int) num25));
bl.remove(num25);
newmsg = newmsg.replaceAll("z", String.valueOf((int) num26));
bl.remove(num26);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
System.out.println(newmsg);
System.out.println();
System.out.println();
System.out.println();
System.out.println();
String newmsg1 = newmsg.replaceAll(String.valueOf((int) num1),"a" );
newmsg1= newmsg1.replaceAll(String.valueOf((int) num2),"b" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num3),"c" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num4),"d" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num5),"e" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num6),"f" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num7),"g" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num8),"h" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num9),"i" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num10),"j" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num11),"k" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num12),"l" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num13),"m" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num14),"n" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num15),"o" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num16),"p" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num17),"q" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num18),"r" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num19),"s" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num20),"t" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num21),"u" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num22),"v" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num23),"w" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num24),"x" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num25),"y" );
newmsg1 = newmsg1.replaceAll(String.valueOf((int) num26),"z" );
System.out.println(newmsg1);
}
}
How would I now go about changing replacing the string characters with those `from the` randomly generated alphabet.
import java.util.*;
class alphaRandom {
public static void main(String[] args) {
ArrayList<Character> al = new ArrayList<Character>();
Random gen = new Random();
int num;
for(int i = 65; i <= 90; i++) {
al.add((char)i);
}
while(al.size() > 0) {
num = gen.nextInt(al.size());
System.out.print(al.get(num));
al.remove(num);
}
}
}
答案 0 :(得分:0)
其中一种方法可能是
c
,k
,...,n
)的内容 - Collection.shuffle
在这里很有用。a->c
b->k
... z->n
然后
char[]
数组,其长度与要编码的字符串相同你似乎已经实现了前两点所描述的
ArrayList<Character> al = new ArrayList<Character> ();
//...
for (int i = 65; i <=90; i++){
al.add((char)i);
}
顺便说一下,你可以用更可读的方式重写你的循环,如:
for (char ch = 'a'; ch<='z'; ch++){
al.add(ch);
}
请记住避免使用幻数(更多信息:What is a magic number, and why is it bad?)