Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.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 在while循环中显示数字_Java_Variables_While Loop_Joptionpane - Fatal编程技术网

Java 在while循环中显示数字

Java 在while循环中显示数字,java,variables,while-loop,joptionpane,Java,Variables,While Loop,Joptionpane,我正在尝试创建一个程序,用户在其中输入起始号码和结束号码。程序将打印包括开始和结束之间的所有值,并在消息对话框屏幕上显示所有值。但是我不能让他们在一个消息对话框中显示所有的数字,它会为每个数字创建一个新的数字 public static void main(String[] args) { int start; int end; int result; start = Integer.parseInt(JOptionPane.showInputDialog("P

我正在尝试创建一个程序,用户在其中输入起始号码和结束号码。程序将打印包括开始和结束之间的所有值,并在消息对话框屏幕上显示所有值。但是我不能让他们在一个消息对话框中显示所有的数字,它会为每个数字创建一个新的数字

public static void main(String[] args) {

    int start;
    int end;
    int result;
    start = Integer.parseInt(JOptionPane.showInputDialog("Please enter a starting integer "));
    end = Integer.parseInt(JOptionPane.showInputDialog("Please enter an ending integer "));
    while (start <= end) {

        result = start;
        start++;

        JOptionPane.showMessageDialog(null,result);
    }
}
publicstaticvoidmain(字符串[]args){
int启动;
内端;
int结果;
start=Integer.parseInt(JOptionPane.showInputDialog(“请输入起始整数”);
end=Integer.parseInt(JOptionPane.showInputDialog(“请输入一个结束整数”);

while(start循环在每次迭代期间调用
showMessageDialog()
。您希望循环将每个数字附加到字符串中,然后在完成后,在对话框中显示整个字符串

public static void main(String[] args) {

  int start;
  int end;
  int result;

  start = Integer.parseInt(JOptionPane.showInputDialog(
      "Please enter a starting integer "));
  end = Integer.parseInt(JOptionPane.showInputDialog(
      "Please enter an ending integer "));

  String msg = "";
  while (start <= end) {
    msg = msg + " " + start;
    start++;
  }

  JOptionPane.showMessageDialog(null, msg);
}

循环在每次迭代期间调用
showMessageDialog()
。您希望循环将每个数字附加到字符串中,然后在完成后,在对话框中显示整个字符串

public static void main(String[] args) {

  int start;
  int end;
  int result;

  start = Integer.parseInt(JOptionPane.showInputDialog(
      "Please enter a starting integer "));
  end = Integer.parseInt(JOptionPane.showInputDialog(
      "Please enter an ending integer "));

  String msg = "";
  while (start <= end) {
    msg = msg + " " + start;
    start++;
  }

  JOptionPane.showMessageDialog(null, msg);
}

这是因为您正在调用
showMessageDialog()
在循环的每次迭代中。您希望循环将每个数字附加到一个字符串中,然后在完成后,在对话框中显示整个字符串。谢谢!我将如何继续执行此操作?是否必须为每个数字分配一个变量,并在最后打印所有变量?@adamliss这是因为您正在调用
showMessageDialog()
在循环的每次迭代中。您希望循环将每个数字附加到一个字符串中,然后在完成后,在对话框中显示整个字符串。谢谢!我该如何继续执行此操作?是否必须为每个数字分配一个变量,并在最后打印所有变量?@AdamLissI尝试添加字符串消息,我收到错误消息“局部变量msg可能尚未初始化”抱歉;您需要初始化字符串。已更新。我尝试添加字符串msg我收到错误“局部变量msg可能尚未初始化”抱歉;您需要初始化字符串。已更新。