public static void main(String[] args) {
String password, firstOption, firstNumber = "";
int number, option;
password = JOptionPane.showInputDialog("Please Enter Password");
// now starts code
if (password.equals("HW1")) {
firstOption = JOptionPane.showInputDialog("Please select an option: \n0 = Quit \n1= Math Time");
option = Integer.parseInt(firstOption);
if (option == 0) {
JOptionPane.showMessageDialog(null, "You Quit!", "Quitter!", JOptionPane.OK_CANCEL_OPTION);
} else {
firstNumber = JOptionPane.showInputDialog("Please enter a number between 1-30:");
number = Integer.parseInt(firstNumber);
if (number <= 30 && number >= 0) {
JOptionPane.showMessageDialog(null, "The number " + number + " to the second power is " + number,
"Math Time", JOptionPane.OK_OPTION);
} else {
JOptionPane.showMessageDialog(null, "That is not a number between 1-30",
"Idiot Alert!", JOptionPane.OK_OPTION);
}
}
else {
JOptionPane.showMessageDialog(null, "Incorrect Password", "Incorrect Password", JOptionPane.OK_OPTION);
}
}
问题在于最后一个else语句,我希望它对应于if语句(password.equals(“HW1”)
基本上我希望它检查并查看密码是否为HW1,如果是,则运行if语句中的所有代码。如果没有,请运行else语句
答案 0 :(得分:1)
你的其他陈述超出范围 - 你没有把它放在正确的位置。
你应该看一下
的内容if(password.equals("HW1") {
//The password was correct
...
} else {
//The password was incorrect
JOptionPane.showMessageDialog(null,"Incorrect Password", "Incorrect Password", JOptionPane.OK_OPTION);
}
答案 1 :(得分:0)
在您撰写if
之前,您没有关闭第一个else
使第二个if
具有双倍else
...
尝试在最后}
之前添加else
!
答案 2 :(得分:0)
您缺少一个结束花括号。缩进告诉你。
变化:
}
}
else {
要:
}
}
} else {
答案 3 :(得分:0)
好的,在其他和括号
之间切换 else
JOptionPane.showMessageDialog(null,"Incorrect Password", "Incorrect Password", JOptionPane.OK_OPTION);
}
在if的括号外面取其他。
答案 4 :(得分:0)
你忘了一个括号,试试这个:
public static void main(String[] args) {
String password, firstOption, firstNumber = "";
int number, option;
password = JOptionPane.showInputDialog("Please Enter Password");
// now starts code
if (password.equals("HW1")) {
firstOption = JOptionPane.showInputDialog("Please select an option: \n0 = Quit \n1= Math Time");
option = Integer.parseInt(firstOption);
if (option == 0) {
JOptionPane.showMessageDialog(null, "You Quit!", "Quitter!", JOptionPane.OK_CANCEL_OPTION);
} else {
firstNumber = JOptionPane.showInputDialog("Please enter a number between 1-30:");
number = Integer.parseInt(firstNumber);
if (number <= 30 && number >= 0) {
JOptionPane.showMessageDialog(null, "The number " + number + " to the second power is " + number,
"Math Time", JOptionPane.OK_OPTION);
} else {
JOptionPane.showMessageDialog(null, "That is not a number between 1-30",
"Idiot Alert!", JOptionPane.OK_OPTION);
}
}
} else { // Modified: Bracket added
JOptionPane.showMessageDialog(null, "Incorrect Password", "Incorrect Password", JOptionPane.OK_OPTION);
}
}