此代码在技术上有效,但现在我需要在消息框中输出它。我尝试在每个必须显示的东西前放置一个消息框,但是它会在多个不同的消息框上输出。如何将程序输出到一个消息框中?这是我的代码
import javax.swing.JOptionPane;
public class CozaLozatest
{
public static void main(String[] args)
{
int result2=0;
String n = JOptionPane.showInputDialog("Please input an interger");
int result = Integer.parseInt(n);
for (int i=0; i<result; i++){
JOptionPane.showMessageDialog(null," ");
++result2;
if (result2 % 11 == 0)
JOptionPane.showMessageDialog(null," ");
if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
JOptionPane.showMessageDialog(null,"CozaLozaWoza");}
else if (i % 3 == 0 && i % 5 == 0) {
JOptionPane.showMessageDialog(null,"CozaLoza");
} else if (i % 5 == 0 && i % 7 == 0) {
JOptionPane.showMessageDialog(null,"LozaWoza");
} else if (i % 5 == 0) {
JOptionPane.showMessageDialog(null,"Loza");
} else if (i % 3 == 0) {
JOptionPane.showMessageDialog(null,"Coza");
} else if (i % 7 == 0) {
JOptionPane.showMessageDialog(null,"Woza");
} else {
JOptionPane.showMessageDialog(null," "+i);
}
}
}
}
此外,我的代码在没有输出框的情况下工作
import javax.swing.JOptionPane;
public class CozaLozaFinished
{
public static void main(String[] args)
{
int result2=0;
String n = JOptionPane.showInputDialog("Please input an interger");
int result = Integer.parseInt(n);
for (int i=0; i<result; i++){
System.out.print(" ");
++result2;
if (result2 % 11 == 0)
System.out.println();
if (i % 3 == 0 && i % 5 == 0 && i % 7 == 0) {
System.out.print("CozaLozaWoza");}
else if (i % 3 == 0 && i % 5 == 0) {
System.out.print("CozaLoza");
} else if (i % 5 == 0 && i % 7 == 0) {
System.out.print("LozaWoza");
} else if (i % 5 == 0) {
System.out.print("Loza");
} else if (i % 3 == 0) {
System.out.print("Coza");
} else if (i % 7 == 0) {
System.out.print("Woza");
} else {
System.out.print(" "+i);
}
}
}
}