1. ⭐️试题考查范围⭐️
2. ⭐️主类和主方法的命名⭐️
在蓝桥杯提供的官方文档中明确指出了Java组使用主类的命名规则为Main。
public class Main{
}
这里大家要非常的注意!!!
📚主方法为:
public static void main(String[] args){....}
3. ⭐️输入输出⭐️
📚一般的输入输出:
使用Scanner类
📒第一步:
首先需要构造一个Scanner类的对象,并且与标准输入流System.in关联(Scanner类的包名是 java.util.Scanner)。
Scanner sc=new Scanner(System.in);
说明:当创建了一个Scanner类对象之后,控制台会一直等待输入,直到敲回车键结束,把所输入的内容传给对象sc,若要获取需要的内容,调用sc的方法即可。
📒第二步:
不同的数据类型调用不同的sc.nextXXX()方法。
其中数字类型调用以下几种方法:
int a=sc.nextInt();
double b=sc.nextDouble();
long l=sc.nextLong();
short s=sc.nextShort();
字符(字符串)类型使用以下方法:
String s1 = sc.next(); // -》 C++中 cin/scanf
String s2 = sc.nextLine(); // -> C++中 gets()
📚注意:
☕️in.next() 从缓冲区接收字符遇到空格后停止。相当于 cin 和 scanf。
☕️in.nextLine() 从缓冲区接收字符,并且接收空格,遇到换行才停止,并且会自动舍弃换行,相当于 gets()。
📚例如:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String s1 = sc.next();
String s2 = sc.nextLine();
System.out.println("s1:"+s1+" leangth:"+s1.length());
System.out.println("s2:"+s2+" leangth:"+s2.length());
}
}
输入:abc def
输出:
s1:abc leangth:3
s2: def leangth:4
Process finished with exit code 0
📚分析:in.next()将从缓冲区内接收了abc赋值给了s1 , 遇到空格后停止,缓冲区内还剩下了一个空格和def,in.nextLine()将缓冲区剩下的字符赋值给 s2。
除此之外我们还可使用hashNext()。
📚hashNext()特点:
- 📒in.hasNext()的返回值是布尔类型(Boolean)的,当它在缓冲区内扫描到字符时返回的值是true,否则会发生阻塞.等待数据输入。
- 📒in.hasNext()是不会返回false的
- 📒多组输入时,使用 while + in.hasNext() 相当于 while(scanf())。
import java.util.Scanner;
public class HashNext {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int a, b, c;
while(in.hasNext()){
a = in.nextInt();
b = in.nextInt();
c = in.nextInt();
System.out.printf("%d\n",a+b+c);
}
//in.hasNext() // 判断缓存区中还有没有数据,有返回true, 否则等待输入。
//in.hasNextInt() // 判断输入的是不是int型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输入。
//in.hasNextDouble() // 判断输入的是不是double型的数据,是的话返回true 否则继续扫描缓冲区,或者等待输入。
}
}
📚运行程序
输入:1 2 3
输出:6
输入:123
2
3
输出:128
📚输出语句:
System.out.printf(); //和C/C++中的printf一样。 可使用格式控制符进行格式化输出。
// 例如: 输出一个int类型变量 System.out.printf("%d",a);
System.out.print() //不能使用格式控制符进行格式化输出,仅输出变量
System.out.println() //不能使用格式控制符进行格式化输出,仅输出变量,但会自动输出一个换行。
📚快速输入输出
⭐️使用BufferedReader和BufferedWriter实现快速输入输出
📚BufferedReader
BufferedReader类从字符输入流中读取文本并缓冲字符,以便有效地读取字符,数组和行可以通过构造函数指定缓冲区大小也可以使用默认大小。
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
📚主要使用的方法:
String s = in.read() // 读入一个字符 可读入空格回车 但不抛弃回车
String s1 = in.readLine(); // 读入一行 可读入空格可读入回车 但会将回车抛弃
string s2[] = in.readLine().Split(" "); // 使用Split通过空格分割读入的一行字符串,存在s2中
📚BufferedWriter
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
主要使用 BufferedWriter类中的 write() 类进行输出。 当数据量大的时候一定要使用这个类进行输出,谨记!
需要注意的是 write() 不能直接输出int类型, 因为write(int a) 会输出其对应的ASCii码的字符 ,比如输出 65 会显示 A 详见代码:
int a = 65;
char b = '2';
String c = "3";
out.write(a);
out.write("\n");
out.write(b);
out.write("\n");
out.write(c);
out.write("\n");
out.flush();
输出:
A
2
3
所以当需要输出一个int类型的变量时
- 可以用Integer.toString(int a)方法将其变为字符串形式输出;
- 可以使用 + 拼接一个字符串,这样参数整体就是一个字符串了,比如加一个换行符。
int a = 65;
out.write(a + "\n");
out.write(Integer.toString(a));
out.flush();
输出:
65
65
📚整合代码如下:
import java.io.*;
public class BufferedReader01 {
static BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
//测试 read() 和 readLine();
int b = in.read(); // read()只读取一个字符
int x = in.read(); // 吸收 \r
// String e = in.readLine();
String d = in.readLine();
out.write("\n");
out.write(b + "\n");
out.write(x + "\n");
out.write(d + "\n");
//out.write(e);
out.flush();
}
}
输入:
1
ABC DEF
输出:
49(1的ASCii码)
10 (换行键的ASCII码)
ABC DEF
⭐️数组⭐️
可参考我的文章:Java总结五:数组、排序和查找
📚补充:
数组的拷贝:
1: a.clone(int[] b); // 将b数组的值拷贝给a数组
📚示例代码如下:
int[] a = {1,2,3};
int[] b = new int[10];
b = a.clone();// 将b数组的值拷贝给a数组`
for(int i = 0; i < 3; i++){
System.out.print(a[i]+" ");
}
System.out.print("\n");
for(int i = 0; i < 3; i++){
System.out.print(b[i]+" ");
}
输出:
1 2 3
1 2 3
2:System.arraycopy(Object a, int begin_a, Object b, int begin_b, int length);//从a复制到b,复制从a数组指定的位置begin_a开始,到begin_a+length-1结束。放置从b的begin_b开始,到begin_b+length-1结束。
📚示例代码如下:
int[] a = {1, 2, 3, 4};
int[] b = new int[10];
System.arraycopy(a, 0, b, 0, 3);
for(int i = 0; i < 4; i++)
System.out.print(b[i]+" ");
输出:
1 2 3 0
3:Arrays.copyOf(int[] a, int length);//从a数组的第一个元素开始复制,复制length个元素。
📚示例代码如下:
int[] a = {1, 2, 3, 4};
int[] b = new int[10];
b = Arrays.copyOf(a, 4);
for(int i = 0; i < 4; i++)
System.out.println(b[i]);
输出:
1
2
3
4
4:Arrays.copyOfRange(int[] a, int begin, int end);//从a数组begin开始复制,到end-1位置结束。
📚示例代码如下:
int[] a = {1, 2, 3, 4};
int[] b = new int[10];
b = Arrays.copyOfRange(a, 0, 4);
for(int i = 0; i < 4; i++)
System.out.println(b[i]);
输出:
1
2
3
4
其它知识点可参考我的其它文章!