我无法弄清楚
我的变量没有返回打印在输出消息中,我不知道我哪里出错了,我的老师告诉我逐行检查代码并检查逻辑和语法错误。任何帮助将不胜感激。
package selfhelpstore;
import javax.swing.JOptionPane;
public class SelfHelpStore {
private static String getStringInput(String prompt) {
String input = JOptionPane.showInputDialog(prompt);
String nullFlag = "n";
int i = 1;
while (i<=2)
if (input == null) {
System.exit(0);
}else if (input.isEmpty()) {
i++;
nullFlag = "y";
prompt = JOptionPane.showInputDialog("Re-enter: ");
} else {
i=4;
nullFlag = "n";
}
if (!input.isEmpty()) {
nullFlag = "n";
}
if (nullFlag.equals("y")) {
JOptionPane.showMessageDialog(null, "Null or blank - exiting...");
System.exit(0);
}
return prompt;
}
public static void main(String[] args) {
String openingMsg, nameInputMsg, customerName, nameOutputMsg,
returnInputMsg, customerReturn, returnOutputMsg,
greetingOutputMsg, outputMsg,coverInputMsg, coverMsg ;
openingMsg = "*** Welcome to Self Help Book Store Online Ordering System ***n"
+ " It's a great day to read a book!";
JOptionPane.showMessageDialog(null, openingMsg);
nameInputMsg = getStringInput("Please enter your name: ");
customerName = nameInputMsg;
returnInputMsg = getStringInput("Are you a returning customer (yes or no)? ");
customerReturn = returnInputMsg;
coverInputMsg = getStringInput("would you like this book in hard cover or paper back?");
coverMsg = coverInputMsg;
nameOutputMsg = "Welcome " + customerName + ".nn";
returnOutputMsg = "Your return customer status is " + customerReturn + ".n";
greetingOutputMsg = "Thank you for visiting our Self Help Book Store!" + "n"
+ "Your " + coverMsg + " should be mailed in less than two business days.n";
outputMsg = nameOutputMsg + returnOutputMsg + greetingOutputMsg;
JOptionPane.showMessageDialog(null, outputMsg);
System.exit(0);
}
}
当你希望它返回输入时,你的getStringInput方法正在返回变量提示。
private static String getStringInput(String prompt) {
String input = JOptionPane.showInputDialog(prompt);
String nullFlag = "n";
int i = 1;
while(i <= 2)
if(input == null)
{
System.exit(0);
} else if(input.isEmpty())
{
i++;
nullFlag = "y";
prompt = JOptionPane.showInputDialog("Re-enter: ");
} else {
i = 4;
nullFlag = "n";
}
if(!input.isEmpty()) {
nullFlag = "n";
}
if(nullFlag.equals("y")) {
JOptionPane.showMessageDialog(null, "Null or blank - exiting...");
System.exit(0);
}
return input; // Change this line
}