import java.util.Scanner;
public class WordScrambler {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int chosenWord;
String[] words = {"hogwash","Rudolph","yule-log","Eggnog","CandyCane","Christmas","Fruitcake","gingerbread","Krampus","nutcracker"};
System.out.println("Pick a number between 1-10");
chosenWord=in.nextInt();
String a=words[chosenWord].substring(0,words[chosenWord].length()/2);
String b=words[chosenWord].substring(words[chosenWord].length()/2);
String c=b+a;
int x=(int)(Math.random()*(words[chosenWord].length()-1))+1;
String d=c.substring(0, words[chosenWord].length()-x);
String e=c.substring(words[chosenWord].length()-x);
String f=e+d;
System.out.println(f);
}
}
这是我到目前为止所拥有的。 我无法找到一种方法来争夺它。 现在输出是例如:Hogwash:shhogwa。 这是Scrambles唯一的一个词。
答案 0 :(得分:0)
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int chosenIndex;
String[] words = { "hogwash", "Rudolph", "yule-log", "Eggnog", "CandyCane", "Christmas", "Fruitcake",
"gingerbread", "Krampus", "nutcracker" };
System.out.println("Pick a number between 1-10");
chosenIndex = in.nextInt();
String chosenWord = words[chosenIndex - 1];
System.out.println(chosenWord);
StringBuilder scrambled = new StringBuilder();
List<Character> letters = new ArrayList<Character>();
for (char x : chosenWord.toCharArray()) {
letters.add(x);
}
while (letters.size() != 0) {
int random = (int) (Math.random() * letters.size());
scrambled.append(letters.remove(random));
}
System.out.println(scrambled.toString());
}
使用有意义的变量名,以便明确其所指的