JOptionPane的Java问题
我一辈子都搞不懂这个项目的问题。当试图添加记录或显示记录时,我的程序会询问我是否要查找更多记录。每次我输入y
或n
时,程序都会执行我的errMessage()
方法。两个输入都会出现错误消息,然后显示Have a Nice Day!
基本上,我只是想知道我的语法或代码的编写方式是否有问题
public class cmdLst_C
{
// main() method
public static void main(String[] args) {
// variable declarations
// declare loopagain as boolean assigned true
boolean loopagain = true;
// declare strArg as String
String strArg;
String strMenu;
/** Declare strMenu as String and Assign a string that creates a menu as follows:
* [A]dd record
* [F]ind record
* [S]how All records
*/
strMenu = " [A]dd record\n " +
" [F]ind record\n " +
" [S]how All records\n ";
// determine there is a command line argument by testing the args array for a length of 0
if (args.length == 0)
{
// create a loop statement using the boolean loopagain variable
while (loopagain=true)
{
// Display the menu string in an InputDialog and assign the return value to the variable strArg
strArg = JOptionPane.showInputDialog(null, strMenu ,"Menu"
, JOptionPane.QUESTION_MESSAGE);
// convert the string strArg to upper case
strArg = strArg.toUpperCase();
// Determine if an entry was made in the dialog by testing strArg for a length greater than 0
if (args.length == 0)
{
// setup a switch construct that uses the character value of strArg as its argument
switch (strArg)
{
// if strArg is the character 'A'
// call the addrec() method
// assign a value to the boolean loop_variable using the loopquery() method
case "A":
if (strArg.equals("A"))
{
addRec();
loopquery();
}
// if strArg is the character 'F'
// call the findrec() method
// assign a value to the boolean loop_variable using the loopquery() method
case "F":
if (strArg.equals("F"))
{
findRec();
loopquery();
}
// if strArg is the character 'S'
// call the showall() method
// assign a value to the boolean loop_variable using the loopquery() method
case "S":
if (strArg.equals("S"))
{
showAll();
loopquery();
}
// if none of the above, run an errMessage() method
// end of switch
}
// end of strArg test
// otherwise, if no entry, run an errMessage() method
// end of loop
break;
}
// exit program with the MessageDialog "Have a Nice Day!"
JOptionPane.showMessageDialog(null, "Have a Nice Day!");
// end of args test
}
/** end of main() method */
}
}
}
# 1 楼答案
这个
是一个赋值,作为副作用,它的计算结果为
true
。使用或者
而且,你在
case
中的if
测试是毫无意义的。这就是switch
测试的内容