【问题标题】:Homework Help: Why won't variables return作业帮助:为什么变量不会返回
【发布时间】:2014-12-03 21:28:29
【问题描述】:

我想不通

我的变量没有在输出消息中返回打印我不知道我哪里出错了,我的老师告诉我逐行检查代码并检查逻辑和语法错误。任何帮助,将不胜感激。

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 + ".\n\n";
        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);
    }
}

【问题讨论】:

  • 首先请正确缩进,否则我还没来得及帮忙就死了
  • 第一条评论同上,这让人很难理解发生了什么。并删除所有无用的 cmets 和空白。
  • 抱歉,StackOverflows 很乐意回答特定的编程问题,或协助解决代码中的特定障碍,但我们不会通过它来为您提供答案。正如@realUser404 所说,一个好的第一步是仔细缩进,但是你必须按照老师的说法逐行检查,直到找到不工作的部分。这是最有效的学习方法。 =)
  • 第二,我相信你的 IDE 有一个“调试”工具。您只需要在方法的第一行放置一个“停止”,然后正如您的老师所说,只需逐行检查变量的值。然后您将能够非常轻松地找到错误

标签: java methods input


【解决方案1】:

当您希望它返回输入时,您的 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 2015-08-30
    • 2014-05-10
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多