Ejemplo 1: escáner java
import java.util.Scanner;
// import scanner
Scanner myScanner = new Scanner(System.in); // Make scanner obj
String inputString = myScanner.nextLine(); // Take whole line
boolean inputBoolean = myScanner.nextBoolean(); //Boolean input
long inputLong = myScanner.nextLong(); //Interger,long ... input
Ejemplo 2: escáner en java
import java.util.Scanner; //Import Scanner in java
class classname{
public void methodname(){
Scanner s_name = new Scanner(System.in); //Scanner declaration
//Use Scanner object to take input
int val1 = s_name.nextInt(); //int
float val2 = s_name.nextFloat(); //float
double val3 = s_name.nextDouble(); //double
string name = s_name.nextLine(); //string
char ch = s_name.nextLine().charAt(0); //character
}}
Ejemplo 3: escáner java
import java.util.Scanner;//import Scanner
Scanner input=new Scanner(System.in);//Create the scanner Obj
float numF = input.nextFloat();//Returns float
int num1 = input.nextInt(); //Returns int
byte byte1 = input.nextByte();//Returns Byte
long lg1 = input.nextLong();//Returns long
boolean b1 = input.nextBoolean();//Returns bool
double num2 = input.nextDouble();//Returns Double
String nome = input.nextLine();//Returns String
//execution is pause until you give input
Ejemplo 4: cómo usar la clase de escáner en java
// import Scanner
import java.util.Scanner;
// Initialize Scanner
Scanner input = new Scanner(System.in);
// Test program with Scanner
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("Hello," + name + " , it is nice to meet you!");
Ejemplo 5: escáner java
Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
Ejemplo 6: escáner estático java
import java.util.Scanner;
class ComputerScienceHomework16 {
// making it static in other functions
static Scanner scanner = new Scanner(System.in);
public static void test(){
System.out.print("type a number: ");
int num = scanner.nextInt();
System.out.println("you typed the number " + num);
}
public static void main(String[] args) {
test()
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)