
package zhang.app.myXAXB;
import zhang.xaxb.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class xaxb extends Activity {
private EditText editText;
private TextView textView;
private Button ok;
private int m_aNum[] = new int[4];
private char input[] = new char[4];
private int in=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xaxb_main);
editText = (EditText) findViewById(R.id.editText);
textView = (TextView) findViewById(R.id.textView);
ok = (Button) findViewById(R.id.button);
ok.setEnabled(false);
ok.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (editText.getText().toString().equalsIgnoreCase("")) {
} else {
String snum=editText.getText().toString();
in = Integer.parseInt(editText.getText().toString());
if ((in > 1000) && (in < 10000)){// 四位数
for(int i=0;i<4;i++){
input[i]=snum.charAt(i);
}
for(int i=0;i<4;i++)
for(int j=0;j<i;j++){
if(input[i]==input[j]){
Toast.makeText(xaxb.this, "输入错误",Toast.LENGTH_SHORT).show();
editText.setText("");
}
}
}
else{//结束位数检查
Toast.makeText(xaxb.this, "输入错误",Toast.LENGTH_SHORT).show();
editText.setText("");
}
}
for (int i = 0; i < input.length; i++) {
System.out.println("Input---"+input[i]);
}
int nA=0,nB=0;
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if ((int) input[i] - '0' == m_aNum[j]) {
if (i == j) {
nA++;
} else {
nB++;
}
}
}
}
textView.setText(nA + "A" + nB + "B");
if(nA==4){
textView.setTextColor(android.graphics.Color.YELLOW);
ok.setEnabled(false);
new AlertDialog.Builder(xaxb.this).setTitle("结果").setMessage("答对了!").show();
}
}
});
}
// 初始化生成随机数
public void init() {
ok.setEnabled(true);
for (int i = 0; i < 4; i++) {
m_aNum[i] = (int) (Math.random() * 10);
if(m_aNum[0]==0){
m_aNum[0] = (int) (Math.random() * 10);
}else{}
}
while(isdouble()){
for(int k=0;k<4;k++)
for(int j=0;j<k;j++){
while (m_aNum[j] == m_aNum[k]) {
m_aNum[k]=(int) (Math.random() * 10);
}
}
}
};
public boolean isdouble(){
for(int k=0;k<4;k++)
for(int j=0;j<k;j++){
if(m_aNum[j] == m_aNum[k]) {
return true;
}
}
return false;
};
// menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, "开始");
menu.add(0, 2, 2, "帮助");
menu.add(0,3,3,"退出");
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case 1:
init();
textView.setText("");
editText.setText("");
break;
case 3:
finish();
break;
case 2:
new AlertDialog.Builder(xaxb.this).setTitle("帮助").setMessage("这是个猜数游戏!\n " +
"nA表示n个位上的数值和位置都是对的。\n mB代表m个位上的数值是对的,但是位置错误。")
.show();
}
return super.onOptionsItemSelected(item);
}
}
<?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"
>
<TableLayout android:layout_width="fill_parent" android:id="@+id/tableLayout1" android:layout_height="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:layout_weight="7" android:singleLine="true" android:inputType="number" android:layout_height="fill_parent" android:layout_width="fill_parent" android:id="@+id/editText"></EditText>
<Button android:layout_weight="1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/button" android:text="@string/ok"></Button>
</TableRow>
</TableLayout>
<TextView android:layout_height="fill_parent" android:id="@+id/textView" android:layout_width="fill_parent" android:textSize="40dip"></TextView>
</LinearLayout>