函数调用:同一个类当中,一个函数内要直接调用另一个函数,被调用函数必须用 static 修饰。
编程求下列表达式的和:
package one;
public class One_6 {
static long sum(int n) {
long result=0;
for(int i=1; i<=n; i++) {
result += n;
}
return result;
}
static long sumOfSquare(int n) {
long result=0;
for(int i=1; i<=n; i++) {
result +=n*n;
}
return result;
}
static double sumOfReciprocal(int n) {
double result=0;
for(int i=1; i<=n; i++) {
result +=(1.0/n);
}
return result;
}
public static void main(String[] args) {
double result;
result = sum(100) + sumOfSquare(50) + sumOfReciprocal(10);
System.out.println("result=" + result);
}
}