作者在 2016-08-20 01:09:18 发布以下内容
public class Calculate {
/**
* @param args
*/
private final static char[] arrayoperate = { '+', '-', 'x', '/' };
public char getOperate() {
Random rdm = new Random();
return arrayoperate[rdm.nextInt(4)];
}
public int getData2() {
Random rdm = new Random();
return rdm.nextInt(100);
}
public int getData1(int data2, char operate) {
Random rdm = new Random();
int data1=0;
if (operate=='+'){
data1= rdm.nextInt(100);
}
else if (operate=='-'){
data1= rdm.nextInt(100) + data2;
}
else if (operate=='x'){
data1= rdm.nextInt(10);
}
else {
data1= rdm.nextInt(10) * data2;
}
return data1;
}
public int result(int data1, int data2, char operate) {
if (operate=='+') {
return data1 + data2;
}
else if (operate=='-') {
return data1 - data2;
}
else if (operate=='x') {
return data1 * data2;
}
else {
return data1 / data2;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Calculate cal = new Calculate();
char operate=cal.getOperate();
int data2=cal.getData2();
int data1 = cal.getData1(data2,operate);
int result=cal.result(data1, data2, operate);
System.out.println("Calculate.main()" + " operate:"
+ operate);
System.out.println("Calculate.main()" + "data1:"
+ data1);
System.out.println("Calculate.main()" + "data2:"
+ data2);
System.out.println("Calculate.main()" + data1+operate
+ data2);
System.out.println("result:" + data1+operate
+ data2+"="+result);
}
}
/////////////////////////////////////////////////////////////////////
public class MainActivity extends Activity {
private TextView tv1, tv5, tv3, tv4;
private EditText et;
Button bt1, bt2;
int data1, data2, result;
Calculate cal;
char operate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViews();
initial();
bt1.setOnClickListener(new OnClickListener() {
int score = 0;
@Override
public void onClick(View arg0) {
String etName = et.getText().toString();// get et Text;
String resultName = String.valueOf(result);
boolean bo = etName.equals(resultName);
if (bo) {
score += 10;
}
Toast.makeText(MainActivity.this,
bo ? "恭喜小朋友,你答对了!分数小计:" + score : "请再做一次后提交,加油!你的最高分是多少啊!",
Toast.LENGTH_LONG).show();
}
});
bt2.setOnClickListener(new OnClickListener() {
int count = 1;
@Override
public void onClick(View arg0) {
et.setText("");
count++;
// TODO Auto-generated method stub
initial();// 重新随机到值;
Toast.makeText(MainActivity.this, "第" + count + "题: ",
Toast.LENGTH_LONG).show();
}
}
);
}
public void initial() {
cal = new Calculate();
operate = cal.getOperate();
data2 = cal.getData2();
data1 = cal.getData1(data2, operate);
result = cal.result(data1, data2, operate);
tv1.setText(String.valueOf(data1));// data1
tv3.setText(String.valueOf(operate));
tv4.setText(String.valueOf(data2));
}
void findViews() {
tv1 = (TextView) findViewById(R.id.textView1);// data1
tv3 = (TextView) findViewById(R.id.textView3);// +
tv4 = (TextView) findViewById(R.id.textView4);// data2
tv5 = (TextView) findViewById(R.id.textView5);
et = (EditText) findViewById(R.id.editText1);
bt1 = (Button) findViewById(R.id.button1);
bt2 = (Button) findViewById(R.id.button2);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
///////////////////////////////////////////////////
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="5"
android:text="data1"
android:textSize="30sp" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:text="+"
android:textSize="30sp" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="5"
android:text="data2"
android:textSize="30sp" />
<TextView
android:id="@+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="="
android:textSize="30sp" />
<EditText
android:id="@+id/editText1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:inputType="number"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:text="是否正确" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="下一题" >
</Button>
</LinearLayout>
</LinearLayout>