我有一个项目,我需要一个方法来工作。我不确定它在哪里弄乱,但我还需要在方法中添加一个循环,只允许3次尝试输入或终止。如果它的空白3次程序应该终止。这需要一个while循环吗?
我很难接受这个。
private static String getStringInput(String prompt) {
// declare variables
String openingMsg, nameInputMsg, customerName, nameOutputMsg,
returnInputMsg, customerReturn, returnOutputMsg, colorInputMsg, colorSelection,
greetingOutputMsg, outputMsg;
// display opening message
openingMsg = "*** Welcome to Pizzas-R-Us Online Ordering System ***\n"
+ " It's a great day to order a pizza!";
JOptionPane.showMessageDialog(null, openingMsg);
// get required input using dialogs
nameInputMsg = "Please enter your name: ";
customerName = JOptionPane.showInputDialog(nameInputMsg);
returnInputMsg = "Are you a returning customer (yes or no)? ";
customerReturn = JOptionPane.showInputDialog(returnInputMsg);
if (customerReturn.equals("yes"))
{
JOptionPane.showMessageDialog(null, "Thanks for coming back!");
}
else if (customerReturn.equals("no"))
{
JOptionPane.showMessageDialog(null, "Thanks for being New!");
}
else
{
JOptionPane.showMessageDialog(null, "Invalid Syntax");
}
colorInputMsg = "What color would you like (black, silver, gold)";
colorSelection = JOptionPane.showInputDialog(colorInputMsg);
private static double totalCost(int number, double cost, double salesTaxRate)
// build output strings
nameOutputMsg = "Welcome " + customerName + ".\n\n";
returnOutputMsg = "Your return customer status is " + customerReturn + ".\n";
colorOutputMsg = "you have selected a " + colorSelection + " watch .\n;
greetingOutputMsg = "Thank you for visiting Pizzas-R-Us!" + "\n\n"
+ "Your order should be ready in less than 10 minutes.\n";
// create and display output string
outputMsg = nameOutputMsg + returnOutputMsg + colorOutputMsg + greetingOutputMsg;
JOptionPane.showMessageDialog(null, outputMsg);
System.exit(0);
} // end main()
public static String getStringInput(String prompt)
{
String inputValue;
do
{
inputValue - jOptionPane.showInputDialog(prompt);
if (inputValue == null)
{
jOptionPane.showMessageDialog(null, "You have canceled the program");
System.exit(1);
}
if (inputValue.equals(""))
{
jOptionPane.showMessageDialog(null, "no input received")
}
return inputValue;
} while(inputValue.equals("" && ));
}
public static double totalCost(int number, double cost, double salesTaxRate){
}
答案 0 :(得分:0)
您的代码可能存在其他问题,我不知道您正在获得什么样的异常,或者它是否会编译,但是这是一种实现循环的方法得到输入3次,如果他们没有输入任何东西,那就很好了:
public static String getStringInput(String prompt) {
String inputValue;
int counter = 0; // introduce counter to track times no input received
do {
inputValue = jOptionPane.showInputDialog(prompt);
if (inputValue == null) {
jOptionPane.showMessageDialog(null, "You have canceled the program");
System.exit(1);
}
if (inputValue.equals("")) {
jOptionPane.showMessageDialog(null, "no input received")
counter++; // increment the counter
}
} while(inputValue.equals("") && counter < 3); // add condition to also exit loop if no input received 3 times
return inputValue; // moved return outside of loop
}