반응형

자바 키보드 입력 세 가지 예제 

 

package helloworld;

import java.io.BufferedReader;
import java.io.Console;
import java.io.InputStreamReader;
import java.util.Scanner;

public class input {

public static void main(String[] args) {
// 키보드 입력 1
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Name: ");
        try {
            String s = br.readLine();
            System.out.println("Hello, " + s);
        }catch(Exception e) {
            System.out.println(e);
        }
        
        // 키보드 입력 2
        System.out.println("Age: ");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        System.out.println(a);
        scanner.close();
        
        // 키보드 입력 3
        Console console = System.console();
        if (console != null) {
         String username = console.readLine("Please enter user name : ");   
         System.out.println("You entered : " + username);
        } else {
         System.out.println("No console!");
        }

}

}

반응형

+ Recent posts