Word Scramble 不加扰单词
Word Scramble Not Scrambling Words
这个程序应该打乱输入字符串中的字母,但它只是重复输入。我不确定我做错了什么,问题似乎出在加扰器功能上。谢谢
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r = new Random();
while (in.hasNext()) {
String str = in.next();
System.out.println(str);
}
}
//shuffle letters in word besides first and last
public static String scrambler(String str, Random r) {
//basic char array for input strings
char[] a = str.toCharArray();
//scramble letters
for (int i = 0; i < a.length; i++) {
//shuffle letters in word besides first and last
int j = r.nextInt(a.length);
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return new String(a);
}
你不是在调用 Scrambler 方法,你应该写
public static void main( String[] args){
Scanner in = new Scanner(System.in);
Random r = new Random();
while(in.hasNext()){
String str = in.next();
System.out.println(scrambler(str,r));
}
这个程序应该打乱输入字符串中的字母,但它只是重复输入。我不确定我做错了什么,问题似乎出在加扰器功能上。谢谢
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Random r = new Random();
while (in.hasNext()) {
String str = in.next();
System.out.println(str);
}
}
//shuffle letters in word besides first and last
public static String scrambler(String str, Random r) {
//basic char array for input strings
char[] a = str.toCharArray();
//scramble letters
for (int i = 0; i < a.length; i++) {
//shuffle letters in word besides first and last
int j = r.nextInt(a.length);
char temp = a[i];
a[i] = a[j];
a[j] = temp;
}
return new String(a);
}
你不是在调用 Scrambler 方法,你应该写
public static void main( String[] args){
Scanner in = new Scanner(System.in);
Random r = new Random();
while(in.hasNext()){
String str = in.next();
System.out.println(scrambler(str,r));
}