我有一个数组中的单词(对象)列表。每个单词都有一个数字、单词和提示。(请不要数字比它在数组中的索引大 1)我希望用户能够删除数组中的一个项目。我编写了一个方法,它读取用户输入(int)单词,输入索引中的提示获取下一个数组中的提示和单词的值,然后那个将获取后面的提示和单词那等等
例如:首先就像
1 dog bark
2 cat meow
3 cow moo
4 chicken cluck
5 pig oink
用户在 3 处删除单词后
1 dog bark
2 cat meow
3 pig oink
4 pig oink
谁能告诉我问题是什么?
代码
public void deleteWord() throws IOException {
if (wCount > 0) {
int again = JOptionPane.YES_OPTION;
while (again == JOptionPane.YES_OPTION) {
int num = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter the number of the word you wish to delete", "Enter word number", JOptionPane.PLAIN_MESSAGE))-1;
int cnfrm = JOptionPane.showConfirmDialog(null, "Are you sure you wish to delete the word:" + "\n" + "\t" + wArr[num].getWrd(), "Are you sure?", JOptionPane.YES_NO_OPTION);
if (cnfrm == JOptionPane.YES_OPTION) {
for (int i = num; i < (wCount - 1); i++) {
for (int j = (i + 1); j < wCount; j++) {
wArr[i].setWrd(wArr[j].getWrd());
wArr[i].setHnt(wArr[j].getHnt());
}
}
wCount--;
wArr[wCount] = null;
}
PrintWriter pw = new PrintWriter(new FileWriter("words.txt", false));
for (int x = 0; x < wCount; x++) {
pw.println(wArr[x].toString(1));
}
pw.close();
displayWords();
again = JOptionPane.showConfirmDialog(null, "Do you wish to delete another word?", "Delete another wod?", JOptionPane.YES_NO_OPTION);
}
} else {
JOptionPane.showMessageDialog(null, "Thre are no words to delete", "ERROR", JOptionPane.ERROR_MESSAGE);
}
}
编辑:
这是一个家庭作业,这显然意味着我仍然不知道关于编程的分配,包括。ArrayList
s。我会找到他们的,但不幸的是这个项目(如果你想知道的话,Hangman)将于周一到期,所以我不会在这个程序中实施它。