我在让程序运行时遇到了一些问题。我在这方面非常基础,并且没有很多经验,这是我的第一个程序,问题是JOptionPane的循环没有结束,JOptionMenu你出现并问我添加,查找或显示所有记录,无论我的输入是什么,它都会说“有一个美好的一天"”,但不会结束并返回添加,查找或显示所有记录JOTION输入。我假设我的编码有点偏差,并且代码中的其他地方可能是一个错误,但是对此的任何帮助都会令人惊讶并且非常难以理解!
public static void main(String[] args) {
// variable declarations //
boolean loopagain=true;
// declare strArg as String //
String strArg;
String strMenu;
char strArgs;
// declare strMenu as String and Assign a string that creates a menue 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 ";
if (args.length == 0){
while (loopagain) {
strArg = JOptionPane.showInputDialog(null, strMenu ,"Menu"
, JOptionPane.QUESTION_MESSAGE);
strArg = strMenu.toUpperCase();
strArgs = strArg.toUpperCase().charAt(0);
if (args.length > 0){
switch (strArgs) {
case 0:
if (strArgs == 'A')
addRec();
break;
case 1:
if (strArgs == 'F')
findRec();
break;
case 2:
if (strArgs == 'S')
showAll();
break;
default:
errMessage();
// end of switch //
}
// end of strArg test //
// otherwise, if no entry, run an errMessage() method // errMessage();
// end of loop //
break;
}
/** exit program with the MessageDialog "Have a Noce Day!" */
JOptionPane.showMessageDialog(null, "Have a Nice Day!");
// end of args test //
}
// end of main() method //
}
public static void errMessage() {
JOptionPane.showMessageDialog(null, "Invalid Menu Choice");
}
// create a method named loopquery()
// - that returns a boolean value
// - accepts no arguments
// - content:
// - declaration of a boolean variable initialized to false
// - an InputDialog that requests if you want to loopagain (y,n)and assigns the value to a string variable
// - converts the String variable to upper case
// - changes the value of the boolean variable to true if the string variable has a value of "Y"
// - returns the value of the boolean variable
//
public static void loopquery() {
String loopquery;
boolean loopagain;
loopagain=false;
loopquery = JOptionPane.showInputDialog(null, "Another table (y.n)",
"Again?", JOptionPane.QUESTION_MESSAGE);
loopquery = loopquery.toUpperCase();
}
public static void addRec() {
JOptionPane.showMessageDialog(null, "AddRec");
}
public static void findRec() {
String findrecs;
findrecs = JOptionPane.showInputDialog(null, "Request a record" ,"Record"
, JOptionPane.QUESTION_MESSAGE);
System.out.println("FindRec: " + findrecs);
}
public static void showAll() {
JOptionPane.showMessageDialog(null, "ShowAll");
} }
答案 0 :(得分:0)
while (loopagain) {
...
...
JOptionPane.showMessageDialog(null, "Have a Nice Day!");
// end of args test //
}
这需要
do{
...
...
}while(loopquery());
JOptionPane.showMessageDialog(null, "Have a Nice Day!");
// end of args test //
你所做的是将loopagain设置为true并且永远不会改变它以便它无限循环 退出消息也在错误的地方
答案 1 :(得分:0)
您的错误在这里:
if (args.length == 0) {
while (loopagain) {
strArg = JOptionPane.showInputDialog(null, strMenu, "Menu", JOptionPane.QUESTION_MESSAGE);
strArg = strMenu.toUpperCase();
strArgs = strArg.toUpperCase().charAt(0);
if (args.length > 0) {
// ...
break; // you never get here, so you never break loop
您是否有意使用args
?这是命令行参数数组。如果是length == 0
,则以后不会length > 0
。由于你永远不会进入这个区块,所以你永远不会打破循环。