簡體   English   中英

試圖讓方法起作用

[英]trying to get method to work

我有一個項目,需要找到一種工作方法。 我不確定它到底在哪里,但我還需要向該方法添加一個循環,以僅允許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){
}

您的代碼可能還有其他問題,我不知道您收到的是哪種異常,或者甚至無法編譯,但是這是一種實現循環的方法,以嘗試獲取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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM