当我的输出由于我的方法中的
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();
不完整,因为我不知道如何显示输出
你的代码很不清楚(你想显示一堆joptionpanes吗?),但这可能就是你想要的:
String output = "";
for (int x = 0; x < NumberOfSlot; x++)
{
if (Math.random() * 10 <= 5) {
output+="R";
} else {
output+="l";
}
}
JOptionPane.showMessageDialog(null, output);
请提供更多信息。
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
。