计算Pi Java程序 [英] Calculating Pi Java Program

查看:211
本文介绍了计算Pi Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上第一堂Java编程课,这是我的第一堂课.我对如何处理感到困惑.任何帮助或纠正将不胜感激.

I'm taking my first Java programming class and this is my first class project. I'm so confused about how to approach it. Any help or correction will be appreciated.

您可以通过使用以下序列来近似常量PI的值:

You can approximate the value of the constant PI by using the following series:

PI = 4 ( 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + ( (-1)^(i+1) )/ (2i - 1) )

提示用户输入i的值(换言之,使用该系列中的多少个术语) 计算PI.例如,如果用户输入10000,则求和该系列的前10,000个元素,然后显示结果.

Prompt the user for the value of i (in other words, how many terms in this series to use) to calculate PI. For example, if the user enters 10000, sum the first 10,000 elements of the series and then display the result.

除了显示最终结果(您的PI的最终近似值)外,我还希望您沿中间结果以每10的幂次计算的方式显示.因此,将10、100、1000、10000等以此类推.筛选出PI近似于该数量的求和元素.

In addition to displaying the final result (your final approximation of PI), I want you to display along the way your intermediate calculates at every power of 10. So 10, 100, 1000, 10000 and so on, display to the screen the approximation of PI at that number of summed elements.

这是我到目前为止所做的..

This is what i did so far ..

import java.util.Scanner;
        public class CalculatePI {

            public static void main(String[] args) {
                // Create a Scanner object
                Scanner input = new Scanner (System.in);

                // Prompt the user to enter input
                System.out.println("Enter number of terms");
                double i = input.nextDouble(); // value of i user entered
                    double sum = 0;
                for(i=0; i<10000; i++){
                           if(i%2 == 0) // if the remainder of `i/2` is 0
                           sum += -1 / ( 2 * i - 1);
                        else
                           sum += 1 / (2 * i - 1);
    }

                        System.out.println(sum);


    }
}

推荐答案

我首先看到的是您尝试从void main方法返回值.

First thing I see is you attempting to return a value from your void main method.

不要从您的主要方法中选择return pi;.打印.

don't return pi; from your main method. Print it.

System.out.println(pi);


第二,当人们编写一个for循环时,他们通常在i上进行迭代,这可能与您的教授在公式中提到的i相同.


Secondly, when people write a for loop, they're commonly iterating over i, which is probably the same i that your professor referred to in the formula

for(double i=0; i<SomeNumber; i++)
{
    sum += ((-1)^(i+1)) / (2 * i - 1) );
}

现在,它不能按原样正常工作,您仍然必须处理^,这不是Java本身不使用的.幸运的是,-1^(i+1)是一个交替数字,因此您只需使用if语句

now, that won't work correctly as is, you still have to handle the ^, which java doesn't use natively. luckily for you, -1^(i+1) is an alternating number, so you can just use an if statement

for(double i=0; i<SomeNumber; i++)
{
    if(i%2 == 0) // if the remainder of `i/2` is 0
        sum += -1 / ( 2 * i - 1);
    else
        sum += 1 / (2 * i - 1);
}

这篇关于计算Pi Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆