对于我本学期要完成的计算机科学类(class)的作业,我需要构建一个 java 程序,该程序导入 .txt 单词文件,接受一个单词作为输入,对输入单词进行打乱,然后输出一个字谜列表(即长度与使用完全相同字母的打乱单词相同的单词)进行打乱。程序还需要能够循环,以便用户可以在收到输出后输入新单词并重复程序或输入特定字符来终止程序。
我对编程和编码等还很陌生,所以仍然有很多概念和技巧我还不知道或理解。
我能够查找或以其他方式找出该项目的大部分必要代码,并已将其大部分组装完毕,但有一些事情我仍然需要做,但不知道如何做:
1)我需要将代码的不同部分分成多个方法,但我不确定应该如何从主方法中分离、组织或访问它们。
2)我需要为程序创建一个循环,但我还没有找到让循环正常工作的方法;我要么陷入无限循环,要么根本没有循环。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Anagrams {
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to X's Anagram Solver!");
System.out.println("Please enter a word to scramble to continue, or enter 'n' or 'N' to quit:");
List<String> words = new ArrayList<>();
try (Scanner Kbin = new Scanner(new File("dict.txt"))){
while (Kbin.hasNext()) {
words.add(Kbin.next());
}
}
Map<String, List<String>> map = new HashMap<>();
for (String str : words) {
char[] temp = str.toCharArray();
Arrays.sort(temp);
String key = new String(temp).toLowerCase();
if (map.get(key) != null) {
map.get(key).add(str.toLowerCase());
}
else {
List<String> anagramList = new ArrayList<>();
anagramList.add(str);
map.put(key, anagramList);
}
}
Scanner input = new Scanner(System.in);
String str = input.next();
char[] key = str.toCharArray();
Arrays.sort(key);
str = new String(key).toLowerCase();
if (!map.containsKey(str)) {
System.out.println("The input word was not found in our dictionary file.");
System.out.println("Please enter a different word to scramble to continue, or enter 'n' or 'N' to quit:");
}
else if (map.get(str).size() != 1) {
System.out.println("All words found in '" + str + "':");
for (String p : map.get(str)) {
System.out.println(p + " ");
}
System.out.println("You may enter a new word to scramble, or enter 'n' or 'N' to quit:");
}
else {
System.out.println("No anagrams for this word were found.");
System.out.println("You may enter a new word to scramble, or enter 'n' or 'N' to quit:");
}
str = input.next();
System.out.println("Thank you for using X's Anagram Solver!");
System.out.println("Have a nice day!");
input.close();
}
}
为了清楚起见,如果用户要为程序输入单词“共享”,该单词将被打乱并存储为其规范形式(按排序顺序存储的字母)。然后,程序将确定 .txt 文件中是否存在单词“share”以及是否有任何其他单词共享相同的字母。然后输出是 .txt 中与“share”具有相同字母的单词列表,如下所示:
- 野兔
- 听到
- 分享
- 剪切
我应该如何将代码的不同部分分成方法?一旦我这样做了,我该如何实现循环呢?我真的很茫然,只是需要一些指导,如果有人能阐明我应该做什么,我将非常感激。
提前感谢任何可以帮助我的人!
附注抱歉,我无法提供代码中使用的 .txt 文件,但它基本上只是一个按字母顺序排列的长单词列表。
最佳答案
也许像这样oO
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class Anagrams {
Map<String, List<String>> map;
Anagrams(){
map = getAnagrams();
}
private List<String> getWords(){
List<String> words=new ArrayList<>();
try (Scanner Kbin = new Scanner(new File("dict.txt"))){
while (Kbin.hasNext()) {
words.add(Kbin.next());
}
} catch (FileNotFoundException e) {
System.out.println("couldnt find dict.txt");
}
return words;
}
private Map<String,List<String>> getAnagrams(){
Map<String, List<String>> map = new HashMap<>();
List<String> words = getWords();
for (String str : words) {
String key = sorted(str);
if (map.get(key) != null) {
map.get(key).add(str.toLowerCase());
}
else {
List<String> anagramList = new ArrayList<>();
anagramList.add(str);
map.put(key, anagramList);
}
}
return map;
}
public String sorted(String unsorted){
char[] key = unsorted.toCharArray();
Arrays.sort(key);
return new String(key).toLowerCase();
}
public void evaluate(String str){
str=sorted(str);
if (!map.containsKey(str)) {
System.out.println("The input word was not found in our dictionary file.");
System.out.println("Please enter a different word to scramble to continue, or enter 'n' or 'N' to quit:");
}
else if (map.get(str).size() != 1) {
System.out.println("All words found in '" + str + "':");
for (String p : map.get(str)) {
System.out.println(p + " ");
}
System.out.println("You may enter a new word to scramble, or enter 'n' or 'N' to quit:");
}
else {
System.out.println("No anagrams for this word were found.");
System.out.println("You may enter a new word to scramble, or enter 'n' or 'N' to quit:");
}
System.out.println("Thank you for using X's Anagram Solver!");
System.out.println("Have a nice day!");
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Welcome to X's Anagram Solver!");
System.out.println("Please enter a word to scramble to continue, or enter 'n' or 'N' to quit:");
Scanner input = new Scanner(System.in);
Anagrams anagrams=new Anagrams();
String str=input.next();
while(!"exit".equals(str)){
anagrams.evaluate(str);
str=input.next();
}
input.close();
}
}
关于Java 程序接受输入单词并输出在导入的 .txt 文件中找到的任何字谜?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445532/