我是该论坛的新手(以注册用户身份),所以我非常努力(我发誓!),不要发布问题,而是寻找旧的答案,并检查了其他人在与我类似的问题中的错误,但是我不能没事。
我的代码在这里,应该检查一个词是否是另一个词的字谜。我很确定自己已经使我的生活变得很复杂,并且会有更简单的方法来完成这项工作,但是...我已经从事此工作了一段时间了,并且希望看到它的工作...
知道为什么不这样做吗?
我所看到的只是空字典,并且当两个单词具有相同的字母数时,这两个词始终是字谜(这意味着实际上我的字典没有做任何事情:'()
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!");
}
}
}
}
您的循环条件有误
for (firstLength=0;firstLength==firstArray.length;firstLength++)
这应该是
for (firstLength=0;firstLength<firstArray.length;firstLength++)
由于条件firstLength==firstArray.length
为假,因此您的程序将永远不会进入循环。
我相信:
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;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句