代码:
package bao; import java.util.Random; public class a { public static void main(String[] args) { int s=0,n=0; Random d=new Random(); int x=(d.nextInt(10))+1; //x为随机数 System.out.println("随机数="+x); for(int i=0;i<=x;i++) { //10的i次方 int m=(int)Math.pow(10, i); //m为10的i次方 //相加 n+=x*m; //n为当前数字 int a=s; //a为之前相加结果 s+=n; //s为加上当前数字之后的结果 System.out.println("当前数字:"+n); System.out.println(a+"+"+n+"="+s); System.out.println("--------------------------"); } System.out.println(s); }}
结果:
随机数=10 当前数字:10 0+10=10 -------------------------- 当前数字:110 10+110=120 -------------------------- 当前数字:1110 120+1110=1230 -------------------------- 当前数字:11110 1230+11110=12340 -------------------------- 当前数字:111110 12340+111110=123450 -------------------------- 当前数字:1111110 123450+1111110=1234560 -------------------------- 当前数字:11111110 1234560+11111110=12345670 -------------------------- 当前数字:111111110 12345670+111111110=123456780 -------------------------- 当前数字:1111111110 123456780+1111111110=1234567890 -------------------------- 当前数字:-1773790778 1234567890+-1773790778=-539222888 -------------------------- 当前数字:-1773790788 -539222888+-1773790788=1981953620 -------------------------- 1981953620