zoukankan      html  css  js  c++  java
  • 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("
    逆序");
                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();
            
        }
    
    }
  • 相关阅读:
    第36课 经典问题解析三
    第35课 函数对象分析
    67. Add Binary
    66. Plus One
    58. Length of Last Word
    53. Maximum Subarray
    38. Count and Say
    35. Search Insert Position
    28. Implement strStr()
    27. Remove Element
  • 原文地址:https://www.cnblogs.com/wallan/p/5522230.html
Copyright © 2011-2022 走看看