The main error of your code is this:
System.out.println(i+"\t"+SumaSerie);
You should call the sumaSerie()
method by passing its argument to it.
Then, the logic of your program does not work, the result of series addition will always be zero, because the division between two integers when the value is less than one results in zero in Java. You must then do a casting with at least one of the operands here:
double SumaSerie = i/(i+1);
Regarding the for
loop, you can write it like this:
for(int i=1; i<=10; i++)
declaring variable i
and assigning it the value 1
at the same time. Declare int i=0;
to then change its value again when starting the loop for
is unnecessary.
On the other hand, it is important that you respect the naming convention in Java (see What is the convention for writing variables in Java? Java? ).
COMPLETE SOLUTION
public class SumaSerie {
public static double sumaSerie(int i)
{
double dblSumaSerie = (double)i/(i+1); //Casting a double de uno de los operandos
return dblSumaSerie;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("i\t SUMA");
System.out.println();
for(int i=1; i<=10; i++)
{
System.out.println(i+"\t"+sumaSerie(i)); //Llamada al método con su argumento
}
}
}
Result:
i SUMA
1 0.5
2 0.6666666666666666
3 0.75
4 0.8
5 0.8333333333333334
6 0.8571428571428571
7 0.875
8 0.8888888888888888
9 0.9
10 0.9090909090909091