In order for the user to be able to set the value of the variable himself, it is necessary to be able to input values from the keyboard.
To do this, use the in
object, which is written like this:
Scanner in = new Scanner(System.in);
But for it to work, you must first import using
import java.util.Scanner
To get an integer value, use in.nextInt (); , the result of which must be assigned to a variable.
For example:
int a = in.nextInt();
For other data types, there are other methods:
- next(): reads the input line to the first space
- nextLine(): reads the entire entered line
- nextInt(): reads the entered number int
- nextDouble(): reads the entered number double
- nextBoolean(): reads value boolean
- nextByte(): reads the entered number byte
- nextFloat(): reads the entered number float
- nextShort(): сreads the entered number short