我尝试使用以下参数创建名为loopquery的方法:
/** 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
*/
以下是对此的看法,但我认为我错过了很多。有没有人可以帮我满足上述所有要求?继承我的呕吐代码
private static void loopquery() {
String loopquery;
boolean loopagain = true;
loopquery = JOptionPane.showInputDialog(null, "Another table (y.n)",
"Again?", JOptionPane.QUESTION_MESSAGE);
loopquery = loopquery.toUpperCase();
}
答案 0 :(得分:0)
符合要求:
您可以在代码末尾添加这些行来修改布尔值并返回值:
loopagain = loopquery.equals("Y");
return loopagain;
符合要求:
将boolean初始化为true,替换此初始化:
boolean loopagain = true;
对于这个:
boolean loopagain = false;
注意:
Java中变量和方法的标准符号是CamelCase,为了遵循标准命名约定,你应该分别为“loopQuery”和“loopAgain”替换名称“loopquery”和“loopagain”
答案 1 :(得分:0)
if(loopquery.equals("Y"))
return true;
else if (loopquery.equals("N"))
return false;
请记住,您必须将方法从void更改为boolean,因为您无法在void方法中返回值
此致
裴家
答案 2 :(得分:0)
private static boolean loopquery() {//change the return type to boolean
String loopquery;
boolean loopagain = false;//asked to have this initialized to false
loopquery = JOptionPane.showInputDialog(null, "Another table (y.n)",
"Again?", JOptionPane.QUESTION_MESSAGE);
if (loopquery.toUpperCase().equals("Y")) {
loopagain = true;
}
return loopagain;
}
您的基本结构大部分都是正确的,但与您提出的要求存在差异:
false
,您将其初始化为true
void
这是如何工作的,如果loopagain
为"Y"
,它会将loopagain更改为true
,否则会跳过并返回loopagain作为其初始值{{ {1}}。
答案 3 :(得分:0)
private static boolean isLoopQuery()
{
// Declaration of a boolean variable initialized to false
boolean loopAgain = false;
// An InputDialog that requests if you want to loopagain (y,n)and assigns the value to a string variable
String loopQuery = JOptionPane.showInputDialog(null, "Another table (y.n)",
"Again?", JOptionPane.QUESTION_MESSAGE);
// Converts the String variable to upper case
loopQuery = loopQuery.toUpperCase();
// Changes the value of the boolean variable to true if the string variable has a value of "Y"
if(loopQuery.equals("Y"));
{
loopAgain = true;
}
// returns the value of the boolean variable
return loopAgain;
}
希望这有点清楚并且符合您的每个要求。