一个小于4位的整数,求它是几位数,并输出各位数字. 请问,如何编写?谢谢

解决方案 »

  1.   

    import java.util.*;public class Count {
      public static void main(String args[]){
        int max = 1000;
        Vector vals = new Vector();
        int num = 1234;
        int count = 0;
        for(; max>=1; max/=10){
          if(num/max>0){
            vals.add(new Integer(num/max));
            num -= max*(num/max);
          }
        }
        
        System.out.println("各位数字分别为:");
        int index = 0;
        while(index < vals.size()){
          System.out.print(""+(Integer)vals.elementAt(index++)+"\t");
        }
      }
    }
      

  2.   

    import java.util.*;public class Count {
      public static void main(String args[]){
        int max = 1000;
        Vector vals = new Vector();
        int num = 1604;
        int count = 0;
        
        while(num/max<=0)
          max /= 10;
          
        while(max>=1){
          vals.add(new Integer(num/max));
          num -= max*(num/max);
          max /=10;
        }
        
        System.out.println("各位数字分别为:");
        int index = 0;
        while(index < vals.size()){
          System.out.print(""+(Integer)vals.elementAt(index++)+"\t");
        }
      }
    }
      

  3.   

    这个更简单一点:public class Count {
      public static void main(String args[]){
        int num = 120365887;
        String str = new String(""+num);
        System.out.println("共有"+str.length()+"位,分别为: ");
        for(int i=0; i<str.length(); i++){
          System.out.print(str.charAt(i)+"\t");
        }
      }
    }
      

  4.   

    public void display(int num)
    {
      String temp=String.valueOf(num);
      System.out.println("位数:");
      System.out.println(temp.lenth());
      System.out.println("各个数字:");
      for(int i=0;i<temp.lemgth();i++)
        System.out.print(temp.charAt(i))
      System.out.println();
    }
      

  5.   

    import javax.swing.*;
    public class TT { public static void main(String[] args) {
    String s= JOptionPane.showInputDialog("请输入一个数");

    for(int j=0;j<s.length();j++)
    {
    System.out.print(s.charAt(j)+" ");
    }
    System.out.println();
    System.out.println("这是一个"+s.length()+"位数");


    }
    }
      

  6.   

    哈哈,来个像C的。
    好简单哦!
    public class a
    {
    public static void main(String [] args)
    {
    int i = 1234, temp = 1000;
    while(i%temp == 0)
    temp /= 10;

    while(temp!=0)
    {
    System.out.println(i/temp);
    i %= temp;
    temp /= 10;
    }
    }
    }
      

  7.   

    不好意思,有个地方写错了!
    while(i/temp == 0)  // % -> /
    temp /= 10;
      

  8.   

    public class a {
    public static void main(String [] args) {
    int i = 234, temp = 1000;
    while(i/temp == 0) temp /= 10;
    while(temp!=0) {
    System.out.println(i/temp);
    i %= temp; temp /= 10;
    }
    }
    }
      

  9.   

    import java.util.*;public class tmp{
      public static void main(String args[]){
        int i=1234;
        Stack st;
        if (i==0) System.out.println("it is zero.");
        else {
          while (i>0) 
            st.push(i%10);
          System.out.println(st.size());//输出位数
          while (!st.empty())
            System.out.print(st.pop());
        }
      }
    }就我的程序说说以上几位程序的不足之处:
    1、字符串做法,如果输入的时候是0012,程序就要出错
    2、取余做法,如果这个是就是0,那他也是1位数,而楼上的程序都没有考虑到这一点。
      

  10.   

    昏,漏了句:
       while (i>0){
         st.push(i%10);
         i=i/10;//漏掉的
       }
      

  11.   

    String a = String.valueOf(yournumber);
    System.out.println(a.length);
    char[] b = a.toCharArray();
    for(int i = 0;i<b.size();i++)
    System.out.println(b[i]);