初学者java小程序,求java大神帮我补充一下空缺代码!求如何计算 1-1/2+1/3-…+1/9-1/10的值?
importjava.util.*;publicclassMulAdd{publicstaticvoidmain(Stringargs[]){Scannersc=;//接...
import java.util.*;public class MulAdd { public static void main(String args[]){ Scanner sc = ;//接受输入 System.out.println("请输入您要求的长度"); int num = sc.nextInt(); int i; double sum1=0.0,sum2=0.0,sum; for ( ; ; ){//给定循环条件 //循环体 } System.out.println( );//输出计算结果 }}
展开
展开全部
import java.util.*;
public class MulAdd{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要求的长度:");
int num=sc.nextInt();
sc.close();
int i;
double sum1=0.0,sum2=0.0,sum;
for(i =1;i<=num;i++){
sum1=Math.pow(-1,i-1);
sum2=1.d/i;
sum+=sum1*sum2;
}
System.out.println(sum);
}
}
追问
sum+=sum1*sum2;
The local variable sum may not have been initialized
System.out.println(sum);
The local variable sum may not have been initialized
有错哦
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询