在gui对话框中打印while循环的多个行(Java)

时间:2016-11-15 14:57:32

标签: java messagebox

我想在1个gui对话框中打印多行while循环。

这是我目前的代码

package Tempconv1;
import javax.swing.JOptionPane;
public class TimesTables 
{

    public static void main(String[] args)
    {
        int num1 = 0, counter = 0, total = 0; //Declaring Variables
        String str; //String for ShowInputDialog

        str = JOptionPane.showInputDialog("Insert a number");  //Asks user to input number
        num1 = Integer.parseInt(str); 


            //Calculating number inputs
            while (counter <12)
            {
                counter = counter + 1;
                total = counter * num1;
                String multimsg = ("The calculation is " + num1 + " x " + counter + "=" + total);
                JOptionPane.showMessageDialog(null, multimsg);
            }
            JOptionPane.showMessageDialog(null, "All Done");
    }


}

它工作但打印出“计算是5x1 = 5”,然后打开一个新的消息框以显示“计算是5x2 = 10”。 我希望它打印出来

计算是5x1 = 5 计算是5x2 = 10 等

全部在1个文本框中

4 个答案:

答案 0 :(得分:0)

您告诉程序要做的是创建单个计算的字符串,然后创建一个对话框。然后再为每个循环再做一次。

您需要将所有消息构建到循环内的一个字符串中,然后在循环外创建对话框。

    String multimsg = "";
    while (counter <12)
    {
        counter = counter + 1;
        total = counter * num1;
        multimsg += ("The calculation is " + num1 + " x " + counter + "=" + total);
    }
    JOptionPane.showMessageDialog(null, multimsg);

您可能希望使用stringbuilder或类似方法来更有效地构建消息字符串。 (https://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html

答案 1 :(得分:0)

问题是您在while循环中显示消息。使用while循环的语法可能有点令人困惑。

如果你使用的是java 8,你可以用IntStream替换while / counter,你可以通过加入收集器中的字符串来编写消息:

String message = IntStream.rangeClosed(1, 12)
                          .mapToObj(i -> "The calculation is " + num1 + "x" + i + " = " + i * num1)
                          .collect(Collectors.joining(", "));

第一行创建一个从1到12的整数流。第二行将每个数字映射到单个textmessage。最后一行通过将它们与a,

连接起来连接所有单个消息

你的主要方法看起来像这样:

public static void main(String[] args){
    int num1 = 0, counter = 0, total = 0; //Declaring Variables
    String str; //String for ShowInputDialog

    str = JOptionPane.showInputDialog("Insert a number");  //Asks user to input number
    num1 = Integer.parseInt(str); 


    //Calculating number inputs
    String message = IntStream.rangeClosed(1, 12)
                          .mapToObj(i -> "The calculation is " + num1 + "x" + i + " = " + i * num1)
                          .collect(Collectors.joining(", "));

    JOptionPane.showMessageDialog(null, message);
    JOptionPane.showMessageDialog(null, "All Done");

}

答案 2 :(得分:-1)

循环前的

String multimsg = "";

在数学后的循环中

multimsg=multimsg+"The calculation is " + num1 + " x " + counter + "=" + total; 并在while循环JOptionPane.showMessageDialog(null, multimsg);

之后

答案 3 :(得分:-1)

仅在循环内构建msg并将其保存到StringBuilder。

public static void main(String[] args)
{
    int num1 = 0, counter = 0, total = 0; //Declaring Variables
    String str; //String for ShowInputDialog

    str = JOptionPane.showInputDialog("Insert a number");  //Asks user to input number
    num1 = Integer.parseInt(str); 
    StringBuilder sb = new StringBuilder();

    //Calculating number inputs
    while (counter <12)
    {
        counter = counter + 1;
        total = counter * num1;
        sb.append("The calculation is ").append(num1).append(" x ").append(counter).append("=").append(total).append(" ");

    }
    JOptionPane.showMessageDialog(null, sb.toString());
    JOptionPane.showMessageDialog(null, "All Done");
}