当用户输入“2”为“n”时,如何获得前两个序列的总和?

时间:2016-10-26 23:38:24

标签: java

序列为:sum = 1 + 1/2 + 1/4 + 1/9 + 1/16 + 1/25+...

当我输入“2”时,它只给我1.25的总和。你如何得到它,当输入“2”时,它是加1 + 1/2?

哦,我在入门级java课程中,所以我不能使用数组或任何前进的东西。

提前致谢!

import java.util.Scanner;
public class Sum
{
    public static void main(String[] args)
    {
        //declarations
        Scanner scan = new Scanner(System.in);
        double sum = 0;
        int n;

        //input
        System.out.println("Enter n: ");
        n = scan.nextInt();

        //process
        for(int counter = 1; counter <= n; counter += 1)
        {
            sum += 1.0 / (counter*counter);
        }

        System.out.println("The sum is: " + sum);
    }
}

5 个答案:

答案 0 :(得分:0)

以下代码将解决您的问题,我使用Math.pow()来运行序列,而不是将它乘以两次。

public static void main(String[] args) throws UnknownHostException {
        //declarations
        Scanner scan = new Scanner(System.in);
        //to generate this sequence we take the first two as constants and start generating from the third onwards
        double first = 1;//first number in the sequence
        double second = 0.5;//second number in the sequence
        double sum = first+second;//adding first and second
        int n;

        //input
        System.out.println("Enter n: ");
        n = scan.nextInt();
        if(n==1){
           System.out.println("The sum is: " + first);
           return;
        }
        if(n==2){
           System.out.println("The sum is: " + sum);
           return;
        }
        //process
        for(int counter = 2; counter <n; counter += 1)
        {
            sum += 1.0 / Math.pow(counter, 2);//will be computed when you enter values when n is more than 3

        }

        System.out.println("The sum is: " + sum);
     }

答案 1 :(得分:0)

既然你说1/2必须是序列的一部分,那就这样吧。 (但这是一个奇怪的序列,我强烈建议你仔细检查一下你的教授。)我假设序列的其余部分由1 / i 2 定义。请注意,根据这些假设,序列终止于1 /(n-1) 2 而不是1 / n 2

您需要对案例n == 1n > 1进行特殊处理。一种可能性是如果sumn == 1初始化为1;如果n > 1,则将其初始化为1.5;然后将其初始化为0.然后在counter = 2开始循环并将循环终止条件更改为counter < n(而不是<=)。

答案 2 :(得分:0)

如果这是你的序列,那么你应该首先将总和设置为1.5,然后其余部分将起作用。你的序列应该是一个几何序列1/n^2我认为这是一个错误。

public static void main(String[]args) {
     Scanner scan = new Scanner(System.in);
        double sum = 1.5;
        int n;

        //input
        System.out.println("Enter n: ");
        n = scan.nextInt();
        if(n==1)
          System.out.println("The sum is: " + 1);
        //process
        for(int counter = 2; counter <n; counter++) {
            double mul = counter*counter;
            sum += 1.0/mul ;
        }

        System.out.println("The sum is: " + sum);

}

输出:

    Enter n: 
    2
    The sum is: 1.5
    Enter n: 
    3
    The sum is: 1.75

答案 3 :(得分:0)

你需要管理&#34; 1&#34;和&#34; 2&#34;作为特例。

import java.util.Scanner;
public class Sum
{
    public static void main(String[] args)
    {
    //declarations

        Scanner scan = new Scanner(System.in);
        double sum = 0;
        int n;

        //input
        System.out.println("Enter n: ");
        n = scan.nextInt();

        //process
        for(int counter = 1; counter <= n; counter += 1)
        {
          if (counter == 1)
            sum = 1;
          else if (counter == 2 )
            sum += 1.0/((counter-1)+(counter-1));
          else 
            sum += 1.0 / ((counter-1)*(counter-1)); 
        }

        System.out.println("The sum is: " + sum);
    }
}

答案 4 :(得分:-2)

根据你的for循环,生成的序列将是1 + 1/(2*2) + 1/(3*3)+ ......

因此,当您输入2 =&gt; 1+1/(2*2) = 1+0.25=1.25

否则,你的逻辑是好的。你可以实现一些例外,但是正如你提到的那样,你不熟悉Java,你会慢慢遇到它们。

快乐学习Java:)