编程函数实现s =1 +2∧2 +3∧3 ……+n ∧n,其中n 从键盘输入,在main ()中

用该函数

//java实现
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("请输入要统计的数:");
int num = s.nextInt();
int total = count(num);
System.out.println("统计结果:"+total);
}

/**
 * 计算1+2^2+3^3+4^4+....num*num
 * @param num
 * @return 统计之和
 */
public static int count(int num) {
int total = 0;
if (num > 0) {
for (int i = 1; i <= num; i++) {
total += i * i;
}
}
return total;
}
温馨提示:答案为网友推荐,仅供参考
相似回答