在你的循環,你應該通過名稱列表迭代和每個元素調用equalsIgnoreCase(「Q」),將另一個輸入對話框每次迭代。
考慮:
while (names.isEmpty() || !names.get(names.size() - 1).equalsIgnoreCase("q"))
{
names.add(JOptionPane.showInputDialog("Enter type of pie"));
}
如果你想忽略餡餅「Q」的名字:
while (names.isEmpty() || !names.get(names.size() - 1).equalsIgnoreCase("q"))
{
names.add(JOptionPane.showInputDialog("Enter type of pie"));
}
// populates the ArrayList names with the JOptionPane user input
if (!names.isEmpty())
{
names.remove(names.size() - 1);
// remove the last name inputted by the user
// since the only way to terminate the loop is by entering "q",
// you are removing the name of "q" from the list.
}
編輯: 這是一個更好的實現,因爲它只是增加了名稱不是「q」:
String userInput = new String();
while (!userInput.equalsIgnoreCase("q"))
{
userInput = JOptionPane.showInputDialog("Enter type of pie");
if (!userInput.equalsIgnoreCase("q"))
{
names.add(userInput);
}
}
你是什麼意思它無法找到它呢? – Marcin
編譯錯誤說找不到符號「名稱」 – dan
在添加for循環之前它工作嗎? I.e是否在上面發佈的代碼編譯? – Marcin