求s = a + aa + aaa + aaaa + aa...a的值,其中a是一个数字。例如2 + 22 + 222 + 2222 + 22222(此时共有5个数相加),几个数相加有键盘控制。
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入每位数字");
int n = sc.nextInt();
System.out.println("输入位数");
int m = sc.nextInt();
int num = 0;
int sum = 0;
StringBuilder sb = new StringBuilder();
for(int i = 1;i <= m;i++){
num = num * 10 + n;
sb.append(num);
if(i == m){
sb.append("=");
}else{
sb.append("+");
}
sum += num;
}
sb.append(sum);
System.out.println(sb);
}