简体   繁体   中英

Splitting the input number in Java

Hi I have a problem with my code I want the input number. Instead of showing the numbers in one single dialog it displays the number per dialog take a look at the code.

import javax.swing.JOptionPane;

public class Vector_number {

    public static void main(String[] args) {
        String x;
        int i = 0;  
        int number; 
        int[] y;
        y = new int[10];

        x = JOptionPane.showInputDialog("Enter integer: ");

        number = Integer.parseInt(x);
        String myStr = " ";

        while (number > 0)  {           
            y[i] = number%10;           
            number = number/10;     
            i++;    
        }

        for (i = i-1; i >= 0 ; i--) {       
            JOptionPane.showMessageDialog(null, y[i]+ " ", 
                        "Weeeee", JOptionPane.PLAIN_MESSAGE);
            System.exit(0);
        }
    }
}

Sure. You call dialog in for loop.

Also, don't use System.exit at all.

first build the string then show the dialog

StringBuilder str = new StringBuilder();
for (i = i-1; i >= 0 ; i--) {
      str .append( y[i]).append(" ");
} 
JOptionPane.showMessageDialog(null, str.toString, "Weeeee", JOptionPane.PLAIN_MESSAGE); 

Instead of displaying 'i' option panes, gather all the numbers from the array first and then show them in a single pane. try

`for(i=i-1;i>=0;i--){
  myStr+=" "+y[i];
 }
 JOptionPane.showMessageDialog(null, myStr,...`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM