java - Java - 单词加扰器[保留第一个和最后一个字母]
问题描述
我的任务是编写一个程序来打乱一个单词,同时保持相同的第一个和最后一个字母并且只交换两个字母,然后提示用户如果他们愿意继续。
示例:userInput = bacon
|Output = bcaon
我附上了我的程序的想象,可能有几个问题,但由于图像中的错误,我无法真正运行它。我真的很困惑,因为我有一个助教来帮助我完成这项任务,他们似乎认为这肯定会奏效,但正如你所见,它没有。
如果有人能告诉我到底出了什么问题以及为什么,我将不胜感激。如果您要添加任何内容来使该程序正常运行,我也非常非常感谢您,但最重要的是,我只想了解问题所在以及原因。
import java.util.Scanner;
import java.util.Random;
public class FreeStyle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in); // Importing and initializing keyboard to 'in'
System.out.println("Please enter a word to be scrambled"); // Asking user for a word
String word = in.next(); // Initializing the user's input
System.out.println(swapLetters(word));
System.out.println("Would you like to enter another word? y/n");
String answer = in.next();
boolean userDone = true; //Using a Boolean statement to ask the user if they are done enter words or not
while (userDone) {
if (answer.equals('y')) {
System.out.println("Please enter a new word"); //Ask user for new word to scramble
word = in.nextLine(); //New initialization for 'word'
} else if (answer.equals('n')) { //If user types 'n', loops then breaks because while(userDone) is false
userDone = false;
} else {
System.out.println("Invalid input, please enter more than 3 letter words."); // The logic doesn't flow or apply to words that are less than 4 letters, so this catches that error and notifies the user
}
}
}
private static String swapLetters(String word) { //Private method used for the program only, no need to involve the user
Random r = new Random(); //Using random instead of math floor
//int arraysize = word.length();
int a = r.nextInt(word.length()-2)+1;
int b = r.nextInt(word.length()-2)+1;
//String word2 = word.substring(a, a+1);
String word2 = word.substring(0, a) + word.charAt(b)+word.substring(a+1, b)+word.charAt(a)+word.substring(b+1);
return word2;
}
解决方案
几点:
- 为什么不使用已经完成的东西来完成您正在尝试做的事情 - Collections.shuffle?请参阅代码中的注释以了解其工作原理。
- 您不能
equals()
在 String 和 char 之间使用(' ' 用于字符," " 用于字符串)。简单的修复 - 只需将您的“y”放入“y”,对于“n”也是如此。 - 我在开始时重构了我们用来获取用户输入的代码,然后将其转换为一个单独的方法,因此我们可以再次重用它 -
getInputAndScramble
。 - 最后,我使用了一个 do-while 循环来保持循环,直到用户用“n”字母停止循环。
请在代码中查看我的评论,希望能解决问题。
public class Scrambler {
public static void main(String[] args) {
boolean userDone = true;
String word;
Scanner in = new Scanner(System.in);
getInputAndScramble(in); //Extracted method to get Scanner input and scramble
do {
System.out.println("Would you like to enter another word? y/n");
word = in.next();
while (userDone) {
if (word.equals("y")) {
getInputAndScramble(in);
break;
} else if (word.equals("n")) {
userDone = false;
} else {
System.out.println("Invalid input, please enter more than 3 letter words.");
}
}
} while (userDone); //continue until "n"
}
private static void getInputAndScramble(Scanner in) {
System.out.println("Please enter a word to be scrambled");
String word = in.next();
System.out.println(swapLetters(word));
}
private static String swapLetters(String word) {
/* Convert word into an ArrayList of characters.
Create ArrayList size of word,
convert String word into a char array and insert every char in
the char array into our ArrayList.
*/
ArrayList<Character> chars = new ArrayList<>(word.length());
for (char c : word.toCharArray()) {
chars.add(c);
}
//Shuffle, omitting first and last letters
Collections.shuffle(chars.subList(1, chars.size()-1));
//Add shuffled letters into an array to output as a word entity
char[] shuffled = new char[chars.size()];
for (int i = 0; i < shuffled.length; i++) {
shuffled[i] = chars.get(i);
}
//Return shuffled String
return new String(shuffled);
}
}