Java计算s=a+aa+aaa+aa...aaa

public class Adda {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
         @SuppressWarnings("resource")
        Scanner in = new Scanner(System.in);
          System.out.println("请输入一个数");
          int number = in.nextInt();
          System.out.println("请输入加几次");
          int count = in.nextInt();
          long sum = 0;
          String plusStr = "";
          for(int i = 1; i <= count; i++) {
           if(i == 1) {
            sum += number;
            plusStr += number + "+";
           }
           else {
            String str = "";
            for(int j = 1; j <= i; j++) {
             str += number;
            }
            sum += Long.parseLong(str);
            plusStr += str + "+";
           }
          }
          plusStr = plusStr.substring(0, plusStr.length() - 1);
          System.out.println(plusStr + "=" + sum);
    }

}

你可能感兴趣的:(Java)