在Java中,您可以将多个对话框压缩为一个

时间:2015-04-03 23:44:54

标签: java loops dialog

有没有办法将多个对话框变成一个?

import javax.swing.JOptionPane;
public class loop {
public static void main(String args[]) {

    int input;
    int sum = 0;
    int num1 = 0;
    int counter = 1;
    String num = "";


    input=(Integer.parseInt(JOptionPane.showInputDialog ("Enter an integer")));


    if (input == (-input)) {

        input = input * (-1);
        num = String.valueOf(input);
        num1 = num.length();
        JOptionPane.showMessageDialog(null,"the digits of " + input + " are: ");

        for (int i = 0; i < num1; i++) {
            String var = num.substring(i, counter);
            int var1 = Character.getNumericValue(var.charAt(0));
            JOptionPane.showMessageDialog(null, var + " ");
            sum = sum + var1;
            counter++;
        }

        JOptionPane.showMessageDialog(null,"the sum is: " + sum);

    } else {
        num = String.valueOf(input);
        num1 = num.length();
       JOptionPane.showMessageDialog(null, "the digits of " + input + " are: ");

        for (int i = 0; i < num1; i++) {
            String var = num.substring(i, counter);
            int var1 = Character.getNumericValue(var.charAt(0));
           JOptionPane.showMessageDialog(null,var + " ");
            sum = sum + var1;
            counter++;
        }

        JOptionPane.showMessageDialog(null, "the sum is: " + sum);
    }
}
}

我必须编写一个程序,用户输入一个整数,程序将输出分解的数字和总和。使用上面的代码,我可以运行该程序但是每个分解的整数都有自己的对话框,是否可以让它输出一个对话框而不是多个?

import javax.swing.JOptionPane; 

 public class loop{
 public static void main(String []args){


  {

  long a,b,test;
  long counter = 0;


     a=(Long.parseLong(JOptionPane.showInputDialog ("Enter an integer")));
     test = a;
     while (test > 0)

     {
     test  = test/10;
     counter = counter + 1;
     }
     counter = counter - 1;


     while (counter >= 0)
     {  
     b = a% (long) Math.pow(10, counter);
     a = a/(long) Math.pow(10,counter);
     a = b;
     counter = counter - 1;

    {
    String longString=String.valueOf(a);
    JOptionPane.showMessageDialog(null, a + " ");
    }
   }
   }
   }
   }

我开始采用不同的方式,但现在它缩小了一些数字并输出了一个从未输入过的数字。

1 个答案:

答案 0 :(得分:1)

不确定。不要在for循环中调用JOptionPane.showMessageDialog()。 将值分配给循环外的String,然后在循环完成后,调用JOptionPane.showMessageDialog()传递String。