English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Java complete list of examples
Dans ce programme, vous apprendrez à imprimer le nombre saisi par l'utilisateur en Java. Les entiers sont stockés dans la variable System.in et affichés sur l'écran via System.out.
import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { //Création d'une instance de lecteur //Entrée provenant de l'entrée standard-Clavier Scanner reader = new Scanner(System.in); System.out.print("Entrez un nombre: "); //nextInt() reads the next integer from the keyboard int number = reader.nextInt(); //println() will print the following line to the output screen System.out.println("You entered: " + number); } }
When running this program, the output is:
Enter a number: 10 You entered: 10
In this program, an object of the Scanner class reader is created to obtain input from the standard input (i.e., the keyboard).
Then, 'Enter a number' will print a prompt to provide the user with a visual prompt for the next operation.
Then, reader.nextInt() will read all the input integers from the keyboard unless it encounters a newline character \ n (Enter). Then, the input integers will be saved to the integer variable number.
If the input character is not an integer, the compiler will throw an InputMismatchException.
Finally, use the function println() to print the number to the standard output (System.out) computer screen.