题目:求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。
import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; String output=""; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入a的值"); String input =stadin.readLine(); for(int i =1;i<=Integer.parseInt(input);i++) { output+=input; int a=Integer.parseInt(output); s+=a; } System.out.println(s); } } 另解: import java.io.*; public class Sumloop { public static void main(String[] args) throws IOException { int s=0; int n; int t=0; BufferedReader stadin = new BufferedReader(new InputStreamReader(System.in)); String input = stadin.readLine(); n=Integer.parseInt(input); for(int i=1;i<=n;i++){ t=t*10+n; s=s+t; System.out.println(t); } System.out.println(s); } }