首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 基礎:控制台輸入輸出 (Scanner & System.out) 完整實戰

     編輯:Java 相關知識

前言

控制台交互是 Java 初學者接觸的第一種 I/O 方式。本文系統講解 Scanner 輸入與 System.out 輸出的核心 API、常用模式、異常處理與最佳實踐。

輸出:System.out 詳解

System.out 是 PrintStream 類型,主要方法:

方法說明示例
print()不換行輸出System.out.print("Hello");
println()輸出並換行System.out.println("World");
printf()格式化輸出 (C 風格)System.out.printf("%.2f%%", 99.5);
format()同 printf,返回 PrintStreamSystem.out.format("%s: %d", "Age", 18);

常用格式化佔位符

  • %s 字符串
  • %d 整數 (十進制)
  • %f 浮點數 (默認 6 位小數)
  • %.2f 保留 2 位小數
  • %n 平台無關換行符
  • %t 日期時間 (如 %tF 年-月-日,%tT 時:分:秒)

輸入:Scanner 詳解

java.util.Scanner 可解析基本類型與字符串,默認分隔符為空白字符 (空格、制表符、換行符)。

創建 Scanner

Scanner sc = new Scanner(System.in);        // 標準輸入
Scanner sc = new Scanner(new File("data.txt")); // 文件輸入
Scanner sc = new Scanner("1 2 3");           // 字符串輸入

核心 next 系列方法

方法讀取內容異常備註
next()下一個 Token (非空白字符序列)NoSuchElementException不包含分隔符
nextLine()整行內容 (含空格,至換行符)NoSuchElementException**會消耗換行符**
nextInt()下一個 intInputMismatchException不消耗換行符
nextDouble()下一個 doubleInputMismatchException
nextBoolean()下一個 boolean (true/false 不區分大小寫)InputMismatchException
hasNext()是否有下一個 Token-非阻塞檢查
hasNextInt()下一個是否為 int-安全讀取前檢查

經典坑:nextInt() 後調用 nextLine() 讀到空行

Scanner sc = new Scanner(System.in);
int age = sc.nextInt();      // 輸入 "18\n",讀取 18,**殘留 "\n"**
String name = sc.nextLine(); // 直接讀到殘留的 "\n",返回空字符串!

// 解決:nextInt() 後額外調用一次 nextLine() 消耗換行符
sc.nextLine(); // 消耗換行符
String name = sc.nextLine(); // 正常讀取下一行

完整實戰示例:學生信息錄入系統

import java.util.Scanner;

public class ConsoleIO {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("=== 學生信息錄入 ===");

        // 1. 讀取姓名 (可能含空格)
        System.out.print("姓名: ");
        String name = sc.nextLine();

        // 2. 讀取年齡 (整數)
        System.out.print("年齡: ");
        while (!sc.hasNextInt()) {
            System.out.print("請輸入整數: ");
            sc.next(); // 丟棄無效輸入
        }
        int age = sc.nextInt();
        sc.nextLine(); // 消耗換行符!

        // 3. 讀取身高 (浮點數)
        System.out.print("身高(m): ");
        while (!sc.hasNextDouble()) {
            System.out.print("請輸入數字: ");
            sc.next();
        }
        double height = sc.nextDouble();
        sc.nextLine(); // 消耗換行符

        // 4. 讀取愛好 (多行,以 "END" 結束)
        System.out.println("愛好 (輸入 END 結束): ");
        StringBuilder hobbies = new StringBuilder();
        while (sc.hasNextLine()) {
            String line = sc.nextLine();
            if ("END".equalsIgnoreCase(line)) break;
            hobbies.append(line).append("; ");
        }

        // 5. 格式化輸出
        System.out.printf("%n=== 信息確認 ===%n");
        System.out.printf("姓名: %s%n", name);
        System.out.printf("年齡: %d 歲%n", age);
        System.out.printf("身高: %.2f m%n", height);
        System.out.printf("愛好: %s%n", hobbies);

        sc.close(); // 關閉釋放資源
    }
}

進階技巧

1. 自定義分隔符

Scanner sc = new Scanner("1,2,3,4,5");
sc.useDelimiter(","); // 或正則 "\\s*,\\s*"
while (sc.hasNextInt()) System.out.println(sc.nextInt());

2. 正則匹配提取

Scanner sc = new Scanner("User: Alice, Age: 20");
sc.findInLine("User: (\\w+)"); // 移動到匹配位置
String name = sc.match().group(1); // "Alice"

3. 輸入重定向 (批量測試)

// 命令行
java ConsoleIO < input.txt > output.txt

// 代碼中無需修改,System.in 自動關聯文件

資源管理與異常處理

  • try-with-resources (Java 7+):try (Scanner sc = new Scanner(System.in)) { ... } 自動關閉
  • 捕獲 InputMismatchException:輸入類型不匹配時,需 sc.next() 清除錯誤 Token,否則死循環
  • 關閉 Scanner 會關閉底層流:sc.close() 會調用 System.in.close(),導致後續無法再讀取標準輸入,**全程僅創建一個 Scanner**

總結

控制台 I/O 核心口訣:輸出用 printf 格式化,輸入用 Scanner 次系列,nextInt 後必消耗換行,hasNext 預檢防異常,全程單例 Scanner,try-with-resources 保平安。掌握這些模式,足以應對所有控制台交互場景。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved