您好,我正在制作一个java工作,出于某种原因,当我在对话框中单击“否”时它到达了if else,它不会退出,而是继续询问学生姓名`public static void main( String [] args){
JFrame frame = new JFrame("Student Record");
JOptionPane.showMessageDialog(frame,
"Welcome to the School's Student Grade Record!");
System.out.println("School's Student Grade Record");
int welcome = JOptionPane.YES_NO_OPTION;
JOptionPane.showConfirmDialog
(null, "Woild you like to input grades on the students?",
"Grade",welcome);
if (welcome == JOptionPane.YES_OPTION){
String name;
String name1 = "";
do {
name = JOptionPane.showInputDialog("Enter Student's Name");
if (name.matches("^[a-zA-Z ]*$")){
name1 = name;
System.out.println("Student name :" +name1);
}else{
JOptionPane.showMessageDialog(frame,"Enter Student name");
}
}while (!name.matches("^[a-zA-Z ]*$"));
}else {
System.exit(0);
}
}
}`
答案 0 :(得分:0)
您的代码只是将JOptionPane.YES_NO_OPTION
(即0)的值复制到您的welcome
中,稍后您将其与JOptionPane.YES_OPTION
进行比较,0==0
也是0 if
将始终为true,因此showConfirmDialog
块将始终执行。
int
将为您提供int welcome;
// store the output result of showConfirmDialog in welcome
welcome=JOptionPane.showConfirmDialog(null, "Woild you like to input grades on the students?",
"Grade",JOptionPane.YES_NO_OPTION);
// compare result with predefined positive value i.e JOptionPane.YES_OPTION
if (welcome == JOptionPane.YES_OPTION){
结果,您必须将其存储在某个变量中,因此请像这样使用
{{1}}