求助JAVA编程求:1+1⼀3+1⼀5+1⼀9+……+1⼀999

用JAVA编写程序求1+1/3+1/5+1/9+……+1/999的值貌视都还不行!
2024-12-25 08:11:14
推荐回答(3个)
回答1:

这个才是正确的
public class Test {

public static void main(String args[]) {
float sum = 0;

for (int i = 1; i <= 999; i += 2) {
sum = sum + 1 / i;
}
System.out.println(sum);
}
}

回答2:

import java.io.*;

class Test {
public static void main(String args[]) throws IOException {
int sum = 0;

for (int i = 1; i <= 999; i += 2) {
sum += i;
}
System.out.println(sum);
}
}
看看满意不!

回答3:

long sum = 0;
for(int i=1;i<1000;i++){
if(i%2!=0){
sum += 1/i;
}
System.out.println("sum=" + sum);
}