java基础练习 给定一个正整数m,统计m的位数,分别打印每一位数字,再按照逆序打印出各位数字。

要求:m定义为类的属性,需定义构造函数为m赋值;当m大于99999时,输出错误信息“the number is too large”,不再执行。

 

public class T {
    private int m;

    public T(int m) {
        super();
        this.m = m;
    }

    public int getM() {
        return m;
    }

    public void setM(int m) {
        this.m = m;
    }
    public void M()
    {
        if(m>99999)
        {
            System.out.println("the number is too large");
        }
        else
        {
            String str=m+"";
            System.out.println("一共有"+str.length()+"位");
            System.out.println("每位数字是:");
            for(int i=0;i<str.length();i++)
            {
                System.out.print(str.charAt(i)+" ");
            }
            System.out.println("\n逆序");
            for(int i=str.length()-1;i>=0;i--)
            {
                System.out.print(str.charAt(i));
            }
            
        }
    }
    public static void main(String[] args)
    {
        T m=new T(85412);
        m.M();
        
    }

}

 

posted on 2016-05-24 08:59  煜渝  阅读(3765)  评论(0编辑  收藏  举报

导航