对于本学期的计算机科学课程的作业,我需要构建一个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();
}
}
为清楚起见,如果用户要为程序输入单词“ share”,则该单词会被打乱并存储为规范形式(字母按排序顺序存储)。然后,程序将确定.txt文件中是否存在单词“ share”,以及是否有其他单词共享相同的字母。然后,输出是.txt中与“ share”具有相同字母的单词列表,如下所示:
如何将代码的不同部分分成方法?这样做后如何实现循环?我在这里真的很茫然,只需要一些指导,如果有人可以阐明我应该做什么,我将不胜感激。
在此先感谢您能帮助我的人!
P.S。抱歉,我没有提供代码中使用的.txt文件的方法,但它基本上只是一长串按字母顺序排列的单词。
答案 0 :(得分:0)
也许是这样
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();
}
}