實現一個經典的 Android 猜數字遊戲:程序隨機生成 0-100 之間的整數,用戶輸入猜測值,程序提示「大了」「小了」或「恭喜猜中」,並統計猜測次數。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="24dp"
android:gravity="center">
<TextView
android:id="@+id/tvHint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入 0-100 之間的數字"
android:textSize="18sp"
android:layout_marginBottom="16dp"/>
<EditText
android:id="@+id/etGuess"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="輸入你的猜測"
android:maxLength="3"
android:gravity="center"/>
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="提交猜測"/>
<TextView
android:id="@+id/tvResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:textSize="16sp"
android:textColor="#FF0000"/>
<Button
android:id="@+id/btnRestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="重新開始"
android:visibility="gone"/>
</LinearLayout>
package com.example.guessnumber;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int targetNumber;
private int guessCount = 0;
private EditText etGuess;
private TextView tvHint, tvResult;
private Button btnSubmit, btnRestart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etGuess = findViewById(R.id.etGuess);
tvHint = findViewById(R.id.tvHint);
tvResult = findViewById(R.id.tvResult);
btnSubmit = findViewById(R.id.btnSubmit);
btnRestart = findViewById(R.id.btnRestart);
initGame();
btnSubmit.setOnClickListener(v -> checkGuess());
btnRestart.setOnClickListener(v -> initGame());
}
private void initGame() {
targetNumber = new Random().nextInt(101); // 0-100
guessCount = 0;
tvHint.setText("請輸入 0-100 之間的數字");
tvResult.setText("");
etGuess.setText("");
etGuess.setEnabled(true);
btnSubmit.setEnabled(true);
btnRestart.setVisibility(View.GONE);
}
private void checkGuess() {
String input = etGuess.getText().toString().trim();
if (input.isEmpty()) {
tvResult.setText("請輸入數字!");
return;
}
int guess = Integer.parseInt(input);
if (guess < 0 || guess > 100) {
tvResult.setText("超出範圍,請輸入 0-100");
return;
}
guessCount++;
if (guess == targetNumber) {
tvResult.setText(String.format("🎉 恭喜猜中!答案是 %d,共猜了 %d 次", targetNumber, guessCount));
endGame();
} else if (guess < targetNumber) {
tvResult.setText("📈 太小了,再大一點");
} else {
tvResult.setText("📉 太大了,再小一點");
}
etGuess.setText("");
}
private void endGame() {
etGuess.setEnabled(false);
btnSubmit.setEnabled(false);
btnRestart.setVisibility(View.VISIBLE);
}
}
SharedPreferences 記錄最佳紀錄(最少猜測次數)。完整項目已上傳至 GitHub,可直接導入 Android Studio 運行。