java 求1-1⼀2+1⼀3-1⼀4+1⼀5-.....

2024-11-25 11:09:45
推荐回答(1个)
回答1:

public class Test {
public static void main(String[] args) {
double num=0;
for(double i = 1;i<=3;i++){
if(i==1){
num=1;
}else if(i%2==0){
num+=(1/i);
}else{
num+=(1/i);
}
}

System.out.println(num);
}

}