编写一个程序来计算 (a/1)+(a/2)+(a/3)+(a/4)+......... +(a/n)

Write a Program that calculates the sum of (a/1)+(a/2)+(a/3)+(a/4)+..........+(a/n)

我正在尝试解决此程序以打印以下总和:(a/1)+(a/2)+(a/3)+(a/4 )+..........+(a/n) 其中 a 由用户输入,n 的限制也由用户输入这是我试过的程序:

/**
 * Program to 
 * 
 * Anirudh Gupta
 * th August 2014
 */
import java.io.*;
public class Program87b
{
   public static void main () throws IOException
   {
       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(isr);
       System.out.println("Enter the value of the numerator");
       double a=Double.parseDouble(br.readLine());
       System.out.println("Enter the limit for the denominator");
       int limit=Integer.parseInt(br.readLine());
       double n=1.0;
       double sum=0.0;
       while(n<=limit)
       {
           sum=+(a/n);
           n++;
       }
       System.out.println(sum);
   }
}

但是当我输入 a=4 和 n=5 我得到 0.8 这只是 (4/5) 的答案而不是 (4/1)+(4/2)+(4/ 3)+(4/4)+(4/5) 应该是 9.1333333333...

将 =+ 更改为 += 我不确定变量 'd' 的来源,但我认为它应该替换为 'n'

public static void main () throws IOException
   {
       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(isr);

       System.out.println("Enter the value of the numerator");
       double a=Double.parseDouble(br.readLine());

       System.out.println("Enter the limit for the denominator");
       int limit=Integer.parseInt(br.readLine());

       double n=1.0;
       double sum=0.0;

       while(n<=limit)
       {
           sum+=(a/n);
           n++;
       }

       System.out.println(sum);
   }

这给出了您预期的输出:

import java.io.*;
public class Sum
{
   public static void main(String[] args) throws IOException
   {
       InputStreamReader isr=new InputStreamReader(System.in);
       BufferedReader br= new BufferedReader(isr);
       System.out.println("Enter the value of the numerator");
       double a=Double.parseDouble(br.readLine());
       System.out.println("Enter the limit for the denominator");
       int limit=Integer.parseInt(br.readLine());
       double n=1.0;
       double sum=0.0;
       while(n<=limit)
       {
           sum+=(a/n);
           n++;
       }
       System.out.println(sum);
   }
}

您的错误来自这一行 sum =+ (a/n);,它应该是 sum += (a/n);。此外,您还有一些不需要的额外变量,因此我会将您的代码修改为类似于以下内容:

public static void main () throws IOException {
     BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

     System.out.println("Enter the value of the numerator");
     double a=Double.parseDouble(br.readLine());

     System.out.println("Enter the limit for the denominator");
     int limit=Integer.parseInt(br.readLine());

     double sum = 0.0;
     for (int i = 1; i <= limit; i++) {
          sum += a / i;
     }

     System.out.println(sum);
}

注意:如果你想让代码更具可读性,你也可以使用 java.util.Scanner class 而不是 java.io.BuferedReader

 public static void main () {

         Scanner in = new Scanner(System.in);

         System.out.println("Enter the value of the numerator");
         double a = in.nextDouble();

         System.out.println("Enter the limit for the denominator");
         int limit= in.nextInt();

         double sum = 0.0;
         for (int i = 1; i <= limit; i++) {
              sum += a / i;
         }

         System.out.println(sum);
    }

如果准确性不是问题的话,您的序列看起来很像 Harmonic Series a*Hn You may want to use the approximation