使用 JOptionPane 显示输出

问题描述 投票:0回答:2

当我的输出由于我的方法中的

JOptionPane
循环而存在多种变化时,如何使用
if
显示我的方法的输出字符串? 例如:

public static int ballpath(int NumberOfSlot) {
    for (int i = 0; i < NumberOfSlot; i++) {
        if (Math.random() * 10 <= 5) {
            String output3 = "R";
            JOptionPane.showMessageDialog(null, output3);
        } else {
            String output4 = "l";
            JOptionPane.showMessageDialog(null, output4);
        }
    }
    JOptionPane.showMessageDialog();
}

当我在程序中调用该方法时,如何使用

JOptionPane
显示
"l"
"R"
? p.s 最后一段代码
JOptionPane.showMessageDialog();
不完整,因为我不知道如何显示输出

java swing
2个回答
0
投票

你的代码很不清楚(你想显示一堆joptionpanes吗?),但这可能就是你想要的:

String output = "";
for (int x = 0; x < NumberOfSlot; x++)
{
    if (Math.random() * 10 <= 5) {
        output+="R";
    } else {
        output+="l";
    }
}
JOptionPane.showMessageDialog(null, output);

请提供更多信息。


0
投票
   String output = "";  
   for (int i = 0; i < NumberOfSlot; i++) {
        output = Math.random() * 10 <= 5 ? output+"R" : output+"L";
    }
    JOptionPane.showMessageDialog(null, output);
    //edited the answer according to op's last comment.

String
之外声明一个名为
output
forloop
,然后您可以在
conditions
内更改它。然后打开最后的
joption pane

© www.soinside.com 2019 - 2024. All rights reserved.