dim a, b, c
a = inputbox("a", "please input")
b = inputbox("b", "please input")
c = a + b
msgbox(c)
輸入 1 和 1,彈窗顯示 11 而非 2。
InputBox 返回 字符串子類型 (Variant/String)+ 運算符重載:雙方均為數值 → 加法;雙方均為字符串 → 拼接;一數一字 → 加法(字符串轉數值)| 方案 | 代碼 | 原理 | 缺陷 |
|---|---|---|---|
| CInt 強制轉換 | c = CInt(a) + CInt(b) | 四捨五入轉整數 (0.5→偶數) | 輸入小數會四捨五入,非精確加法 |
| Int 截取整數 | a = Int(inputbox(...)) | 向下取整 (負數向更負) | 同樣丟失小數部分 |
| 隱式類型轉換技巧 | c = 0 + a + b | 0 + 字符串 觸發數值加法語境 | ✅ 保留小數精度,最通用 |
Dim a, b, c
a = InputBox("a", "please input")
b = InputBox("b", "please input")
If IsNumeric(a) And IsNumeric(b) Then
c = 0 + a + b ' 或 CDbl(a) + CDbl(b) 顯式轉雙精度
MsgBox c
Else
MsgBox "請輸入有效數字!"
End If
原文標題為「簡單的 Java 讀取文件示例」,以下提供現代 Java (NIO.2) 最佳實踐:
// 返回字符串 (默認 UTF-8)
String content = Files.readString(Path.of("data.txt"));
// 返回行列表
List<String> lines = Files.readAllLines(Path.of("data.txt"));
try (Stream<String> stream = Files.lines(Path.of("big.log"))) {
stream.filter(line -> line.contains("ERROR"))
.forEach(System.out::println);
}
try (BufferedReader br = Files.newBufferedReader(Path.of("config.properties"))) {
String line;
while ((line = br.readLine()) != null) {
// 處理每行
}
}
byte[] data = Files.readAllBytes(Path.of("image.png")); // 小文件
// 或流式
try (InputStream is = Files.newInputStream(Path.of("large.dat"))) {
byte[] buffer = new byte[8192];
int len;
while ((len = is.read(buffer)) != -1) {
// 處理 buffer[0..len-1]
}
}
Files.lines() 返回 Stream 實現惰性讀取Files.newBufferedReader(path, StandardCharsets.UTF_8)IOException,區分文件不存在、權限不足、磁盤錯誤VBScript 的 + 運算符語境敏感特性是動態語言常見陷阱,0 + str 技巧 可優雅解決。Java 文件讀取已全面進入 NIO.2 時代,掌握 Files.readString、Files.lines、BufferedReader 三大模式即可覆蓋 99% 場景。