本題庫包含 10 題期末測驗題 與 10 題補考測驗題。
每道題目皆綜合本學期所有重點要求,包含:UI 佈局 (EditText/Button)、跑馬燈效果、If-Else 條件判斷、AlertDialog 交談窗 及 Intent 跨程式操作。
📝 題目需求:
- 介面要求:最上方需有一條跑馬燈,提醒「今日美金匯率波動大,請小心交易!」。包含一個輸入台幣的
EditText與一個「開始換算」的Button。並有另一顆按鈕「查詢最新牌告匯率」。 - 邏輯要求:點擊「開始換算」時,跳出
AlertDialog(包含美金、日幣選項)。選定後將輸入的台幣進行換算,並用Toast顯示結果。若未輸入任何數字需用If攔截並提示錯誤。 - Intent 要求:點擊「查詢最新牌告匯率」時,使用隱式 Intent 開啟瀏覽器前往台灣銀行網站。
💻 解答程式碼 (縮減版):
<!-- activity_main.xml -->
<TextView android:id="@+id/tvMarquee" android:text="今日美金匯率波動大,請小心交易!" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<EditText android:id="@+id/editTwd" android:hint="請輸入台幣金額" android:inputType="numberDecimal" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btnCalc" android:text="開始換算" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btnWeb" android:text="查詢最新牌告匯率" android:layout_width="match_parent" android:layout_height="wrap_content"/>// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true); // 啟動跑馬燈
EditText editTwd = findViewById(R.id.editTwd);
findViewById(R.id.btnCalc).setOnClickListener(v -> {
if (editTwd.getText().toString().isEmpty()) {
Toast.makeText(this, "請先輸入金額!", Toast.LENGTH_SHORT).show();
return;
}
double twd = Double.parseDouble(editTwd.getText().toString());
String[] currencies = {"美金 (÷30)", "日幣 (×4.5)"};
new AlertDialog.Builder(this).setTitle("請選擇換算幣別").setItems(currencies, (dialog, which) -> {
double result = (which == 0) ? (twd / 30.0) : (twd * 4.5);
Toast.makeText(this, "換算結果:" + result, Toast.LENGTH_LONG).show();
}).show();
});
findViewById(R.id.btnWeb).setOnClickListener(v -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://rate.bot.com.tw/"));
startActivity(intent);
});📝 題目需求:
- 介面要求:頂部跑馬燈顯示「今日特餐:豪華海鮮牛肉堡,限時特價中!」。一個
EditText讓客人輸入桌號,一顆「選擇主餐」按鈕。 - 邏輯要求:按鈕點下後,跳出
AlertDialog顯示餐點陣列 (牛肉漢堡、海鮮總匯、花生厚片)。利用If-Else判斷:若使用者點擊「花生厚片」,需再次跳出警告AlertDialog詢問「此餐點含花生,確定您沒有過敏嗎?」,按下確定才點餐成功。 - Intent 要求:新增一顆「呼叫服務生」按鈕,隱式 Intent 撥打虛擬分機號碼。
💻 解答程式碼:
<!-- activity_main.xml -->
<TextView android:id="@+id/tvMarquee" android:text="今日特餐:豪華海鮮牛肉堡,限時特價中!" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<EditText android:id="@+id/editTable" android:hint="請輸入桌號" android:inputType="number" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btnOrder" android:text="選擇主餐" android:layout_width="match_parent" android:layout_height="wrap_content"/>
<Button android:id="@+id/btnCall" android:text="呼叫服務生" android:layout_width="match_parent" android:layout_height="wrap_content"/>// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editTable = findViewById(R.id.editTable);
findViewById(R.id.btnOrder).setOnClickListener(v -> {
String[] meals = {"牛肉漢堡", "海鮮總匯", "花生厚片"};
new AlertDialog.Builder(this).setTitle("請選擇餐點").setItems(meals, (dialog, which) -> {
if (which == 2) { // 點到花生厚片
new AlertDialog.Builder(this).setTitle("警告").setMessage("此餐點含有花生,確定要點嗎?")
.setPositiveButton("確定", (d, w) -> Toast.makeText(this, "桌號 "+editTable.getText()+" 點餐成功!", Toast.LENGTH_SHORT).show())
.setNegativeButton("取消", null).show();
} else {
Toast.makeText(this, "桌號 "+editTable.getText()+" 點餐成功!", Toast.LENGTH_SHORT).show();
}
}).show();
});
findViewById(R.id.btnCall).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0900000000")));
});📝 題目需求:
- 介面要求:上方跑馬燈「每日運動30分鐘,健康遠離痛!」。輸入身高、體重共兩個
EditText,一顆「計算 BMI」的按鈕與「尋找運動影片」按鈕。 - 邏輯要求:點擊計算後,利用
If-Else判斷 BMI 數值。若 BMI >= 24,跳出AlertDialog警告「您已經過重,請注意飲食!」,否則跳出「您的體重很標準!」。 - Intent 要求:點擊「尋找運動影片」,隱式 Intent 跳轉至 YouTube 搜尋燃脂運動的網址。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editH = findViewById(R.id.editHeight);
EditText editW = findViewById(R.id.editWeight);
findViewById(R.id.btnCalc).setOnClickListener(v -> {
double h = Double.parseDouble(editH.getText().toString()) / 100;
double w = Double.parseDouble(editW.getText().toString());
double bmi = w / (h * h);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("BMI 計算結果: " + String.format("%.1f", bmi));
if (bmi >= 24) {
builder.setMessage("您已經過重,請注意飲食與運動喔!");
} else {
builder.setMessage("您的體重很標準,請繼續保持!");
}
builder.setPositiveButton("了解", null).show();
});
findViewById(R.id.btnYoutube).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=燃脂運動")));
});📝 題目需求:
- 介面要求:跑馬燈顯示「請輸入六位數員工密碼進行解鎖...」。一個密碼欄位
EditText (inputType="textPassword"),一顆「確認解鎖」按鈕。 - 邏輯要求:內建一組正確密碼
"123456"。若密碼輸入錯誤,跳出AlertDialog警示「密碼錯誤,請重新輸入」並清空輸入框 (setText(""))。 - Intent 要求:若輸入正確,使用顯式 (
Explicit Intent) 跳轉至另一個名為HomeActivity的成功畫面。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editPwd = findViewById(R.id.editPwd);
findViewById(R.id.btnUnlock).setOnClickListener(v -> {
String pwd = editPwd.getText().toString();
if (pwd.equals("123456")) {
// 顯式 Intent 跨 Activity 跳轉
Intent intent = new Intent(MainActivity.this, HomeActivity.class);
startActivity(intent);
} else {
new AlertDialog.Builder(this).setTitle("警示").setMessage("密碼錯誤,請重新輸入!")
.setPositiveButton("確定", (dialog, which) -> editPwd.setText("")).show();
}
});📝 題目需求:
- 介面要求:跑馬燈顯示「本月雙魚座壽星入園只要半價!」。一個輸入年齡的
EditText,一顆「確認票種」按鈕。 - 邏輯要求:按鈕點擊後,若大於等於 18 歲跳出
Toast顯示全票 500 元。若小於 18 歲,跳出AlertDialog提示「您符合優惠資格,請出示學生證」,點擊後發車至結帳 Activity。 - Intent 要求:顯式 Intent 跳轉至
CheckoutActivity,且需利用putExtra將票價變數帶過去。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editAge = findViewById(R.id.editAge);
findViewById(R.id.btnTicket).setOnClickListener(v -> {
int age = Integer.parseInt(editAge.getText().toString());
if (age >= 18) {
Toast.makeText(this, "全票:500元", Toast.LENGTH_SHORT).show();
} else {
new AlertDialog.Builder(this).setTitle("學生優惠").setMessage("您符合學生優惠,票價為 250 元,請至櫃台出示學生證!")
.setPositiveButton("前往結帳", (dialog, which) -> {
Intent intent = new Intent(MainActivity.this, CheckoutActivity.class);
intent.putExtra("PRICE", 250);
startActivity(intent);
}).setNegativeButton("取消", null).show();
}
});📝 題目需求:
- 介面要求:上方跑馬燈顯示「本月預算還剩餘 2000 元,請節約開銷!」。畫面中央有
EditText輸入今日花費金額,一顆「新增記帳」按鈕。 - 邏輯要求:若單筆記帳金額大於 1000 元,利用
If防呆並彈出AlertDialog詢問:「您這筆花費超過 1000 元,確定這真的是必須的開銷嗎?」。使用者按下「確定」後才用Toast顯示「記帳成功」。 - Intent 要求:新增並點擊「開啟計算機」按鈕,隱式 Intent (
ACTION_MAIN、CATEGORY_APP_CALCULATOR) 嘗試開啟系統計算機。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editExpense = findViewById(R.id.editExpense);
findViewById(R.id.btnAdd).setOnClickListener(v -> {
int expense = Integer.parseInt(editExpense.getText().toString());
if (expense > 1000) {
new AlertDialog.Builder(this).setTitle("超支警告").setMessage("這筆花費超過 1000 元,確定是必須的嗎?")
.setPositiveButton("確定記帳", (dialog, which) -> Toast.makeText(this, "記帳成功!", Toast.LENGTH_SHORT).show())
.setNegativeButton("我再想想", (dialog, which) -> editExpense.setText("")).show();
} else {
Toast.makeText(this, "記帳成功: " + expense + " 元", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnCalc).setOnClickListener(v -> {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_APP_CALCULATOR);
startActivity(intent);
});📝 題目需求:
- 介面要求:跑馬燈顯示「強烈冷氣團來襲,今晚氣溫將驟降!」。
EditText讓使用者輸入「攝氏溫度」,及「溫度分析」按鈕。 - 邏輯要求:按鈕點擊後,利用
If-Else若小於 15 度跳出AlertDialog警告「寒流來襲,請穿上羽絨外套」;大於 30 度跳出「注意防曬與中暑」。介於中間則Toast「天氣舒適」。 - Intent 要求:新增一按鈕,隱式 Intent (
ACTION_VIEW) 連結至中央氣象署網頁。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editTemp = findViewById(R.id.editTemp);
findViewById(R.id.btnAnalyze).setOnClickListener(v -> {
int temp = Integer.parseInt(editTemp.getText().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("穿衣建議");
if (temp < 15) {
builder.setMessage("寒流來襲,請穿上羽絨外套與毛帽!").setPositiveButton("好", null).show();
} else if (temp > 30) {
builder.setMessage("天氣炎熱,請注意防曬並多補充水分!").setPositiveButton("好", null).show();
} else {
Toast.makeText(this, "今天天氣很舒適,非常適合出遊!", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.btnWeatherInfo).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.cwa.gov.tw/")));
});📝 題目需求:
- 介面要求:跑馬燈顯示「請保持安靜,圖書館內禁止飲食!」。設計
EditText輸入「已借閱本數」,與一顆「借閱新書」的按鈕。 - 邏輯要求:最高上限為 5 本。用
If-Else判斷。大於或等於 5 本時跳出AlertDialog拒絕借閱並提醒「您的借閱已達上限,請先還書」。小於 5 則Toast「借閱成功,還有 X 本的額度」。 - Intent 要求:顯式 Intent 跳至
RuleActivity(借閱規則宣告頁面)。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editBooks = findViewById(R.id.editBooks);
findViewById(R.id.btnBorrow).setOnClickListener(v -> {
int currentBooks = Integer.parseInt(editBooks.getText().toString());
if (currentBooks >= 5) {
new AlertDialog.Builder(this).setTitle("借閱失敗")
.setMessage("您的借閱已達上限 (5本),請先歸還部分書籍後再借!")
.setPositiveButton("確定", null).show();
} else {
int left = 5 - currentBooks - 1; // 扣掉現在要借的這1本
Toast.makeText(this, "借閱成功!您還可以再借 " + left + " 本", Toast.LENGTH_LONG).show();
}
});
findViewById(R.id.btnRule).setOnClickListener(v -> {
startActivity(new Intent(MainActivity.this, RuleActivity.class));
});📝 題目需求:
- 介面要求:上方跑馬燈「全館滿 1000 現折 200,滿 2000 打八折!」。
EditText輸入總金額,一個「結帳」按鈕。 - 邏輯要求:計算結帳總額。利用
If-Else if:若原本金額大於等於 2000,金額 * 0.8;否若大於等於 1000,金額變為原價減 200。最後呼叫AlertDialog顯示「原價、折扣與應付總額」。 - Intent 要求:結帳確認後跳轉至實體店面地圖 (隱式 Intent 搜尋 Google Maps)。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editPrice = findViewById(R.id.editPrice);
findViewById(R.id.btnCheckout).setOnClickListener(v -> {
int original = Integer.parseInt(editPrice.getText().toString());
double finalPrice = original;
String discountNote = "無折扣";
if (original >= 2000) {
finalPrice = original * 0.8;
discountNote = "滿 2000 享八折優惠";
} else if (original >= 1000) {
finalPrice = original - 200;
discountNote = "滿 1000 折 200";
}
new AlertDialog.Builder(this)
.setTitle("結帳明細")
.setMessage("原價:" + original + "\n折扣:" + discountNote + "\n應付總額:" + finalPrice)
.setPositiveButton("前往實體結帳", (dialog, which) -> {
// 隱式 Intent 顯示地圖,搜尋附近的店家
Uri gmmIntentUri = Uri.parse("geo:0,0?q=便利商店");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
startActivity(mapIntent);
}).show();
});📝 題目需求:
- 介面要求:跑馬燈顯示「尾牙抽獎即將開始,最高獎金一百萬元!」。使用者透過
EditText輸入自己心中的「幸運號碼 (1~100)」。按下「開獎」按鈕。 - 邏輯要求:使用
Math.random()產出 1~100 的得獎號碼。利用If-Else比對使用者輸入的號碼是否跟開獎號碼一致。「中獎」跳出AlertDialog恭喜並給予煙火金句;「沒中」清空輸入框讓他再接再厲。 - Intent 要求:顯式 Intent 跳至
ListActivity用來查看所有得獎名單歷年紀綠。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editLucky = findViewById(R.id.editLucky);
findViewById(R.id.btnDraw).setOnClickListener(v -> {
int userNum = Integer.parseInt(editLucky.getText().toString());
int winNum = (int)(Math.random() * 100) + 1; // 產生 1~100
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("開獎結果: " + winNum);
if (userNum == winNum) {
builder.setMessage("天啊!這機率只有 1%!恭喜你中獎了!!!").setPositiveButton("領獎去", null).show();
} else {
builder.setMessage("很可惜沒收中,下次再接再厲囉!")
.setPositiveButton("再抽一次", (dialog, which) -> editLucky.setText(""))
.show();
}
});
findViewById(R.id.btnHistory).setOnClickListener(v -> {
startActivity(new Intent(MainActivity.this, ListActivity.class));
});補考題庫將原本期末的邏輯進行稍加修改。
📝 題目需求 (修改自期末提型1):
- 介面要求:跑馬燈顯示「請確認公制的轉換單位是否正確!」。輸入「公分(cm)」長度的
EditText,與「長度換算」的Button。另一顆按鈕「搜尋長度單位表」。 - 邏輯要求:點擊按鈕,跳出
AlertDialog詢問要轉成「公尺 (÷100)」還是「英吋 (÷2.54)」。算完用Toast顯示。未輸入需用If擋下。 - Intent 要求:隱式 Intent 連結至 Google 搜尋"長度轉換表"。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editCm = findViewById(R.id.editCm);
findViewById(R.id.btnCalc).setOnClickListener(v -> {
if (editCm.getText().toString().isEmpty()) return;
double cm = Double.parseDouble(editCm.getText().toString());
String[] units = {"公尺 (÷100)", "英吋 (÷2.54)"};
new AlertDialog.Builder(this).setTitle("請選擇換算單位").setItems(units, (dialog, which) -> {
double result = (which == 0) ? (cm / 100.0) : (cm / 2.54);
Toast.makeText(this, "換算結果:" + result, Toast.LENGTH_LONG).show();
}).show();
});
findViewById(R.id.btnWeb).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/search?q=長度轉換表")));
});📝 題目需求 (修改自期末提型2):
- 介面要求:跑馬燈「新品上市:鮮榨青檸綠茶!」。
EditText輸入顧客姓名。一顆「選擇甜度」按鈕。 - 邏輯要求:跳出
AlertDialog(無糖、半糖、全糖)。若選「全糖」,跳出警告對話框「全糖熱量偏高,確定要喝這麼甜嗎?」,確定後再Toast點餐成功。 - Intent 要求:隱式 Intent 撥打外送電話。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editName = findViewById(R.id.editName);
findViewById(R.id.btnSugar).setOnClickListener(v -> {
String[] sugars = {"無糖", "半糖", "全糖"};
new AlertDialog.Builder(this).setTitle("選擇甜度").setItems(sugars, (dialog, which) -> {
if (which == 2) {
new AlertDialog.Builder(this).setTitle("醫師警告").setMessage("全糖熱量偏高,確定要喝這麼甜嗎?")
.setPositiveButton("不管啦", (d, w) -> Toast.makeText(this, editName.getText()+" 訂購完成", Toast.LENGTH_SHORT).show())
.setNegativeButton("重新選擇", null).show();
} else {
Toast.makeText(this, editName.getText()+" 訂購完成", Toast.LENGTH_SHORT).show();
}
}).show();
});
findViewById(R.id.btnCall).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse("tel:0212345678")));
});📝 題目需求 (修改自期末提型3):
- 介面要求:跑馬燈「了解你的BMR,減重更輕鬆!」。男生統一帶入公式常數,只需要輸入「體重(kg)」即可計算。一顆「計算BMR」按鈕與「看減重影片」按鈕。
- 邏輯要求:男性簡化版BMR = 體重 * 24 * 1。若 BMR 大於 2000,
AlertDialog顯示「肌肉猛男」;小於 1500 顯示「代謝偏低請多運動」。 - Intent 要求:隱式 Intent 搜尋 YouTube "提升代謝率"。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editW = findViewById(R.id.editWeight);
findViewById(R.id.btnCalc).setOnClickListener(v -> {
double w = Double.parseDouble(editW.getText().toString());
double bmr = w * 24 * 1.0;
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("BMR: " + bmr);
if (bmr > 2000) builder.setMessage("您擁有如同肌肉猛男般的超級代謝率!");
else if (bmr < 1500) builder.setMessage("您的代謝偏低,請多喝水與運動!");
else builder.setMessage("代謝率正常!");
builder.setPositiveButton("了解", null).show();
});
findViewById(R.id.btnYoutube).setOnClickListener(v -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.youtube.com/results?search_query=提升代謝率")));
});📝 題目需求 (修改自期末提型4):
- 介面要求:跑馬燈「請出示數位會員卡或輸入末四碼」。
EditText輸入身分證末四碼,一顆「驗證身分」按鈕。 - 邏輯要求:內建合法號碼為
"8888"。輸入錯跳出AlertDialog警示未註冊,並清空框框;若正確,跳轉至MemberActivity。 - Intent 要求:顯式 Intent 跳轉。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editId = findViewById(R.id.editId);
findViewById(R.id.btnVerify).setOnClickListener(v -> {
if (editId.getText().toString().equals("8888")) {
startActivity(new Intent(MainActivity.this, MemberActivity.class));
} else {
new AlertDialog.Builder(this).setTitle("此卡無效").setMessage("您並非本館註冊會員,請重新輸入!")
.setPositiveButton("知道", (dialog, which) -> editId.setText("")).show();
}
});📝 題目需求 (修改自期末提型5):
- 介面要求:跑馬燈「本院上映最新強檔:奪魂鋸」。
EditText輸入年齡,一顆「我要買票」按鈕。 - 邏輯要求:若滿 18 歲,
Toast歡迎購票。若未滿 18 歲,AlertDialog嚴格拒絕「本片為限制級,您未滿18禁止購票!」。禁止購票將阻擋後續行為。若可購票,跳轉活動。 - Intent 要求:顯式 Intent 跳轉至
MovieActivity選位子。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editAge = findViewById(R.id.editAge);
findViewById(R.id.btnTicket).setOnClickListener(v -> {
int age = Integer.parseInt(editAge.getText().toString());
if (age >= 18) {
Toast.makeText(this, "歡迎購票!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, MovieActivity.class));
} else {
new AlertDialog.Builder(this).setTitle("警告")
.setMessage("本片為限制級,您未滿 18 歲禁止觀看!")
.setPositiveButton("離場", null).show();
}
});📝 題目需求 (修改自期末提型6):
- 介面要求:跑馬燈顯示「每餐控制熱量,體重不失控!」。
EditText輸入今日午餐卡路里,一顆「新增熱量」按鈕。 - 邏輯要求:若輸入大於 800 大卡,利用
If跳出AlertDialog詢問:「熱量似乎太高了,確定要吃這個嗎?」。按下「我還是要吃」才Toast新增成功;「放棄」則清空輸入。 - Intent 要求:新增一按鈕隱式開啟計算機。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editCal = findViewById(R.id.editCal);
findViewById(R.id.btnAdd).setOnClickListener(v -> {
int cal = Integer.parseInt(editCal.getText().toString());
if (cal > 800) {
new AlertDialog.Builder(this).setTitle("熱量超標警告").setMessage("確定要吃下這個高熱量食物嗎?")
.setPositiveButton("我還是要吃", (dialog, which) -> Toast.makeText(this, "新增熱量成功!", Toast.LENGTH_SHORT).show())
.setNegativeButton("放棄不吃了", (dialog, which) -> editCal.setText("")).show();
} else {
Toast.makeText(this, "新增熱量: " + cal, Toast.LENGTH_SHORT).show();
}
});📝 題目需求 (修改自期末提型7):
- 介面要求:跑馬燈「梅雨季來臨,出門請注意氣象!」。
EditText輸入降雨機率(%),及「分析裝備」按鈕。 - 邏輯要求:若機率大於 70%
AlertDialog警告「請攜帶雨傘與穿雨鞋」;小於 10%Toast「天氣晴朗免帶傘」;介於中間Toast「帶把摺疊傘有備無患」。 - Intent 要求:隱式 Intent 連結至中央氣象署網頁。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editRain = findViewById(R.id.editRain);
findViewById(R.id.btnAnalyze).setOnClickListener(v -> {
int rain = Integer.parseInt(editRain.getText().toString());
if (rain > 70) {
new AlertDialog.Builder(this).setTitle("出門建議").setMessage("今天非常有機會下雨,請務必攜帶雨傘!")
.setPositiveButton("了解", null).show();
} else if (rain < 10) {
Toast.makeText(this, "天氣晴朗免帶傘", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "帶把摺疊傘有備無患", Toast.LENGTH_SHORT).show();
}
});📝 題目需求 (修改自期末提型8):
- 介面要求:跑馬燈「本停車場剩餘車位即將額滿!」。
EditText輸入目前「場內車輛數」,按鈕「申請入場」。 - 邏輯要求:停車場最高容量 50 台。當車數 >= 50 跳出
AlertDialog拒絕「車位已滿,請到其他停車場」。小於 50 則Toast「歡迎入場,還剩 X 個位子」。 - Intent 要求:顯式 Intent 跳至
MapActivity導航其他停車場。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editCars = findViewById(R.id.editCars);
findViewById(R.id.btnPark).setOnClickListener(v -> {
int cars = Integer.parseInt(editCars.getText().toString());
if (cars >= 50) {
new AlertDialog.Builder(this).setTitle("入場失敗").setMessage("車位已滿,請導航到其他停車場!")
.setPositiveButton("確定", null).show();
} else {
Toast.makeText(this, "歡迎入場!剩餘車位:" + (50 - cars - 1), Toast.LENGTH_LONG).show();
}
});📝 題目需求 (修改自期末提型9):
- 介面要求:跑馬燈「高鐵點數換現金,會員狂歡月!」。
EditText輸入持有「點數」,「計算折抵」按鈕。 - 邏輯要求:票價固定 1490 元。每 100 點可折抵 10 元。用
If-Else iF取出最大折抵額度:若點數大於 5000 點直接票價打對折(最高優惠);若不足則換算扣減金額。跳出AlertDialog顯示「原票價、折扣與應收金額」。 - Intent 要求:確認後打開 Browser 前往信用卡網頁。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editPoints = findViewById(R.id.editPoints);
findViewById(R.id.btnDiscount).setOnClickListener(v -> {
int points = Integer.parseInt(editPoints.getText().toString());
double finalPrice = 1490;
if (points >= 5000) {
finalPrice = 1490 * 0.5;
} else {
int discountCash = (points / 100) * 10;
finalPrice = 1490 - discountCash;
if (finalPrice < 0) finalPrice = 0; // 防呆
}
new AlertDialog.Builder(this).setTitle("票券結帳")
.setMessage("應收金額:" + finalPrice + " 元")
.setPositiveButton("確認付款", (d, w) -> {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com")));
}).show();
});📝 題目需求 (修改自期末提型10):
- 介面要求:跑馬燈「來試試手氣吧!比莊家大就贏!」。
EditText輸入 1~6 你自己想丟的數字。按下「丟骰子」按鈕。 - 邏輯要求:使用
Math.random()產出 1~6 電腦擲出的數字。比對使用者輸入與電腦點數。「比電腦大」跳AlertDialog恭喜玩家贏了;「比電腦小」跳AlertDialog你輸了;「平手」清空數字重來。 - Intent 要求:顯式 Intent 跳至
ScoreActivity記錄比賽結果。
💻 解答程式碼:
// MainActivity.java
findViewById(R.id.tvMarquee).setSelected(true);
EditText editDice = findViewById(R.id.editDice);
findViewById(R.id.btnRoll).setOnClickListener(v -> {
int userDice = Integer.parseInt(editDice.getText().toString());
int comDice = (int)(Math.random() * 6) + 1; // 產生 1~6
AlertDialog.Builder builder = new AlertDialog.Builder(this).setTitle("電腦丟出了: " + comDice);
if (userDice > comDice) {
builder.setMessage("恭喜你贏了!").setPositiveButton("領獎", null).show();
} else if (userDice < comDice) {
builder.setMessage("可惜,你輸了!").setPositiveButton("再接再厲", null).show();
} else {
builder.setMessage("平手無輸贏,請重擲!").setPositiveButton("重來", (d, w) -> editDice.setText("")).show();
}
});