Using the Scanner Class

Descripción

Diapositivas sobre Using the Scanner Class, creado por Shannon Anderson-Rush el 07/09/2019.
Shannon Anderson-Rush
Diapositivas por Shannon Anderson-Rush, actualizado hace más de 1 año
Shannon Anderson-Rush
Creado por Shannon Anderson-Rush hace alrededor de 5 años
2068
0

Resumen del Recurso

Diapositiva 1

    To make Java programs interactive, you will want to ask and receive input from the user.  Java has a class that accepts user input called Scanner. Before you can use the Scanner class in a program, you must import it. To do that, you code an import statement at the beginning of the program, before the class declaration: import java.util.Scanner; Note that java and util aren’t capitalized, but Scanner is.

Diapositiva 2

    Before you can use the Scanner class to read input from the console, you must declare a Scanner variable and create an instance of the Scanner class. You might want to create the Scanner variable as a class variable and create the Scanner object in the class variable initializer: Scanner scan = new Scanner(System.in);   To read an input value from the user, you can use one of the methods of the Scanner class.

Diapositiva 3

    Scanner Class Methods
    Scanner Class Methods That Get Input Values Method                                                                Explanation boolean nextBoolean()                           Reads a boolean value from the user. double nextDouble()                                Reads a double value from the user. int nextInt()                                                   Reads an int value from the user. String nextLine()                                       Reads a String value from the user. char nextChar()                                          Reads a char value from the user.

Diapositiva 4

    Notice in the first column of the table that each method listing begins with the type of the value that’s returned by the method. The nextInt method, for example, returns an int value. Also, notice that each of the methods ends with an empty set of parentheses. That means that these methods don’t require parameters. If a method does require parameters, the parameters are listed within these parentheses.

Diapositiva 5

    Because these methods read a value from the user and return the value, you most often use them in statements that assign the value to a variable. To store the input from the user, you would use a System.out.print to prompt the user.  For example: System.out.print(“How old are you?);   Then you would capture the input into a variable: int age = scan.nextInt();   Note the age variable could have been declared separately at the top of the program, or in combination with the initialization as shown in this example.
Mostrar resumen completo Ocultar resumen completo

Similar

Todos los Países del Mundo y sus Capitales
maya velasquez
Países de Latinoamérica y sus Capitales
maya velasquez
Tablas de multiplicar
Carmen Giralda
Modelo de Examen de Inglés - Selectividad
juanmadj
Prepara la Selectividad
Diego Santos
MAPA CONCEPTUAL DE POLITICAS PUBLICAS
pamm_1805
LA BELLE EPOQUE
fer.sagredo
CARBOHIDRATOS
Andrea Lopez Riv
FARMACOLOGÍA CARDIOVASCULAR
may.856
Cuadro Sinóptico Educación Mundial
JANET ALVARADO
Mapa mental de Responsabilidad Social Empresarial RSE
Rosmunda Pierri