一、ACM模式
1、打印数字图形
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
for (int i = 1; i <= n; i++) {
printSpace(n - i);
printNum(i);
}
for (int i = n - 1; i >= 1; i--) {
printSpace(n - i);
printNum(i);
}
}
}
public static void printSpace(int x) {
for (int i = 0; i < x; i++) {
System.out.print(" ");
}
}
public static void printNum(int x) {
for (int i = 1; i <= x; i++) {
System.out.print(i);
}
for (int i = x - 1; i >= 1; i--) {
System.out.print(i);
}
System.out.println();
}
}
2、镂空三角形
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
String chars = sc.next();
if (chars.equals("@")) break;
int num = sc.nextInt();
for (int i = 0; i < num - 1; i++) System.out.print(" ");
if (num != 1) System.out.println(chars);
for (int i = 2; i < num; i++) {
for (int j = num - i; j > 0; j--) System.out.print(" ");
System.out.print(chars);
for (int j = 2 * i - 3; j > 0; j--) System.out.print(" ");
System.out.println(chars);
}
for (int i = 0; i < 2 * num - 1; i++) System.out.print(chars);
System.out.println("\n");
}
}
}
3、句子缩写
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
int n = scanner.nextInt();
//Scanner类的nextInt()方法会读取输入中的一个整数,
//但它只会读取整数值,而不会读取换行符。
//当你调用nextInt()后,输入缓冲区中的整数被读取,但换行符仍然留在缓冲区中。
scanner.nextLine();
for (int i = 0; i < n; i++) {
String line = scanner.nextLine();
String[] words = line.split("\\s+");
// 使用正则表达式分割单词(匹配任意空白字符)
StringBuilder result = new StringBuilder();
for (String word : words) {
if (!word.isEmpty()) { // 防止空字符串导致的异常
result.append(word.toUpperCase().substring(0, 1));
}
}
System.out.println(result.toString()); // 输出处理后的结果
}
}
}
1. 注意nextInt()后留下的换行符
在 Java 中,Scanner.nextInt() 读取整数时会跳过前导空白符(如空格、换行符),但不会消耗输入流中的换行符,导致后续的 nextLine() 直接读取到残留的 \n 而返回空字符串
在 Java 中,nextInt() 后是否需要处理换行符取决于后续的输入读取方式:
• 必须处理:若后续调用 nextLine() 读取整行,则需显式调用 scanner.nextLine() 消耗 nextInt() 遗留的 \n,否则会读取到空字符串。
int n = scanner.nextInt();
scanner.nextLine(); // 必须添加!
String line = scanner.nextLine(); // 正确读取新行
• 无需处理:若后续使用 next()、hasNext() 等方法,无需手动处理换行符,因为这些方法会自动跳过所有空白符(包括 \n)。
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
String word = scanner.next(); // 自动跳过残留的 "\n"
}
核心规则:
nextInt() 仅消耗数字和前导空白符,换行符 \n 仍留在缓冲区。只有当后续需要读取完整行内容(如 nextLine())时,才需显式清理换行符;若后续继续用 next() 逐词读取,则换行符会被自动忽略。
1. next(),nextLine()区别
在 Java 中,Scanner.next() 和 Scanner.nextLine() 的核心区别在于输入读取方式:
• next() 逐个读取非空白字符序列(单词),自动跳过所有空白符(空格、制表符、换行符等),但不会消耗换行符,可能导致后续 nextLine() 读取到空字符串。适用于逐词提取数据(如用户名、年龄)。
• nextLine() 读取整行内容(包括换行符 \n),自动跳过当前行的所有空白符并消耗换行符,确保后续输入从下一行开始。适用于需要完整行处理的场景(如读取句子、文件路径)。
关键差异表:
| 方法 | 读取内容 | 跳过空白符 | 消耗换行符 | 示例输入 "Hello World" → 输出 |
|---|---|---|---|---|
next() | "Hello" | ✔️ 是 | ❌ 否 | 下一个 nextLine() 可能返回 " World" |
nextLine() | "Hello World" | ✔️ 是 | ✔️ 是 | 直接获取整行,换行符被清理 |
next() 读单词,nextLine() 读整行;
换行符是否残留?看后续有没有 nextLine()!
若混用需谨慎,nextInt() 后接 nextLine() 必须显式调用 nextLine() 清理换行符,否则会引发输入错位。
2. String[] word = temp.split("\ \s+")
1. 核心作用
将字符串 temp 按空白字符 分割成单词数组。
功能等价于:将连续的空白符(空格/制表符/换行等)视为分隔符,切割出所有非空单词。
2. 正则表达式解析
\\s+ 是 Java 正则表达式的模式,含义是:
\\s:匹配任意单个空白字符(包括空格\u0020、制表符\u0009、换行\u000A等)。+:表示匹配一个或多个连续的相同字符。
⇒ 组合后\\s+表示 匹配一个或多个连续的空白字符。
3. 代码行为示例
假设 temp 的值为 "Hello world\tJava":
String[] words = temp.split("\\s+");
// 结果:words = ["Hello", "world", "Java"]
- 多个空格
和制表符\t均被识别为分隔符 - 连续的空白符被视为单个分隔符处理
4. 对比其他分割方式
| 分割方式 | 输入示例 | 输出结果 |
|---|---|---|
| split(" ") | "a b c" | ["a", "", "", "b", "c"] |
| split("\s+") | "a b c" | ["a", "b", "c"] |
| split("[\s]+") | "a\tb\n c" | ["a", "b", "c"] |
4、字符串拼接
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
//不是必须用nextline,因为next会自动跳过换行符
scanner.nextLine();
for (int i = 0; i < n; i++) {
String fir = scanner.next();
String sec = scanner.next();
String res =
fir.substring(0, fir.length() / 2)
+ sec
+ fir.substring(fir.length() / 2, fir.length());
System.out.println(res);
}
}
}
}
5、字符串交换
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
scanner.nextLine();
for (int k = 0; k < n; k++) {
String col = scanner.nextLine();
char[] ans = col.toCharArray();
for (int i = 0; i < ans.length; i += 2) {
char temp = ans[i];
ans[i] = ans[i + 1];
ans[i + 1] = temp;
}
System.out.println(new String(ans));
}
}
}
}