1、
public class Test1{
public static void main(String [] args){
/* int sum=0;
for(int i=0;i<=100;i++){
if(i%2!=0){
sum+=i*i;
}
}
System.out.println("求12+32+52+...+992之和是:"+sum); */
int sum=0;
for(int i=1;i<100;i+=2){
sum+=i*i;
}
System.out.println(sum);
}}
2、
public class Test2{
public static void main(String [] args){
/* double sum=0;
for(int i=0;i<=40;i++){
if(i%2!=0){
sum=sum+1.0/i;
}
}
System.out.println("1+1/3+1/5+...的前20项之和是:"+sum); */
double sum = 0;
for(double i=1;i<=40;i=i+2){
sum = sum +1/i;
}
System.out.println("sum:"+sum);
}}
时间: 2024-08-06 19:45:57