八大类型数据
一、基本类型
byte int short long float double
二、布尔
boolean
三、字符型
char
代码
import java.util.Scanner;//表示把java.util下的Scanner类导入
public class Input {
public static void main(String[] agrs) {
Scanner myScanner = new Scanner(System.in);
System.out.println("请输入byte");
byte a1 = myScanner.nextByte();
System.out.println("请输入int");
int a2 = myScanner.nextInt();
System.out.println("请输入short");
short a3 = myScanner.nextShort();
System.out.println("请输入long");
long a4 = myScanner.nextLong();
System.out.println("请输入float");
float a5 = myScanner.nextFloat();
System.out.println("请输入double");
double a6 = myScanner.nextDouble();
System.out.println("请输入boolean");
boolean a7 = myScanner.nextBoolean();
System.out.println("请输入char");
char a8 = myScanner.next().charAt(0);
System.out.println("a1=" + a1 + " a2=" + a2 + " a3="
+ a3 + " a4=" + a4 + " a5=" + a5 + " a6=" + a6 + " a7=" + a7 + " a8=" + a8);
}
}
运行结果
一、基本类型和布尔类型
byte int short long float double
都有对应的方法,改掉后缀即可
byte a1 = myScanner.nextByte();
二、字符类型
char a8 = myScanner.next().charAt(0);
与第一种不同,
控制台输入,通过String接受后转化成char型的,都是获得String然后通过charAt指定char型。