我不能为我的生活找出这个程序的问题。在尝试添加记录或显示记录时,我将程序设置为询问我是否要查找更多记录。每次输入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 */
}
}
}
答案 0 :(得分:2)
此
while (loopagain=true) {
是一项作业,作为副作用,它评估为true
。使用
while (loopagain==true) {
或
while (loopagain) {
此外,if
中的case
次测试毫无意义。这就是switch
测试的内容。