1 包相关
直接java.util.*
2 输入相关
涉及到输入需要提前创建一个键盘接收器sc
Scanner sc=new Scanner(System.in).
2.1 输入一个基本数据类型
按照byte,short,int, long, float, double,char,boolean顺序进行演示,以空格进行区分
byte next = sc.nextByte();
short next = sc.nextShort();
int next =sc.nextInt();
long next = sc.nextLong();
float next = sc.nextFloat();
double next =sc.nextDouble();//double
//对于char型的获取,Scanner没有提供相关的方法。只能获取一个字符串
String next = sc.next();
boolean next = sc.nextBoolean();//boolean
2.2 输入一个字符串
String str=sc.NextLine();//接受一行数据,将其作为一个字符串返回
2.3 输入一行数据
2.3.1 空格隔开
如果输入的是一行数据,中间用空格隔开,比如输入的是1 2 3
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
System.out.println(sc.nextInt());
}
}
2.3.2 字符串被其他符号隔开
如被逗号隔开
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
//输入字符串"1,2,3,4,5"
String str = sc.nextLine();
//通过分隔符,将其转变为字符串数组["1","2","3","4","5"]
String[] split = str.split(",");
//测试输出字符串数组,输出["1","2","3","4","5"]
for (String s : split) {
System.out.println(s);
}
int[] nums = new int[str.length()];
for (int i = 0; i < split.length; i++) {
nums[i] = Integer.parseInt(split[i]);
System.out.print(nums[i]);//测试输出整形数组,输出[1,2,3,4,5]
}
}
}
2.4 多行输入
2.4.1 一维数组
输入两行,第一行输入N表示第二行的个数,如N=3,第二行是1,2,3.
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int nextInt = sc.nextInt();
int[] nums = new int[nextInt];
for (int i = 0; i < nextInt; i++) {
nums[i] = sc.nextInt();
System.out.print(nums[i]);//测试
}
}
2.4.2 二维数组
第一行n和m,代表n行m列,用一个矩阵接收数据
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int row = sc.nextInt();
int col = sc.nextInt();
int[][] matrix = new int[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
matrix[i][j] = sc.nextInt();
}
sc.nextLine();
}
//测试
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
System.out.print(matrix[i][j]);
}
System.out.println();
}
}
2.5 Scanner中nextLine()方法和next()方法的区别
Scanner是一个扫描器,我们录取到键盘的数据,先存到缓存区等待读取,它判断读取结束的标示是 空白符;比如空格,回车,tab 等等。
next()方法读取到空白符就结束,一开始就遇到空白符忽略;
nextLine()读取到回车结束也就是“\n”, 一开始遇到空白符不忽略;
2.5.1 next()接收字符串
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.next();
System.out.println(s);
}
}
2.5.2 nextLine()接收字符串
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String s = sc.nextLine();
System.out.println(s);
}
}
2.6 基本数据类型和字符串混用
2.6.1 基本数据类型和next()
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int nextInt = sc.nextInt();
String s = sc.next();
System.out.println(nextInt + "\n" + s);
}
}
2.6.2 基本数据类型和nextInt()混用
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int nextInt = sc.nextInt();
String str = sc.nextLine();
System.out.println(nextInt + "\n" + str );
}
}
输入1 love,想让nextInt接受1,str接受love,但是缓冲区接受的是1 love,遇到1后面的空格,把1赋给了nextInt变量,空格love就给了str。
要想解决这个问题,需要int变量接受后给一个sc.nextLine();
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
int nextInt = sc.nextInt();
sc.nextLine();
String next = sc.nextLine();
System.out.println(next);
}
3 输出
输出接触最多的是println()和print(),两者的区别是前者输出换行,后者输出不换行
其他情况想起来再写