I need to write a method where the parameter accepts an integer (n in this case) and returns the sum of the first n terms of the sequence as a double value. So say I put fractionSums(5); the output would be
1+…+1/5 which would then equal to something like 2.8333~.(final result)
what I have now is:
public static void main(String[] args) {
fractionSums(5);
}
public static void fractionSums(int n) {
Scanner console = new Scanner(System.in);
System.out.print("How many terms do you have? ");
int totalterms = console.nextInt();
}
I’m stuck here and don’t know how to go further than this and where to implement n. How would you recommend me on proceeding with this. Do I have to edit this question in anyway?
Try placing the scanner in
mainandprintlninmain. You should create a method (you’ve called itfractionSums()here ) and invoke it to calculate the fraction sum and return a double (pass the int n to the method). That method can be either recursive or iterative, but since you say “I need to write a method where the parameter accepts an integer (n in this case) and returns the sum of the first n terms of the sequence,” you should make the method return something (either a float or a double).HTH 🙂