我是论坛的新手(作为注册用户),所以我非常努力(我发誓!)不发表问题,寻找旧答案,并检查了其他人在问题上的错误与我的相似,但我无能为力。
我的代码,应该检查一个单词是否是另一个的字谜。我很确定我的生活很复杂,并且会有更简单的方法,但是......我已经做了一段时间了,我希望看到它有用......
知道它为什么没有?
我所看到的只是空字典,当它们的字母数相同时,这两个字总是字谜(这意味着我的字典实际上并没有做任何事情:'()
import acm.program.ConsoleProgram;
import java.util.*;
public class Anagrams extends ConsoleProgram {
String firstWord;
String secondWord;
public boolean checkLength(String firstWord, String secondWord) {
if (firstWord.length() == secondWord.length()) {
println("Same length!");
return true;
} else {
return false;
}
}
public boolean anagram(String firstWord, String secondWord) {
firstWord = firstWord.toLowerCase();
secondWord = secondWord.toLowerCase();
String[] firstArray = firstWord.split("\\a");
String[] secondArray = secondWord.split("\\a");
int firstLength = firstWord.length();
int secondLength = secondWord.length();
Map<String, Integer> firstDictionary = new HashMap<String, Integer>();
Map<String, Integer> secondDictionary = new HashMap<String, Integer>();
for (firstLength = 0; firstLength == firstArray.length; firstLength++) {
System.out.println("checking the letter " + firstArray[firstLength] + " in array" + firstArray.toString());
if (firstDictionary.get(firstArray[firstLength]) == null) {
firstDictionary.put(firstArray[firstLength], 1);
} else {
firstDictionary.put(firstArray[firstLength], firstDictionary.get(firstArray[firstLength]) + 1);
}
}
for (secondLength = 0; secondLength == secondArray.length; secondLength++) {
if (secondDictionary.get(secondArray[secondLength]) == null) {
secondDictionary.put(secondArray[secondLength], 1);
} else {
secondDictionary.put(secondArray[secondLength], secondDictionary.get(secondArray[secondLength]) + 1);
}
}
if (firstDictionary.equals(secondDictionary)) {
return true;
} else {
return false;
}
}
public void run() {
int runAgain = 0;
while (runAgain == 0) {
println("Enter the first word to be analyzed");
firstWord = readLine();
println("Enter the second word to be analyzed");
secondWord = readLine();
if (checkLength(firstWord, secondWord) == true) {
if (anagram(firstWord, secondWord) == true) {
println("Yes! The two words are anagrams!");
}
} else {
println("No. The two words are not anagrams!");
}
}
}
}
答案 0 :(得分:1)
您的循环条件有错误
for (firstLength=0;firstLength==firstArray.length;firstLength++)
这应该是
for (firstLength=0;firstLength<firstArray.length;firstLength++)
因为条件firstLength==firstArray.length
为false,所以您的程序永远不会进入循环。
我相信:
String[] firstArray = firstWord.split("\\a");
String[] secondArray = secondWord.split("\\a");
错了,你只需要char数组,所以改用它:
char[] first = firstWord.toCharArray();
char[] second = secondWord.toCharArray();
工作版:
public boolean anagram(String firstWord, String secondWord) {
firstWord = firstWord.toLowerCase();
secondWord = secondWord.toLowerCase();
char[] firstArray = firstWord.toCharArray();
char[] secondArray = secondWord.toCharArray();
int firstLength = firstWord.length();
int secondLength = secondWord.length();
Map<Character, Integer> firstDictionary = new HashMap<>();
Map<Character, Integer> secondDictionary = new HashMap<>();
for (firstLength = 0; firstLength < firstArray.length; firstLength++) {
// System.out.println("checking the letter " + firstArray[firstLength] + " in array" + firstArray.toString());
if (!firstDictionary.containsKey(firstArray[firstLength])) {
firstDictionary.put(firstArray[firstLength], 1);
} else {
firstDictionary.put(firstArray[firstLength], firstDictionary.get(firstArray[firstLength]) + 1);
}
}
for (secondLength = 0; secondLength < secondArray.length; secondLength++) {
if (!secondDictionary.containsKey(secondArray[secondLength])) {
secondDictionary.put(secondArray[secondLength], 1);
} else {
secondDictionary.put(secondArray[secondLength], secondDictionary.get(secondArray[secondLength]) + 1);
}
}
if (firstDictionary.equals(secondDictionary)) {
return true;
} else {
return false;
}
}
答案 1 :(得分:0)
我会采用不同的方法:
String firstWord = "abcde";
String secondWord = "edcba";
String[] arr1 = firstWord.split("(?!^)"); // split each string to an array of (String) characters
Arrays.sort(arr1); // and sort it alphabetically
String[] arr2 = secondWord.split("(?!^)");
Arrays.sort(arr2);
// now we can compare:
System.out.println(Arrays.equals(arr1, arr2)); // prints 'true'