3
我正在做一些實踐工作,擴展了我最近寫的一個作業項目。這不是一個年級,但我想添加一些錯誤檢查到我的代碼。錯誤檢查是否語句
程序要求您輸入名稱,從下拉列表中選擇,從列表框中選擇並選擇一個單選按鈕。我的目標是填充錯誤消息框,如果任何需要的項目是空白的。
我到目前爲止的錯誤檢查代碼如下,但林不知道如何採取個人缺乏項目和填充到消息框,因爲所有的錯誤檢查是在一個單獨的「如果」聲明。
錯誤校驗碼:
// Listener to handle the print button.
class ButtonListener implements ActionListener
{
ButtonListener() {}
public void actionPerformed(ActionEvent e)
{
JFrame frame1 = new JFrame("Show Message Dialog");
// Checks for required entries
if (error == 0 || name == "" || flag == 0)
{
JOptionPane.showMessageDialog(frame1,
"You must complete the form: " + missing, "ERROR",
JOptionPane.ERROR_MESSAGE);
}
else
{
// Get values from fields
setText();
System.out.println("Passenger's Name: " + name + "\n");
System.out.println("Age Group: " + ageValue + "\n");
for (int i = 0; i < value.length; i++)
{
System.out.println("Destination: " + value[i] + "\n");
}
System.out.println("Departure Day: " + day + "\n");
}
}
}
謝謝!
我想你需要把它們分解成單獨的'if'語句,每個字段一個,並且該字段有相應的錯誤信息。 – 2012-04-19 21:25:05
考慮使用IDE等IDE來格式化您的代碼。 – adarshr 2012-04-19 21:25:36
+1對於自主學習 – 2012-04-19 21:26:43