我無法將字典文件與字典進行比較。我在每一個打印語句,它正在正確閱讀字典文件,它也正確計算所有的字形但它不會計算字典文件中的字形。我很確定這是非常小的事情,如果有人可以修復它,將不勝感激。Java字典從字典文件中讀取
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Anagram3
{
static int size;
static int count;
static char[] charArray;
static char[] words;
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
System.out.println("Type the path of the dictionary to read from : ");
String fileName = sc.nextLine();
List<String> dictionary = new ArrayList<String>();
BufferedReader br = null;
try
{
br = new BufferedReader(new FileReader(fileName));
String word;
while((word = br.readLine())!=null)
{
dictionary.add(word);
}
}
catch(IOException e)
{
e.printStackTrace();
}
String[] words = new String[dictionary.size()];
dictionary.toArray(words);
//for(int i = 0; i < words.length; i++)
// System.out.println(words[i]);
System.out.println("\nEnter the phrase to scramble: ");
String input = sc.nextLine();
System.out.println();
size = input.length();
count = 0;
charArray = new char[size];
for (int j = 0; j < size; j++)
charArray[j] = input.charAt(j);
doAnagram(size);
}
public static void doAnagram(int newSize)
{
int limit;
if (newSize == 1) // if too small, return;
return;
// for each position,
for (int i = 0; i < newSize; i++) {
doAnagram(newSize - 1); // anagram remaining
if (newSize == 2) // if innermost,
printAnagrams();
rotate(newSize); // rotate word
}
}
public static void rotate(int newSize)
{
int i;
int position = size - newSize;
char temp = charArray[position];
for (i = position + 1; i < size; i++)
charArray[i - 1] = charArray[i];
charArray[i - 1] = temp;
}
public static void printAnagrams()
{
for (int i = 0; i < size; i++)
{
//System.out.print(charArray[i]);
if(charArray[i] == words[i])
{
System.out.print(charArray[i]);
}
}
System.out.println();
}
}
(1)請檢查粘貼代碼,它看起來很滑稽在中間。 (2)你有沒有檢查過你產生的排列在字典中? – 9000
是的,這就是我在if語句的printAnnagrams()語句中所做的,這就是我遇到的問題。抱歉,這是我第一次做這個,看起來有趣嗎? – user1022969
嗯,'if(charArray [i] == words [i])'甚至不應該編譯,比較'char'和'String'。如果它編譯,它不應該評估爲「真」。 –