在 JOptionPane 中显示循环
Displaying loop in JOptionPane
我是 Java 编程的初学者,我有一个关于 while 循环的作业。
作业是 windows 打开并显示数字 1-10。
前两个,我下来了。第三个是让用户输入一个数字 'x',下一个 window 是使用 while 循环显示 1 到 'x' 之间的所有整数。
正如我现在编码的那样,每个循环迭代都会在它自己的 window 中弹出,而不是一次全部弹出,在一个 window 中弹出。
TL;DR 我想要 1 个 window 有 10 个循环,而不是 10 个 windows 每个有 1 个循环。
他让我们做的讲义和笔记中有 JOptionPane 和 while 循环,但没有提到如何组合它们。
import javax.swing.JOptionPane;
public class Pr27
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
JOptionPane.showMessageDialog(null, "1 2 3 4 5 6 7 8 9 10");
String text;
text=JOptionPane.showInputDialog("Please enter a number: ");
int f,x;
f=0;
x=Integer.parseInt(text);
while (f<=x)
{//What am I doing wrong between here
JOptionPane.showMessageDialog(null, f);
f++;
}//and here?
}
}
我相信您希望在单个对话框中打印出 x 中小于或等于 f 的所有数字,而不是每次循环迭代时都打印出。
import javax.swing.JOptionPane;
public class Pr27
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
JOptionPane.showMessageDialog(null, "1 2 3 4 5 6 7 8 9 10");
String text;
text = JOptionPane.showInputDialog("Please enter a number: ");
int f, x;
//if you wish to loop from 1 to x then f must start at 1 and not 0 because in your loop you print out f before it increases thus it would be 0.
f = 1;
x = Integer.parseInt(text);
StringBuilder sb = new StringBuilder();
while (f <= x)
{
//rather than show a message dialog every iteration append f and a new line to a StringBuilder for later use.
sb.append(f).append("\n");
//JOptionPane.showMessageDialog(null, f);
f++;
}
//string builder has a "\n" at the end so lets get rid of it by getting a substring
String out = sb.substring(0, sb.length() - 1);
JOptionPane.showMessageDialog(null, out);
}
}
我是 Java 编程的初学者,我有一个关于 while 循环的作业。
作业是 windows 打开并显示数字 1-10。
前两个,我下来了。第三个是让用户输入一个数字 'x',下一个 window 是使用 while 循环显示 1 到 'x' 之间的所有整数。
正如我现在编码的那样,每个循环迭代都会在它自己的 window 中弹出,而不是一次全部弹出,在一个 window 中弹出。
TL;DR 我想要 1 个 window 有 10 个循环,而不是 10 个 windows 每个有 1 个循环。
他让我们做的讲义和笔记中有 JOptionPane 和 while 循环,但没有提到如何组合它们。
import javax.swing.JOptionPane;
public class Pr27
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
JOptionPane.showMessageDialog(null, "1 2 3 4 5 6 7 8 9 10");
String text;
text=JOptionPane.showInputDialog("Please enter a number: ");
int f,x;
f=0;
x=Integer.parseInt(text);
while (f<=x)
{//What am I doing wrong between here
JOptionPane.showMessageDialog(null, f);
f++;
}//and here?
}
}
我相信您希望在单个对话框中打印出 x 中小于或等于 f 的所有数字,而不是每次循环迭代时都打印出。
import javax.swing.JOptionPane;
public class Pr27
{
public static void main(String[] args)
{
JOptionPane.showMessageDialog(null, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
JOptionPane.showMessageDialog(null, "1 2 3 4 5 6 7 8 9 10");
String text;
text = JOptionPane.showInputDialog("Please enter a number: ");
int f, x;
//if you wish to loop from 1 to x then f must start at 1 and not 0 because in your loop you print out f before it increases thus it would be 0.
f = 1;
x = Integer.parseInt(text);
StringBuilder sb = new StringBuilder();
while (f <= x)
{
//rather than show a message dialog every iteration append f and a new line to a StringBuilder for later use.
sb.append(f).append("\n");
//JOptionPane.showMessageDialog(null, f);
f++;
}
//string builder has a "\n" at the end so lets get rid of it by getting a substring
String out = sb.substring(0, sb.length() - 1);
JOptionPane.showMessageDialog(null, out);
}
}