201-猜數字遊戲(行動裝置應用)
1.main的xml檔
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText
android:id="@+id/input_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:maxLength="3"
android:numeric="integer" >
<requestFocus />
</EditText>
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btn" />
<TextView
android:id="@+id/respond"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
2.strings的xml檔
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">猜數字遊戲</string>
<string name="title">請輸入三個不重複的數字 : </string>
<string name="btn">送出猜測數字 </string>
</resources>
3.GDD02類別程式碼
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
public class GDD02 extends Activity {
private int systemA, systemB, systemC;
private String strA, strB, strC, resault;
Button submit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getNum();
submit = (Button) this.findViewById(R.id.submit);
submit.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
EditText text = (EditText) GDD02.this
.findViewById(R.id.input_name);
TextView tv = (TextView) GDD02.this.findViewById(R.id.respond);
String myA, myB, myC;
int a = 0;
int b = 0;
myA = text.getText().toString().substring(0, 1);
myB = text.getText().toString().substring(1, 2);
myC = text.getText().toString().substring(2, 3);
if (myA.equals(myB) || myB.equals(myC) || myC.equals(myA)) {
resault="輸入錯誤,請輸入三個不重複的數字";
tv.setText(resault);
text.setText("");
} else {
if (myA.equals(strA) && myB.equals(strB)
&& myC.equals(strC)) {
tv.setText("Input的數字:" + myA + myB + myC
+ " 恭喜你答對囉~答案 : " + strA + strB + strC);
} else {
if (myA.equals(strA)) {
a += 1;
} else if (myA.equals(strB)) {
b += 1;
} else if (myA.equals(strC)) {
b += 1;
}
if (myB.equals(strB)) {
a += 1;
} else if (myB.equals(strA)) {
b += 1;
} else if (myB.equals(strC)) {
b += 1;
}
if (myC.equals(strC)) {
a += 1;
} else if (myC.equals(strA)) {
b += 1;
} else if (myC.equals(strB)) {
b += 1;
}
resault = "Input的數字:" + myA + myB + myC + "=" + a + A"+ b + "B";
tv.setText(resault);
text.setText("");
}
}
}
});
}
public void getNum() {
systemA = (int) (Math.random() * 10);
systemB = (int) (Math.random() * 10);
systemC = (int) (Math.random() * 10);
if (systemB == systemA) {
systemB = (int) (Math.random() * 10);
}
if (systemC == systemB || systemC == systemA) {
systemC = (int) (Math.random() * 10);
}
strA = String.valueOf(systemA);
strB = String.valueOf(systemB);
strC = String.valueOf(systemC);
}
}
沒有留言:
張貼留言