一、打印輸出到屏幕
Java提供了三種核心輸出方法,適合不同場景:
System.out.println()
- 打印內容后 自動換行
System.out.println("Welcome");
System.out.println("to ISS");
// 輸出:
// Welcome
// to ISS
System.out.print()
- 打印內容后 不換行 (光標停留在末尾)
System.out.print("Welcome ");
System.out.print("to ISS");
// 輸出:Welcome to ISS
System.out.printf()
- 格式化輸出 (類似C語言的
printf
)
String name = "Alice";
int age = 25;
double height = 1.68;
System.out.printf("Name: %s | Age: %d | Height: %.2f m%n", name, age, height);
// 輸出:Name: Alice | Age: 25 | Height: 1.68 m
- 格式說明符:
%s
:字符串%d
:整數%f
:浮點數(%.2f
保留兩位小數)%n
:換行符
二、字符串拼接與轉義字符
字符串拼接
用 +
連接變量與文本:
double price = 9.99;
System.out.println("Price: $" + price); // 輸出:Price: $9.99
轉義字符
特殊字符需用反斜杠\
轉義:
序列 | 作用 | 示例 |
---|---|---|
\n | 換行 | "Line1\nLine2" |
\t | 制表符 | "Name:\tAlice" |
\" | 雙引號 | "He said \"Hi\"" |
\\ | 反斜杠本身 | "Path: C:\\Users" |
三、數字格式化(DecimalFormat)
精確控制數字顯示格式:
import java.text.DecimalFormat;double value = 6543.21;// 示例1:保留1位小數(自動四舍五入)
DecimalFormat df1 = new DecimalFormat("#.#");
System.out.println(df1.format(value)); // 輸出:6543.2// 示例2:千位分隔符+兩位小數
DecimalFormat df2 = new DecimalFormat("#,##0.00");
System.out.println(df2.format(value)); // 輸出:6,543.21// 示例3:固定位數(不足補0)
DecimalFormat df3 = new DecimalFormat("000000.000");
System.out.println(df3.format(42.5)); // 輸出:000042.500
符號說明 :
#
:可選數字位(不顯示無效0)0
:強制數字位(不足補0),
:千位分隔符
四、讀取用戶輸入(Scanner)
通過Scanner
類獲取鍵盤輸入:
import java.util.Scanner;Scanner scanner = new Scanner(System.in); // 創建Scanner對象System.out.print("Enter your name: ");
String name = scanner.nextLine(); // 讀取整行文本System.out.print("Enter your age: ");
int age = scanner.nextInt(); // 讀取整數System.out.print("Enter salary: ");
double salary = scanner.nextDouble(); // 讀取浮點數scanner.close(); // 關閉Scanner釋放資源System.out.printf("Hello %s! You are %d and earn $%.2f", name, age, salary);
注意事項 :
nextLine()
會讀取空格和換行,而nextInt()
/nextDouble()
遇到空格即停止- 混合輸入時,建議先用
nextLine()
讀取換行符避免沖突- 讀取后務必調用
scanner.close()
五、日期時間處理(Java 8+)
import java.time.*;
import java.time.format.DateTimeFormatter;// 獲取當前時間
LocalDateTime now = LocalDateTime.now();
System.out.println("原始格式: " + now); // 輸出:2025-07-24T23:22:22.123// 自定義格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss");
String formatted = now.format(formatter);
System.out.println("格式化后: " + formatted); // 輸出:24/07/2025 23:22:22
常用類 :
LocalDate
:僅日期(年月日)LocalTime
:僅時間(時分秒)LocalDateTime
:日期+時間
六、重點總結
功能 | 核心方法/類 | 使用場景 |
---|---|---|
基礎打印 | System.out.println() | 快速輸出內容并換行 |
格式化輸出 | System.out.printf() | 控制數字/字符串對齊和精度 |
數字格式化 | DecimalFormat | 顯示千位分隔符/固定小數位 |
用戶輸入 | Scanner + nextXxx() | 讀取鍵盤輸入的各類數據 |
日期處理 | LocalDateTime + DateTimeFormatter | 日期計算和格式化顯示 |
七、練習
Java新手編程練習:掌握基礎輸入輸出
題目1:打印姓名和郵箱
編寫程序,按指定格式輸出姓名和郵箱:
John Smith
e0011223@u.nus.edu
解題代碼
public class Exercise1 { public static void main(String[] args) { System.out.println("John Smith"); System.out.println("e0011223@u.nus.edu"); }
}
解析:
- 使用兩個
System.out.println()
分別打印兩行內容 println()
在輸出后自動添加換行符,確保姓名和郵箱分行顯示- 可直接替換引號內字符串為實際信息
題目2:個性化問候語
編寫程序,接收用戶輸入的姓名,輸出問候語:
Good Morning [姓名]
解題代碼
import java.util.Scanner; public class Exercise2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter your name: "); String name = scanner.nextLine(); System.out.println("Good Morning " + name); scanner.close(); }
}
解析:
- 導入
Scanner
類處理輸入 scanner.nextLine()
讀取整行文本(包括空格)- 字符串拼接操作
"Good Morning " + name
組合問候語 - 必須調用
scanner.close()
釋放資源
?題目3:整數平方計算
輸入一個整數,輸出其平方值:
輸入:5
輸出:25
解題代碼
import java.util.Scanner; public class Exercise3 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int num = scanner.nextInt(); int square = num * num; System.out.println("Square: " + square); scanner.close(); }
}
解析:
nextInt()
專用于讀取整數輸入- 使用
num * num
直接計算平方(比Math.pow()
更高效) - 整數運算不會產生浮點數精度問題
題目4:浮點數平方計算
輸入雙精度浮點數,輸出其平方值:
輸入:2.5
輸出:6.25
解題代碼
import java.util.Scanner; public class Exercise4 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double num = scanner.nextDouble(); double square = num * num; System.out.println("Square: " + square); scanner.close(); }
}
解析:
nextDouble()
讀取雙精度浮點數- 浮點數乘法可能產生精度問題(如
0.1 * 0.1 = 0.010000000000000002
) - 商業計算建議使用
BigDecimal
類
題目5:金額格式化
輸入雙精度數,輸出保留兩位小數(自動四舍五入):
輸入:4.555 → 輸出:4.56
輸入:3.232 → 輸出:3.23
解題代碼
import java.text.DecimalFormat;
import java.util.Scanner; public class Exercise5 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a number: "); double num = scanner.nextDouble(); DecimalFormat df = new DecimalFormat("0.00"); String formatted = df.format(num); System.out.println("Formatted: " + formatted); scanner.close(); }
}
解析:
-
DecimalFormat
使用模式字符串控制格式 -
"0.00"
表示:
- 至少1位整數(不足補0)
- 固定2位小數(不足補0,超位四舍五入)
-
模式改為
"#.##"
可隱藏整數部分的無效0