邪恶刽子手Java程序
我在做一个邪恶的刽子手程序,出于某种原因,我得到了这个错误:邪恶刽子手Java程序,java,maps,Java,Maps,我在做一个邪恶的刽子手程序,出于某种原因,我得到了这个错误: What length word do you want to use? 4 How many wrong answers allowed? 4 guesses : 4 guessed : [] current : ---- Your guess? e Exception in thread "main" java.lang.NullPointerException at Game.HangmanManager.recor
What length word do you want to use? 4
How many wrong answers allowed? 4
guesses : 4
guessed : []
current : ----
Your guess? e
Exception in thread "main" java.lang.NullPointerException
at Game.HangmanManager.record(HangmanManager.java:142)
at Game.HangmanMain.playGame(HangmanMain.java:62)
at Game.HangmanMain.main(HangmanMain.java:42)
这是我的节目:
package Game;
import java.util.*;
public class HangmanManager
{
private String pattern;
private int max;
private int length;
private SortedSet<Character> guessesMade;
private Set<String> currentWords;
private Map<String, Set<String>> patternMap;
public HangmanManager(List<String> dictionary , int length, int max)
{
this.max = max;
this.length = length;
if( length < 1 && max < 0)
{
throw new IllegalArgumentException();
}
words = new TreeSet<String>();
guessesMade = new TreeSet<Character>();
currentWords = new TreeSet<String>(); // current words =(words)
patternMap = new TreeMap<String, Set<String>>(); // patternMAP = < pattern, words>
for (String word : dictionary)
{
if (word.length() == length)
{
words.add(word); // if length of the word matches a word with the same length it will be added
}
}
}
public Set<String> words()
{
return words;
}
public int guessesLeft()
{
return max - guessesMade.size();
}
public SortedSet<Character> guesses()
{
return guessesMade;
}
public String pattern()
{
if (words.isEmpty())
{
throw new IllegalArgumentException("Invalid No Words");
}
pattern = " "; // blank for now
for (int i = 0; i < length; i++)
{
pattern += "-"; // will have a "-" for how long the length of the word is
}
return pattern; // will return the number of lines
}
public int record(char guess)
{
if (guessesLeft() < 1 || words.isEmpty())
{
throw new IllegalStateException();
}
if (!words.isEmpty() && guessesMade.contains(guess))
{
throw new IllegalArgumentException();
}
guessesMade.add(guess); // guess
int occurences = 0;
for( String word: words)
{
if( patternMap.containsKey (pattern))
{
occurences = generatePattern(word, guess); // the word including the guess letter will fill in the blank spots
currentWords.add(word); // the word will be added to the possibilities
currentWords = patternMap.get(pattern); // the word will be able to fill once the guesses are made
// if(patternMap.get(pattern)!=null)
// {
// currentWords = patternMap.get(pattern);
//
// }
patternMap.put(pattern, currentWords);
}
else
{
currentWords.add(word);
patternMap.put(pattern, currentWords);
}
}
words = find();
return occurences;
}
private Set<String> find()
{
int maxSize = 0;
Map <String, Integer> patternCount = new TreeMap<String, Integer>();
for (String key : patternMap.keySet()) // keyset equals word
{
patternCount.put(key, patternMap.get(key).size()); // size of the word
if (patternMap.get(key).size() > maxSize)
{
maxSize = patternMap.get(key).size();
pattern = key; // pattern will becomes based on the word
} else if (patternMap.get(key).size() == maxSize)
{
if (key.length() >= pattern.length())
{
pattern = key;
maxSize = patternMap.get(key).size(); // the pattern is now the word key
}
}
}
System.out.println("Current pattern: " + pattern);
return patternMap.get(pattern); // the pattern that will becomes now that the word was picked
}
private int generatePattern(String s, char guess)
{
int count = 0;
pattern = "";
for (int i = 0; i < length; i++)
{
if (s.charAt(i) == guess)
{
pattern += guess + " ";
count++;
} else
{
pattern += "- ";
}
}
return count;
}
}
打包游戏;
导入java.util.*;
公营机库经理
{
私有字符串模式;
私人int max;
私有整数长度;
私人分类集猜测模型;
私设词语;
私人地图;
公共Hangman Manager(列表字典,整数长度,整数最大值)
{
this.max=max;
这个长度=长度;
如果(长度<1&&最大值<0)
{
抛出新的IllegalArgumentException();
}
单词=新树集();
Guessmade=新树集();
currentWords=新树集();//当前单词=(单词)
patternMap=newtreemap();//patternMap=
for(字符串:字典)
{
if(word.length()=长度)
{
words.add(word);//如果单词的长度与相同长度的单词匹配,则会添加该单词
}
}
}
公共集合词()
{
返回单词;
}
公共int猜测左()
{
返回max-guessmade.size();
}
公共分类集猜测()
{
返回猜测模型;
}
公共字符串模式()
{
if(words.isEmpty())
{
抛出新的IllegalArgumentException(“无效无单词”);
}
pattern=“;//暂时为空
for(int i=0;imaxSize)
{
maxSize=patternMap.get(key.size();
pattern=key;//模式将基于单词
}else if(patternMap.get(key).size()==maxSize)
{
如果(key.length()>=pattern.length())
{
模式=键;
maxSize=patternMap.get(key).size();//模式现在是单词key
}
}
}
System.out.println(“当前模式:+模式”);
return patternMap.get(pattern);//拾取单词后将成为的模式
}
私有int-generatePattern(字符串s,字符猜测)
{
整数计数=0;
模式=”;
for(int i=0;i
错误似乎发生在以下对象的记录方法中:
patternMap.put(模式,当前字)代码>第149行
我多次运行调试器,我注意到如果您遵循程序,您将看到在程序运行一次单词之后,currentWords最终变为Null,即使我实例化了它并为patternMap创建了一个映射
如果有人能告诉我该做什么或改变什么,我会非常感激,因为我对这个patternMap
是一个TreeMap
。正如您所说,您的问题在于patternMap.put(pattern,currentWords)代码>在第149行。根据TreeMap
的JavaDocs,put()
抛出NullPointerException
如果指定的键为null,并且此映射使用自然排序,或者其比较器不允许null键
由于树映射
使用其键的自然顺序(即字符串
),问题在于模式
为空
。为什么不将模式初始化为默认值:
private String pattern = " ";
您的代码不完整,无法编译,这意味着您的问题要么将作为副本关闭,要么“为什么我的代码不起作用-没有提供可运行的示例”我可以提供HangmanMain程序和字典文本文件。我注意到currentWords为空,而不是patternpatternMap.put(pattern,null)如果模式