java编程练习题

来源:互联网 发布:数据可视化之美 编辑:程序博客网 时间:2024/06/14 02:45

 

1.求1~n的和。例如:1 + 2 + 3 + 4 + 5  = 15  . . .

public static int Sum(int n){if(n==0) return 0;return n+Sum(n-1); }


2.求数字反转 例如:12-->21;   19-->91 . . .

public static int Reverse(int n){int value=0;while(n!=0){ value=value*10+n%10;  n/=10;}return value;  }


3.数字求和例如:12 -->3 ;   19-->1;    93-->3  . . .

public static void Sum(int sum){        while(true){ if(sum<10){System.out.println(sum);break;}sum=sum/10+sum%10;     }}


 

原创粉丝点击