import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.util.Scanner;public class TongJi { public static void main(String[] args) { //1.只能从键盘读取一个字节 char c = 0; //声明一个变量的同时,一定要对其进行初始化 try { c = (char)System.in.read(); //从键盘读取一个字节 } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(c); //注意:代码块的变量,作用域范围是代码块 //2.只能从键盘获取一个字符串(即一行文本) BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String s = null; //同上 try { s = br.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(s); //3.从键盘可以获取字符串、int类型的值/float类型的值 Scanner sc = new Scanner(System.in); String name = sc.nextLine(); System.out.println(name); int age = sc.nextInt(); System.out.println(age); float salary = sc.nextFloat(); System.out.println(salary); }}
参考: