简体   繁体   English

多个JOptionPane输入发送到一个String输出

[英]Multiple JOptionPane inputs sent to one String output

First time poster here. 第一次海报在这里。 You guys seem to always have answers when I'm stuck and end up googling the question. 你们似乎总是能得到我的答案,并最终搜寻了这个问题。 I'm usually directed here for answers. 我通常会直接在这里寻求答案。

I'm in an intro to java class in college, and so far so good. 我正在上大学的Java课的入门课程,到目前为止到目前为止还不错。 The snag I hit with my homework tonight made me decide to sign up and ask you all (because I'm too impatient and don't want to wait for my professor to email me back). 今晚我在作业中遇到的障碍使我决定注册并询问所有人(因为我太急躁,不想等待教授回信给我)。

In the first part of my homework, I have to ask 4 questions to the user (using display boxes), but am instructed to have one String in the method. 在我的家庭作业的第一部分中,我必须向用户询问4个问题(使用显示框),但被要求在方法中使用一个String。 I assume this one String is going to prompt the user 4 different times. 我假设这个字符串会提示用户4次不同的时间。 Got that to work. 上班了。 When I show the user their choices for each question, I get a display box with every answer with no spaces or line breaks. 当我向用户显示他们对每个问题的选择时,我会得到一个显示框,其中包含每个答案,没有空格或换行符。

My question is, is there a way to force line breaks after each question, even though every question is going to one String? 我的问题是,即使每个问题都指向一个String,是否也可以在每个问题后强制换行?

Here's my code for you to check out. 这是我的代码供您签出。 Before you say anything about me not using JOptionPane.YES_NO_CANCEL_OPTION, for my yes/no questions, we haven't covered them in class, so my professor is not looking for us to be using that just yet. 在您对我不使用JOptionPane.YES_NO_CANCEL_OPTION的任何内容发表评论之前,关于我的是/否问题,我们尚未在课堂上讨论它们,因此我的教授并不希望我们现在使用它。 I look forward to hearing what advice you have to offer. 我期待听到您所提供的建议。

package week4_project;

import javax.swing.*;

public class Week4_Methods_Assignment 
{
    //Constants of Dialog Box types
        final public static int ERROR = 0;
        final public static int INFORMATION = 1;
        final public static int WARNING = 2;
        final public static int QUESTION = 3;
        final public static int PLAIN = -1;

    //Main method
    public static void main(String[] args) 
    {
        final String ASKUSER = getUserInput();
        String message = "";

        message = ASKUSER;

        JOptionPane.showMessageDialog(null, message, "", INFORMATION);

        System.exit(0);     
    }//end of main

    /*This method will ask the user questions about 
      how they drink coffee*/ 
    private static String getUserInput()
    {

        String question = "";

        question += JOptionPane.showInputDialog(null,
                "Regular or decaf", "Type of coffee", QUESTION);
        question = question.trim();

        question += JOptionPane.showInputDialog(null, "Yes or No", 
                "Do you take cream?", QUESTION);
        question = question.trim();

        question += JOptionPane.showInputDialog(null, "Yes or No", 
                "Do you take sugar?", QUESTION);
        question = question.trim();

        System.out.println(question);

        return question;        
    }

}//end of class

You can put System.lineSeparator in your string by concatenating. 您可以通过串联将System.lineSeparator放入字符串中。 This returns the proper line separator for your operating system. 这将为您的操作系统返回正确的行分隔符。

question += JOptionPane.showInputDialog(null, "Regular or decaf", "Type of coffee", QUESTION);
question = question.trim() + System.lineSeparator();

Note that if you are building strings you need to start using StringBuilder to do that, rather than concatenating strings with += . 请注意,如果要构建字符串,则需要开始使用StringBuilder来执行此操作,而不是使用+=将字符串连接在一起。

If you concatenate strings the way you are doing it now, you are creating a new String object each time you call question += "..."; 如果以现在的方式连接字符串,则每次调用question += "...";时,都将创建一个新的String对象question += "..."; . This will trash the heap and lead to fragmentation, it will also consume more memory and lead to more garbage collections to free memory. 这将浪费堆并导致碎片,也将消耗更多的内存并导致更多的垃圾回收以释放内存。

If you instead use StringBuilder with a suitable capacity , you will not trash the heap. 如果改用具有适当容量的 StringBuilder ,则不会破坏堆。 Instead, this class prepares a buffer of suitable length that gets filled as you append strings to it. 相反,此类准备了适当长度的缓冲区,当您将字符串追加到该缓冲区时,该缓冲区将被填充。

When your string is finally built, call StringBuilder.toString to get the string you want. 最终构建完字符串后,调用StringBuilder.toString以获取所需的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM