作业帮 > 综合 > 作业

Java write a program to calculate the sum of each of the ser

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/12 02:26:28
Java write a program to calculate the sum of each of the series:(a)1+4+9+16+...to 20terms.
Java write a program to calculate the sum of each of the ser
import java.util.Scanner;
public class Sum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// 创建一个输入容器
Scanner input = new Scanner(System.in);
System.out.println("输入一个开始数:");
int start = input.nextInt();
System.out.println("输入想计算的个数:");
int end = input.nextInt();
Sum s = new Sum();
int sum = s.sum(start,end);
System.out.println("答案是:" + sum);
}
public int sum(int start,int end) {
int sum = 0;
int sum2 = 0;
for (int i = 0; i < end; i++) {
sum = sum + 1 + 2 * i;
sum2+=sum;
}
return sum2;
}
}