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

Mutliple lines of a while loop printing in gui dialog (Java)

我想在 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");
    }


}

它可以工作,但会打印出 "The calculation is 5x1 = 5",然后打开一个新的消息框来显示 "The calculation is 5x2 = 10"。 我要它打印出来

计算结果为 5x1 = 5 计算结果为 5x2 = 10 等等

全部在 1 个文本框内

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

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

    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)

循环前:String multimsg = "";

在数学之后的循环中:multimsg=multimsg+"The calculation is " + num1 + " x " + counter + "=" + total; 在 while 循环 JOptionPane.showMessageDialog(null, multimsg);

之后

只构建循环内的 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");
}

问题是您在 while 循环中显示消息。 while 循环的语法可能有点混乱。

如果您使用的是 java 8,则可以将 while/counter 替换为 IntStream,并且可以通过在收集器中加入字符串来撰写消息:

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

第一行创建从 1 到 12 的整数流。第二行将每个数字映射到单独的文本消息。最后一行通过用 ,

连接所有单独的消息来连接它们

您的主要方法如下所示:

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");

}