From: 011netservice@gmail.com Date: 2025-04-20 Subject: readme-Console101.txt 以下 #### 標記段落, **** 標記常用(流程、設定、備忘) #### 原版 static void Main(string[] args) { // Input Console.Write("名稱:"); string? sName = Console.ReadLine(); // Process int iYear = 0; int iThisYear = DateTime.Now.Year; while (true) // 迴圈開始 { Console.Write("出生年:"); string? sYear = Console.ReadLine(); if (string.IsNullOrEmpty(sYear)) return; // Try-Catch 錯誤處理專門解決(無法預期的錯誤). try { iYear = int.Parse(sYear); } catch (Exception ex) { Console.WriteLine($"請輸入數字, 因為發生錯誤={ex.Message}."); Console.WriteLine(); // User friendly: 空一行比較易讀美觀. continue; // 下一個指令從迴圈開始執行. } // (可以預期的錯誤)是開發人員應處理的範圍. if (iYear < 1900 || iYear > iThisYear) { Console.WriteLine($"請輸入正確的出生年, 1900~{iThisYear}."); continue; // 下一個指令從迴圈開始執行. } // 這裡的 iYear 是正確的, 可以用來計算年齡. break; // 終止迴圈. 執行迴圈後的下一個指令 } int iAge = DateTime.Now.Year - iYear; // Output string sOutput = $"Hello, {sName} 你今年 {iAge} 歲"; Console.WriteLine(sOutput); // UI: User Interface handlling. // 讓使用者看到上方的結果, 確認後再結束. Console.WriteLine(); // User friendly: 空一行比較易讀美觀. Console.WriteLine("按 Enter鍵結束"); Console.ReadLine(); #### 改寫為 JavaScript 把這段程式碼放到 ### Console101-JavaScript-HTML-2.html 升級版,加入了: ✅ Enter 鍵送出功能 ✅ 清除/重設按鈕 ✅ 更好的一點互動體驗 這樣使用者可以: 用滑鼠點擊「送出」 或直接按 Enter 還可以按「清除」快速重填 年齡計算器

年齡計算器

#### 改寫為 Web 版:完整 HTML 程式碼, Node.js 版 JavaScript 程式 放進 .html 檔案就能直接在瀏覽器中使用,跟原本的 C# 很接近 使用方式: 打開一個文字編輯器(例如 VS Code 或記事本)。 貼上上述程式碼並儲存為 index.html。 在瀏覽器中打開這個檔案(雙擊即可)。 年齡計算器 #### 改寫為 現代化 Web UI,使用 HTML + CSS + JavaScript 打造的表單介面,不再用 prompt() 和 alert(),而是使用表單輸入與動態畫面顯示 特點: - 使用表單輸入姓名與出生年。 - 實時驗證資料格式與範圍。 - 美觀的 UI,使用卡片式設計、按鈕效果與錯誤提示。 - 自適應設計,手機上也能正常顯示。 複製程式碼,儲存成 index.html。用瀏覽器打開即可使用。 現代化年齡計算器(HTML + CSS + JS) 年齡計算器

年齡計算器

#### 改寫為 Node.js 版 JavaScript 程式 - 執行方式: 1. 安裝 Node.js(如果尚未安裝): https://nodejs.org/ 2. 將程式存成一個檔案,例如 age-check.js 3. 在 bash 終端機執行: node age-check.js - JavaScript 程式 const readline = require("readline"); // 建立 readline 介面,模擬 Console.ReadLine() const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); function ask(question) { return new Promise((resolve) => { rl.question(question, (answer) => { resolve(answer); }); }); } async function main() { // Input const sName = await ask("名稱: "); // Process let iYear = 0; const iThisYear = new Date().getFullYear(); while (true) { const sYear = await ask("出生年: "); if (!sYear) { rl.close(); return; } try { iYear = parseInt(sYear); if (isNaN(iYear)) throw new Error("不是有效的數字"); } catch (ex) { console.log(`請輸入數字, 因為發生錯誤=${ex.message}。\n`); continue; } if (iYear < 1900 || iYear > iThisYear) { console.log(`請輸入正確的出生年, 1900~${iThisYear}。`); continue; } break; } const iAge = iThisYear - iYear; // Output const sOutput = `Hello, ${sName} 你今年 ${iAge} 歲`; console.log(sOutput); console.log(); // 空一行 console.log("按 Enter 鍵結束"); await ask(""); // 等使用者按 Enter rl.close(); } main(); #### 改寫為 Java import java.util.Scanner; import java.time.Year; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Input System.out.print("名稱:"); String sName = scanner.nextLine(); // Process int iYear = 0; int iThisYear = Year.now().getValue(); while (true) { // 迴圈開始 System.out.print("出生年:"); String sYear = scanner.nextLine(); if (sYear == null || sYear.isEmpty()) { return; } try { iYear = Integer.parseInt(sYear); } catch (NumberFormatException ex) { System.out.println("請輸入數字, 因為發生錯誤=" + ex.getMessage() + "."); System.out.println(); // User friendly: 空一行比較易讀美觀. continue; } if (iYear < 1900 || iYear > iThisYear) { System.out.println("請輸入正確的出生年, 1900~" + iThisYear + "."); continue; } break; // 終止迴圈 } int iAge = iThisYear - iYear; // Output String sOutput = "Hello, " + sName + " 你今年 " + iAge + " 歲"; System.out.println(sOutput); // UI: User Interface handling. System.out.println(); // User friendly: 空一行比較易讀美觀. System.out.println("按 Enter鍵結束"); scanner.nextLine(); // 等待 Enter 鍵 } } #### 改寫為 Android Java 版程式 (使用 Android Studio 建立的 Activity) - activity_main.xml