今天面试面试官叫我当场写a+aa+aaa+aaaa......java实现,现在完善一下。

package publicpp.demo;


import java.util.Scanner;


public class SumNumbers
{
public SumNumbers(int num,int count)
{
int[] r=getNumbers(num,count);
System.out.println(getString(r)+ "=" + callResult(r));
}

public int[] getNumbers(int num,int count)
{
int [] numbers=new int[count];
int temp=0;
for(int i=0;i {
for(int j=0;j {
int a=10;
temp+=num*Math.pow(a, j);
}
numbers[i]=temp;
temp=0;
}
return numbers;

}

public int callResult(int[] numbers)
{
int result=0;
for(int i=0;i {
result +=numbers[i];
}
return result;
}

public String getString(int [] numbers)
{
String temp="";
String result;
int len = numbers.length;
for(int i=len-1;i>=0;i--)
{
temp+=numbers[i]+"+";

}
result=temp.substring(0,temp.length()-1);
return result;
}

public static void main(String[] args)
{
System.out.println("请输入相加数字的个数:");
Scanner count =new Scanner(System.in);//输入个数
int incount=count.nextInt();
System.out.println("请输入要计算的数字:");
Scanner num =new Scanner(System.in);//输入数字
int innum=num.nextInt();

new SumNumbers(innum,incount);
}
}

你可能感兴趣的:(IT技术类,面试,a+aa+aaa+aaaa)