【问题标题】:In Java can you condense multiple dialog boxes into just one在Java中,您可以将多个对话框压缩成一个吗
【发布时间】:2015-04-03 23:44:53
【问题描述】:

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

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 + " ");
    }
   }
   }
   }
   }

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

【问题讨论】:

    标签: java loops dialog


    【解决方案1】:

    当然。不要在 for 循环中调用 JOptionPane.showMessageDialog()。 将值分配到循环外的字符串中,然后在循环完成后,调用JOptionPane.showMessageDialog() 传递字符串。

    【讨论】:

    • 感谢您的回复,我想我错过了一些简单的东西,或者过于复杂。但是,此时我无法正确获取字符串。如果你有时间可以给我举个例子吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-22
    • 1970-01-01
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多