Java 此输出是否有逻辑错误?

Java 此输出是否有逻辑错误?,java,Java,1.7(近似值p)p可使用以下公式计算: 编写一个程序,显示4*(1-1/3+1/5-1/7+1/9-1/11)的结果 和4*(1-1/3+1/5-1/7+1/9-1/11+1/13) 在3 5 7 9 11 13中使用1.0而不是1 节目 我的代码: /** * This program will print two different results in * trying to reach a approximation of pi. * * Author: X Date: 11/11

1.7(近似值p)p
可使用以下公式计算:

编写一个程序,显示
4*(1-1/3+1/5-1/7+1/9-1/11)的结果

4*(1-1/3+1/5-1/7+1/9-1/11+1/13)

3 5 7 9 11 13中使用
1.0
而不是
1
节目

我的代码:

/**
* This program will print two different results in
* trying to reach a approximation of pi.
*
* Author: X   Date: 11/11/2013
*/
public class one_seven
{
    public static void main(String[] args)
    {
        System.out.print("This will print 1st approximation: ");
        System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)));
        System.out.print("This will print 2nd approximation: ");
        System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)+(1.0/13)));
    }
}
我的代码输出是:

This will print 1st approximation: 2.531601731601732
This will print 2nd approximation: 2.83929403929404

您缺少
1.0/9
术语

 System.out.print("This will print 1st approximation: ");
 System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));
 System.out.print("This will print 2nd approximation: ");
 System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13)));

在这两个近似值中,您都忽略了+(1.0/9.0)

如果我加上,那么结果是否正确?只是想确定这是否是公式的正确编码风格,不会出现逻辑错误?@user2522055:试试看是否有效。我不认为您有逻辑错误,除此之外它似乎是正确的。@user2522055-为什么不尝试一下并找出原因呢?你说的“逻辑错误”是什么意思?你得到了精确的公式,不需要做任何事情,只需要将它复制到你的程序中……只是想确保我编写print语句来计算公式的方式不会产生逻辑错误,因为它产生了错误的答案(意味着1.0/9在其中)。我刚刚开始学习Java,我想确保我理解了实现它的正确方法,这样我就不会被优先级的概念所迷惑,并确保我正确地添加了括号,因为我无法得到逻辑错误。一个错误,我的答案不是预期的@jahroyThank your help@pearsonatortphotoi如果我没有遗漏这一点,我的编码风格是否正确,可以在没有逻辑错误的情况下得到正确答案?这个问题似乎离题了,因为它没有用处,只不过是一个复制/粘贴错误。OP得到了一个近似pi的方程,但未能正确地重新写入方程。