标签:str can public lse ret ext value ati col
public class MultiSum { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("请输入一个一位数"); int num = scan.nextInt(); System.out.println("请输入该数最多时的个数"); int times = scan.nextInt(); System.out.println("您输入的结果为:"); int result =sum(num,times); System.out.print(result); scan.close(); } public static int sum(int num,int times) { int result =0; String str = ""; for(int i=0;i<times;i++) { //搞个字符串,依次循环,要几个数就乘以几个1 str += "1"; int a =Integer.valueOf(str); System.out.print(num*a); if(i<times-1) { System.out.print("+"); }else { System.out.print("="); } result += num*a; } return result; } }
题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加), 几个数相加由输入控制。
标签:str can public lse ret ext value ati col
原文地址:https://www.cnblogs.com/squirrel-xie/p/11106920.html