Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JOptionPane中显示循环_Java_While Loop_Joptionpane - Fatal编程技术网

Java 在JOptionPane中显示循环

Java 在JOptionPane中显示循环,java,while-loop,joptionpane,Java,While Loop,Joptionpane,我是Java编程的初学者,我有一个关于while循环的作业 任务是打开显示数字1-10的窗口 前两个,我下来了。第三个是让用户输入一个数字“x”,下一个窗口是使用while循环显示1和“x”之间的所有整数 正如我现在编写的,每个循环迭代都会在它自己的窗口中弹出,而不是在一个窗口中一次全部弹出 TL;DR我想要一个有10个循环的窗口,而不是每个有1个循环的10个窗口 JOptionPane和while循环出现在他让我们做的讲义和笔记中,但没有提到如何组合它们 import javax.swing.

我是Java编程的初学者,我有一个关于while循环的作业

任务是打开显示数字1-10的窗口

前两个,我下来了。第三个是让用户输入一个数字“x”,下一个窗口是使用while循环显示1和“x”之间的所有整数

正如我现在编写的,每个循环迭代都会在它自己的窗口中弹出,而不是在一个窗口中一次全部弹出

TL;DR我想要一个有10个循环的窗口,而不是每个有1个循环的10个窗口

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?
    }
}
import javax.swing.JOptionPane;
公共类Pr27
{
公共静态void main(字符串[]args)
{
showMessageDialog(null,“1\n2\n3\n4\n5\n6\n7\n8\n9\n10”);
showMessageDialog(null,“12345678910”);
字符串文本;
text=JOptionPane.showInputDialog(“请输入一个数字:”);
int f,x;
f=0;
x=整数.parseInt(文本);

而(f我相信您希望在单个对话框中打印出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);
    }
}
import javax.swing.JOptionPane;
公共类Pr27
{
公共静态void main(字符串[]args)
{
showMessageDialog(null,“1\n2\n3\n4\n5\n6\n7\n8\n9\n10”);
showMessageDialog(null,“12345678910”);
字符串文本;
text=JOptionPane.showInputDialog(“请输入一个数字:”);
int f,x;
//如果您希望从1循环到x,那么f必须从1开始,而不是从0开始,因为在循环中,您在f增加之前打印出f,因此它将是0。
f=1;
x=整数.parseInt(文本);
StringBuilder sb=新的StringBuilder();

while(f um,那么问题出在哪里?解释在代码块之前你在做什么。包括你的问题等待,以便你只想显示
JOptionPane.showMessageDialog(null,f)
最后?你想要的输出是什么?这正是我需要的答案。如果我能通过你的显示器找到你,我会给你一杯啤酒。如果有任何部分你不完全理解,请提问,我会试着更好地解释(你的讲师可能会问)