【Java第13集】java输入输出语句详解
代码示例");// 换行操作方法特点输出println()自动换行,简单直接print()不换行,适合连续输出printf()格式化输出,灵活控制样式输入Scanner类支持多种数据类型读取文件读写FileReaderFileWriter用于文件操作。
·
文章目录
1. 控制台输出语句
1.1 System.out.println()
- 功能:输出数据并自动换行。
- 特点:适用于简单输出,自动处理数据类型转换。
- 示例:
System.out.println("Hello, World"); // 输出字符串并换行 int age = 25; System.out.println("年龄: " + age); // 输出变量值并换行
1.2 System.out.print()
- 功能:输出数据但不换行。
- 特点:适合连续输出多个内容。
- 示例:
System.out.print("Hello, "); System.out.print("World"); // 输出在同一行 // 结果:Hello, World(无换行)
1.3 System.out.printf()
- 功能:格式化输出(类似 C 语言的
printf)。 - 特点:通过格式化字符串控制输出样式。
- 常用格式化符号:
符号 类型 示例 %d整数 System.out.printf("年龄:%d", 25);%f浮点数 System.out.printf("价格:%.2f", 9.99);%s字符串 System.out.printf("姓名:%s", "Alice");%c字符 System.out.printf("首字母:%c", 'A');%x十六进制整数 System.out.printf("十六进制:%x", 255); - 格式化参数详解:
- 宽度:
%8d表示整数占 8 列(不足补空格,右对齐)。 - 精度:
%.2f表示保留 2 位小数。 - 组合:
%8.2f表示总宽度 8,保留 2 位小数。
- 宽度:
- 示例:
int num = 100; double price = 123.45678; System.out.printf("整数:%d,浮点数:%.2f", num, price); // 输出:整数:100,浮点数:123.46
2. 控制台输入语句
2.1 使用 Scanner 类读取输入
- 步骤:
- 导入
java.util.Scanner。 - 创建
Scanner对象(绑定System.in)。 - 调用方法读取数据(如
nextLine()、nextInt()等)。 - 关闭
Scanner(释放资源)。
- 导入
- 示例代码:
import java.util.Scanner; public class InputExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入姓名:"); String name = scanner.nextLine(); // 读取字符串(含空格) System.out.print("请输入年龄:"); int age = scanner.nextInt(); // 读取整数 System.out.println("姓名:" + name + ",年龄:" + age); scanner.close(); // 关闭扫描器 } }
2.2 Scanner 常用方法
| 方法 | 功能 |
|---|---|
nextLine() |
读取一行字符串(含空格) |
nextInt() |
读取整数 |
nextDouble() |
读取浮点数 |
next() |
读取一个单词(以空格分隔) |
hasNext() |
判断是否有下一个输入 |
- 注意事项:
- 输入类型匹配:若输入类型与方法不匹配(如输入字符串调用
nextInt()),会抛出异常。 - 换行符处理:
nextInt()、nextDouble()等方法不会读取换行符,需额外处理(如scanner.nextLine())。
- 输入类型匹配:若输入类型与方法不匹配(如输入字符串调用
3. 格式化输出高级用法
3.1 格式化字符串的完整语法
System.out.printf("格式化字符串", 参数1, 参数2, ...);
- 格式化字符串语法:
%[参数索引$][标志][宽度][.精度]转换符 - 示例:
String name = "Alice"; int age = 25; System.out.printf("%2$s is %1$d years old.", age, name); // 输出:Alice is 25 years old.%1$d:第一个参数作为整数输出。%2$s:第二个参数作为字符串输出。
3.2 日期时间格式化
- 格式化日期:使用
t开头的转换符。import java.util.Date; Date date = new Date(); System.out.printf("当前时间:%tB %<te, %<tY", date); // 输出:当前时间:五月 19, 2025
4. 文件输入输出(简单介绍,后面详细说)
4.1 写入文件
- 代码示例:
import java.io.FileWriter; import java.io.BufferedWriter; import java.io.IOException; public class WriteFile { public static void main(String[] args) { try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write("Hello, Java I/O!"); writer.newLine(); // 换行 } catch (IOException e) { e.printStackTrace(); } } }
4.2 读取文件
- 代码示例:
import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException; public class ReadFile { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("output.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
5. 常见问题与解决方案
Q1: 为什么 nextInt() 后调用 nextLine() 会跳过输入?
- 原因:
nextInt()不读取换行符,导致nextLine()直接读取换行符。 - 解决方案:在
nextInt()后添加scanner.nextLine()清除换行符。int age = scanner.nextInt(); scanner.nextLine(); // 清除换行符 String name = scanner.nextLine();
Q2: 如何读取包含空格的字符串?
- 方法:使用
nextLine()而非next()。String fullName = scanner.nextLine(); // 可读取 "John Doe"
Q3: 如何避免资源泄漏?
- 方法:使用
try-with-resources自动关闭Scanner。try (Scanner scanner = new Scanner(System.in)) { // 读取输入 } // 自动关闭 scanner
6. 总结对比
| 操作 | 方法 | 特点 |
|---|---|---|
| 输出 | println() |
自动换行,简单直接 |
| 输出 | print() |
不换行,适合连续输出 |
| 输出 | printf() |
格式化输出,灵活控制样式 |
| 输入 | Scanner 类 |
支持多种数据类型读取 |
| 文件读写 | FileReader/FileWriter |
用于文件操作 |
更多推荐
所有评论(0)