-2

Hi im currently learning java and my teacher gave me an exercise to create a calculator wich will give you the fibonacci number you gave into the console. I already have created my fibonacci calculator but im not sure how to do a user input wich can be used as a variable. Can you help me or give me any advice ?

public class FibonacciRechner {
    public static void main (String [] args){

        int x = 0;
        int erg1;
        int erg2;

        erg1 = 1;
        erg2 = 2;

        System.out.println(erg1);
        System.out.println(erg2);

        while(x < 5){                       
            erg1 = erg2 + erg1;
            System.out.println(erg1);
            erg2= erg1 + erg2;
            System.out.println(erg2);
            x++;

        }   
    }
}
J. E
  • 1
  • In java you can take userInput in this way: Scanner input = new Scanner(System.in); x = input.nextInt();-> for integer type input – Omore Mar 25 '17 at 08:27

1 Answers1

-1

If you want to take input from user, you have to import Scanner class.

import java.util.Scanner;

Then, make an instance of Scanner class and pass System.in in its Constructor.

Scanner sc = new Scanner(System.in);

Then create a variable and take input. Like this:

int x = sc.nextInt();

It will be stored in x variable. You can also use command line arguments and BufferedReader to take inputs. But this method this better.

Gaurav
  • 83
  • 8