My book I'm learning with uses another libary for reading Inputs so it can't help me....
I can't see where my mistake is. The algorithm:
- Read the value of n
- Set the value of i to 3
- Follow the steps
Iterate
While i < 2*n
i+1
Write 1/(2*i+1) to the console.
My code:
import java.util.Scanner;
public class Aufgabe420 {
public static void main (String[] args) {
int i, n;
System.out.println("Please enter a number!");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
System.out.println("n ="+n);
System.out.println("The while-loop starts!");
i = 3;
while (i < 2*n){
i += 1;
System.out.println(1/(2*i+1));
}
System.out.println("now with for-loop");
for (i = 3; i < (2*n); i+=1) {
System.out.println(1/(2*i+1));
}
}
}
But trying it, it results in: Please enter a number! 5
n =5 The while-loop starts! 0 0 0 0 0 0 0
now with for-loop 0 0 0 0 0 0 0
What's wrong with that code? Thanks for your help.