62,614
社区成员
发帖
与我相关
我的任务
分享
class Test
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入正整数!");
int param = 0;
double result = 0;
try{
param = scanner.nextInt();
if(param <= 0)
throw new Exception();
}catch (Exception e) {
System.out.println("输入非法,程序退出!");
}
for(int n = 1;n <= param;++n)
{
result += Math.pow(-1,n-1)*1.0/n;
}
System.out.println(result);
}
}