1.Exp.java 产生运算式计算
package com.example.cal; import android.util.Log; public class Exp {// 产生表达式及结果 private int item_num = 0;// 0:两项,1:三项 private int sign_num = 0;// 0+,1-,2*,3/ private int sign_num_1 = 0;// 0+,1-,2*,3/ 三项的时候需要两个运算符 private int kuohao = 0;// 0不产生括号,1产生括号 private int kuohao_weizhi = 0;// 0表示前2项有括号,1表示后2项有括号 private int result;// 结果 private StringBuilder exp=new StringBuilder();// 式子 enum Sign { add, subtract, multiply, divide } Exp() { while(true){ if(createExp()==true) { break; } } } public StringBuilder getExp() { return exp; } public void setExp(StringBuilder exp) { this.exp = exp; } public void setResult(int result) { this.result = result; } private boolean createExp() { item_num = (int) (Math.random() * 2); sign_num = (int) (Math.random() * 4); int a = (int) (Math.random() * 100);// 随机生成0-100以内的数 int b = (int) (Math.random() * 100); int c = (int) (Math.random() * 100); kuohao = (int) (Math.random() * 2); Log.e("jhd", item_num+""); Log.e("jhd", sign_num+""); Log.e("jhd", kuohao+""); Log.e("jhd", a+""); Log.e("jhd", b+""); Log.e("jhd", c+""); if (item_num == 0) {// 产生2项的情况 String temp = ""; switch (sign_num) { case 0:// + temp = a + "+" + b; result = a + b; break; case 1:// - temp = a + "-" + b; result = a - b; break; case 2:// * temp = a + "*" + b; result = a * b; break; case 3:// / temp = a + "÷" + b; if (a % b != 0) {// 不整除旧返回false return false; } result = a / b; break; } exp.append(temp); } if (item_num == 1) {// 式子为三项时 sign_num_1 = (int) (Math.random() * 4);// 随机产生一个运算符 if (kuohao == 0) {// 不产生括号 // 比较优先级 String temp; if (sign_num <= 1 && sign_num_1 > 1) {// 先计算后两项 if (calculate(b, sign_num_1, c) == null) { return false; } result = calculate(a, sign_num, calculate(b, sign_num_1, c)); exp.append(a + getSign(sign_num) + b + getSign(sign_num_1) + c); } else {// 计算前两项 if (calculate(a, sign_num, b) == null) { return false; } if (calculate(calculate(a, sign_num, b), sign_num_1, c) == null) { return false; } result = calculate(calculate(a, sign_num, b), sign_num_1, c); exp.append(a + getSign(sign_num) + b + getSign(sign_num_1) + c); } } else{ return false; } } return true; } public Integer calculate(int a, int sign, int b) { Integer temp = null; switch (sign) { case 0: temp = a + b; break; case 1: temp = a - b; break; case 2: temp = a * b; break; case 3: if (a % b != 0) {// 保证能整除 return null; } temp = a / b; break; } return temp; } private String getSign(int sign) { String temp = null; switch (sign) { case 0: temp = "+"; break; case 1: temp = "-"; break; case 2: temp = "*"; break; case 4: temp = "/"; break; } return temp; } public int getResult() {// 得到产生表达式的结果 return result; } /*public String getExpression() {// 得到产生的表达式 //if(get) }*/ }
2.ExpActivity,java
package com.example.cal; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ExpActivity extends Activity implements OnClickListener { private CreateExp expclass; private TextView tv_exp; private EditText et_result; private Button next; private Button exit; private TextView all; private TextView completed; private TextView wrong; private int index = 0;// 当前题目位置 private int wrong_num = 0;// 答错题目数量 private List<Exp> list;// 题目数组,所有题目 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exp); expclass = new CreateExp();// 随机产生一个2-3项四则运算式子,及式子的结果 // ---初始控件 intViews(); int num = getIntent().getIntExtra("num", 0); if (num != 0) { list = new ArrayList<Exp>(); for (int i = 0; i < num; i++) { list.add(new Exp()); } tv_exp.setText(list.get(index).getExp());// all.setText("共计:"+num); completed.setText("已答:"+(index)); wrong.setText("答错:"); } } private void intViews() { // TODO Auto-generated method stub tv_exp = (TextView) findViewById(R.id.tv_exp); et_result = (EditText) findViewById(R.id.et_result); next = (Button) findViewById(R.id.next_btn); exit = (Button) findViewById(R.id.exit_btn); all = (TextView) findViewById(R.id.all); completed = (TextView) findViewById(R.id.completed); wrong = (TextView) findViewById(R.id.wrong); next.setOnClickListener(new OnClickListener() {// 点击下一题时,判断上一道是否正确,并且显示下一道 @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(et_result.getText().toString()==""){ Toast.makeText(getApplicationContext(), "答案不能为空!", Toast.LENGTH_SHORT).show(); } int result = Integer.valueOf(et_result.getText().toString()); if (index >= list.size()) { Toast.makeText(getApplicationContext(), "题目已经答完!", Toast.LENGTH_SHORT).show(); return; } if (result != list.get(index).getResult()) { wrong_num++; wrong.setText("答错:"+wrong_num); Log.d("jhd", "已答错数目:" + wrong_num); } index++; completed.setText("已答:"+(index)); if(index==list.size()) { Toast.makeText(getApplicationContext(), "题目已经答完!", Toast.LENGTH_SHORT).show(); return; } tv_exp.setText(list.get(index).getExp()); et_result.setText(""); } }); exit.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub finish(); } }); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub } }
3.InputAvctivity.java
package com.example.cal; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class InputActivity extends Activity implements OnClickListener{ private EditText et_num; private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_input); et_num=(EditText)findViewById(R.id.et_num); btn=(Button)findViewById(R.id.num_btn); btn.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(et_num!=null){ int n=Integer.valueOf(et_num.getText().toString()); Intent intent=new Intent(InputActivity.this,ExpActivity.class); if(n==0){ Toast.makeText(getApplicationContext(), "请输入大于0的整数!", Toast.LENGTH_SHORT).show(); } intent.putExtra("num", n); startActivity(intent); } } }
4.MainActivuty.java
package com.example.cal; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button enterBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); enterBtn=(Button)findViewById(R.id.enter_btn); enterBtn.setOnClickListener(this); } @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,InputActivity.class); startActivity(intent); } }
5.界面 .xml
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cal.MainActivity" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="100dp" android:text="欢迎进入四则运算" android:textSize="30dp" android:gravity="center" /> <Button android:id="@+id/enter_btn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="进入" /> </LinearLayout>
<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cal.InputActivity" android:orientation="vertical"> <LinearLayout android:layout_width="fill_parent" android:layout_height="50dp" android:orientation="horizontal" > <EditText android:id="@+id/et_num" android:layout_width="0dp" android:layout_height="fill_parent" android:hint="输入算式数:" android:digits="0123456789" android:layout_weight="3" /> <Button android:id="@+id/num_btn" android:layout_width="0dp" android:layout_height="fill_parent" android:text="确认" android:layout_weight="1" /> </LinearLayout> </LinearLayout>
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.cal.ExpActivity" > <TextView android:layout_width="fill_parent" android:layout_height="30dp" android:text="运算式:" android:textSize="20dp" /> <TextView android:id="@+id/tv_exp" android:layout_width="fill_parent" android:layout_height="30dp" android:text="111" android:background="@android:color/white" android:textSize="20dp" /> <TextView android:layout_width="fill_parent" android:layout_height="30dp" android:text="请输入运算结果:" android:textSize="20dp" /> <EditText android:layout_marginTop="5dp" android:id="@+id/et_result" android:layout_width="fill_parent" android:layout_height="wrap_content" android:digits="-0123456789" android:textSize="20dp" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/next_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="下一题" /> <Button android:id="@+id/exit_btn" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="退出" /> </LinearLayout> <LinearLayout android:layout_marginTop="15dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/all" android:gravity="center_horizontal" android:layout_width="0dp" android:text="共计:" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:id="@+id/completed" android:gravity="center_horizontal" android:text="已答:" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> <TextView android:id="@+id/wrong" android:gravity="center_horizontal" android:text="答错:" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content"/> </LinearLayout> </LinearLayout>
介绍:
随机产生最多三位数,随机产生运算符,用户可选择出题数目,系统随机产生算式,用户回答,系统可判断正误。
结对的小伙伴博客:http://www.cnblogs.com/fooreveryu/