在eclipse中多次加扰字符串

时间:2014-04-22 19:38:31

标签: java eclipse

我在写一些程序的代码时遇到了一些麻烦。这个程序的目的是从一个单独的文本文件中取一个单词,将其加扰十次,然后显示该单词的加扰字母。我遇到的问题是,我不确定如何将信件拼写十次。我知道实际的加扰是在我的混音器方法中进行的,但是我怎么想。我想过使用for循环,但我不确定如何去做。

import java.io.*;
import java.util.*;

public class Scrambler {

public static void main(String[] args) throws FileNotFoundException {
    Scanner input = new Scanner(new File("words.txt"));
    String text = input.next();
    System.out.println("Original Word: " + text);
    System.out.println();
    System.out.println("Scrambled Word:");
    System.out.println("********");
    separate(text);
    System.out.println("********");

}

public static void separate(String text) {
    System.out
            .println("  " + text.charAt(0) + "  " + text.charAt(1) + "  ");
    System.out.println(text.charAt(2) + "      " + text.charAt(3));
    System.out
            .println("  " + text.charAt(4) + "  " + text.charAt(5) + "  ");
}

public static String mixer(String text) {
    Random r = new Random();
    int r1 = r.nextInt(text.length());
    int r2 = r.nextInt(text.length());

    String a = text.substring(0, r1);
    char b = text.charAt(r1);
    String c = text.substring(r1 + 1, r2);
    char d = text.charAt(r2);
    String e = text.substring(r2 + 1, text.length());

    text = a + b + c + d + e;

    return text;
}

}

4 个答案:

答案 0 :(得分:2)

您的调音台()无法正常工作。我首先将字符串变为char [],然后检索2个随机索引并切换这些索引中的字符。

char[] stringasarray = text.toCharArray();
int length = text.length;

for(int i=0; i<length; i++){
    int letter1 = rnd.nextInt(length);
    int letter2 = rnd.nextInt(length);

    char temp = stringasarray[letter1];
    stringasarray[letter1] = stringasarray[letter2];
    stringasarray[letter2] = temp;
}
String newtext = new String(stringasarray);

答案 1 :(得分:1)

一个简单的for循环可以做到:

String word = "Hello World";

for(int i = 0; i < 10; i++){
    word = mixer(word);
}

答案 2 :(得分:1)

这是一种将字符串加扰十次的方法;

// Passing in the Random.
public static String mixer(String in, Random rnd) {
    StringBuilder sb = new StringBuilder();
    if (in != null) { // <-- check for null.
        List<Character> chars = new ArrayList<Character>();
        for (char ch : in.toCharArray()) {
            chars.add(ch); // <-- add each character to the List.
        }
        Collections.shuffle(chars, rnd); // <-- "scramble"
        for (char ch : chars) {
            sb.append(ch);
        }
    }
    return sb.toString();
}

public static void main(String[] args) {
    String t = "Hello";
    Random rnd = new Random();
    // I'm not sure why you want to do it 10 times, but here is one way.
    for (int i =0; i < 10; i++) {
        t = mixer(t, rnd); // <-- call mixer function.
    }
    System.out.println(t);
}

答案 3 :(得分:0)

public static String mixer(String text) {
    Random r = new Random();
    int r1 = r.nextInt(text.length());  // generates a random number from 0 to text.length - 1
    int r2 = r.nextInt(text.length());   //generates a random number from 0 to text.length - 1

    String a = text.substring(0, r1); // creates a substring containing characters from 0 to r1
    char b = text.charAt(r1);  //grabs the character at r1
    String c = text.substring(r1 + 1, r2); // creates a substring from r1+1 to r2
    char d = text.charAt(r2);  // grabs the character at r2
    String e = text.substring(r2 + 1, text.length()); // grabs any remaining characters

    text = a + b + c + d + e;  // recombines them

    return text;
}

没有深入研究子字符串的工作方式,这很可能会返回完全相同的字符串。如果你改变了a + b + c + d + e的顺序,它会加扰它。它接受了这个词并将它分成五个部分,然后重新组装它。

然而,它可能会使用大量错误检查,并进行一些验证。