简单的四则运算
首先画一个界面。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="TextView"
android:layout_weight="0.06" />
<Button
android:id="@+idn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:text="answer" />
<Button
android:id="@+idn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.06"
android:text="下一题" />
</LinearLayout>
public class MyActivity extends AppCompatActivity {
private int a,b,c,d,rs;
private TextView tv;
private Button btn1,btn2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView);
btn1 = (Button)findViewById(R.id.btn1);
btn2 = (Button)findViewById(R.id.btn2);
rd();
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
answer(a,b,c,d);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
show(a,b,c,d);
}
});
}
实现点击按钮显示答案的代码
public void answer(int a,int b,int c,int d){
switch (rs){
case 0:
tv.setText(a+"+"+b+"="+(a+b));
break;
case 1:
tv.setText(a+"-"+b+"="+(a-b));
break;
case 2:
tv.setText(a+"*"+b+"="+(a*b));
break;
case 3:
tv.setText(a+"/"+b+"="+(a/b));
break;
case 4:
tv.setText(a+"+"+b+"*"+c+"="+(a+b*c));
break;
case 5:
tv.setText(a+"-"+b+"*"+c+"="+(a-b*c));
break;
case 6:
tv.setText(a+"+"+b+"/"+c+"="+(a+b/c));
break;
case 7:
tv.setText(a+"-"+b+"/"+c+"="+(a-b/c));
break;
case 8:
tv.setText(a+"+"+b+"-"+c+"*"+d+"="+(a+b-c*d));
break;
case 9:
tv.setText(a+"+"+b+"-"+c+"/"+d+"="+(a+b-c/d));
break;
case 10:
tv.setText(a+"-"+b+"+"+c+"*"+d+"="+(a-b+c*d));
break;
case 11:
tv.setText(a+"-"+b+"+"+c+"/"+d+"="+(a-b+c/d));
break;
case 12:
tv.setText(a+"+"+b+"+"+c+"+"+d+"="+(a+b+c+d));
break;
case 13:
tv.setText(a+"+"+b+"+"+c+"-"+d+"="+(a+b+c-d));
break;
case 14:
tv.setText(a+"+"+b+"-"+c+"-"+d+"="+(a+b-c-d));
break;
case 15:
tv.setText(a+"*"+b+"*"+c+"*"+d+"="+(a*b*c*d));
break;
case 16:
tv.setText(a+"*"+b+"*"+c+"+"+d+"="+(a*b*c+d));
break;
case 17:
tv.setText(a+"*"+b+"+"+c+"+"+d+"="+(a*b+c+d));
break;
case 18:
tv.setText(a+"*"+b+"*"+c+"-"+d+"="+(a*b*c-d));
break;
case 19:
tv.setText(a+"*"+b+"-"+c+"-"+d+"="+(a*b-c-d));
break;
}
}
实现显示题目的代码
public void show(int a,int b,int c,int d){
switch (rs){
case 0:
tv.setText(a+"+"+b+"="+"?");
break;
case 1:
tv.setText(a+"-"+b+"="+"?");
break;
case 2:
tv.setText(a+"*"+b+"="+"?");
break;
case 3:
tv.setText(a+"/"+b+"="+"?");
break;
case 4:
tv.setText(a+"+"+b+"*"+c+"="+"?");
break;
case 5:
tv.setText(a+"-"+b+"*"+c+"="+"?");
break;
case 6:
tv.setText(a+"+"+b+"/"+c+"="+"?");
break;
case 7:
tv.setText(a+"-"+b+"/"+c+"="+"?");
break;
case 8:
tv.setText(a+"+"+b+"-"+c+"*"+d+"="+"?");
break;
case 9:
tv.setText(a+"+"+b+"-"+c+"/"+d+"="+"?");
break;
case 10:
tv.setText(a+"-"+b+"+"+c+"*"+d+"="+"?");
break;
case 11:
tv.setText(a+"-"+b+"+"+c+"/"+d+"="+"?");
break;
case 12:
tv.setText(a+"+"+b+"+"+c+"+"+d+"="+"?");
break;
case 13:
tv.setText(a+"+"+b+"+"+c+"-"+d+"="+"?");
break;
case 14:
tv.setText(a+"+"+b+"-"+c+"-"+d+"="+"?");
break;
case 15:
tv.setText(a+"*"+b+"*"+c+"*"+d+"="+"?");
break;
case 16:
tv.setText(a+"*"+b+"*"+c+"+"+d+"="+"?");
break;
case 17:
tv.setText(a+"*"+b+"+"+c+"+"+d+"="+"?");
break;
case 18:
tv.setText(a+"*"+b+"*"+c+"-"+d+"="+"?");
break;
case 19:
tv.setText(a+"*"+b+"-"+c+"-"+d+"="+"?");
break;
}
}
生成随机数字
private void rd() {
Random random = new Random();
random.nextInt();
a = random.nextInt(100);
b = random.nextInt(100);
c = random.nextInt(100);
d = random.nextInt(100);
rs = random.nextInt(20);
}
}
由于android基础没打好,有很多不完善的地方,望见谅。