方法一:从控制台接收一个字符,只能是一个字符
import java.io.IOException;
public class InputTest01 {
//从控制台接收一个字符,然后将其打印出来
public static void main(String[] args) throws IOException{
System.out.println("enter your char");
char i = (char)System.in.read();
System.out.println("your char is :"+i);
}
}
方法二:从控制台接收一个字符串,但是只能是字符串不灵活
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class InputTest02 {
// 从控制台接收一个字符,然后将其打印出来
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = null;
System.out.println("please Enter Your Value: ");
str = br.readLine();
System.out.println("your Value is:" + str);
}
}
方法三:使用Scanner类,灵活使用nextxxx()的函数,可以获取不同的数据类型
import java.util.Scanner;
public class InputTest03 {
public static void main(String[] args) {
// 接收多种类型的输入数据
Scanner sco = new Scanner(System.in);
System.out.println("your name");
String name = sco.nextLine();// 接收一行字符串
System.out.println("your age");
int age = sco.nextInt();// 接收一行数字
System.out.println("your salary");
float sal = sco.nextFloat();// 接收一行数字
System.out.println("your name is " + name + ";\tyour age is " + age + ";\tyour salary is " + sal + ";");
//关闭流
sco.close();
}
}