程序有难度和单项练习,但设计了每次只出5到题,如果做错的话会把错题加入到数据库中,然后通多错题巩固选项可以对错题进行训练。
代码:
普通选项代码:
1 package com.example.szys; 2 3 import java.math.*; 4 import java.util.Random; 5 6 import android.app.Activity; 7 import android.app.AlertDialog; 8 import android.content.ContentValues; 9 import android.content.DialogInterface; 10 import android.content.Intent; 11 import android.database.Cursor; 12 import android.os.Bundle; 13 import android.view.View; 14 import android.widget.Button; 15 import android.widget.EditText; 16 import android.widget.TextView; 17 18 public class CalActivity extends Activity { 19 int op; 20 int a; 21 int b; 22 int n=0; 23 int w=0; 24 String r; 25 Double answer,respon; 26 TextView textview1,textview2; 27 EditText editText; 28 Button button; 29 @Override 30 protected void onCreate(Bundle savedInstanceState) { 31 super.onCreate(savedInstanceState); 32 setContentView(R.layout.cal_main); 33 textview1=(TextView)findViewById(R.id.textView1); 34 textview2=(TextView)findViewById(R.id.textView2); 35 editText=(EditText)findViewById(R.id.EditText1); 36 a =new Random().nextInt(100); 37 b =new Random().nextInt(100); 38 op=new Random().nextInt(4); 39 switch(op) 40 { 41 case 0: 42 textview1.setText(a+"+"+b+"="); 43 answer=(double) (a+b); 44 break; 45 case 1: 46 textview1.setText(a+"-"+b+"="); 47 answer=(double) (a-b); 48 break; 49 case 2: 50 a =new Random().nextInt(10); 51 b =new Random().nextInt(10); 52 textview1.setText(a+"*"+b+"="); 53 answer=(double) (a*b); 54 break; 55 case 3: 56 a =new Random().nextInt(10); 57 b =new Random().nextInt(10); 58 while(b==0){ 59 b =new Random().nextInt(10); 60 } 61 textview1.setText(a+"/"+b+"="); 62 BigDecimal x = new BigDecimal((double)a/b); 63 answer = x.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 64 65 break; 66 } 67 button=(Button)findViewById(R.id.button4); 68 button.setOnClickListener(new Button.OnClickListener(){ 69 70 @Override 71 public void onClick(View arg0) { 72 // TODO Auto-generated method stub 73 if(!checkInteger(editText.getText().toString())) 74 { 75 AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity.this); 76 builder.setCancelable(false); 77 builder.setTitle("错误"); 78 builder.setMessage("你输入的信息有错"); 79 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 80 81 public void onClick(DialogInterface dialog, int whichButton) { 82 83 } 84 }); 85 builder.create().show(); 86 editText.setText(""); 87 return; 88 } 89 respon=Double.parseDouble(editText.getText().toString()); 90 r=textview1.getText().toString(); 91 ContentValues values = new ContentValues(); 92 values.put("problem", r); 93 values.put("answer", answer); 94 editText.setText(""); 95 n++; 96 if(respon.equals(answer)) 97 { 98 textview2.setText("你答对了!"); 99 } 100 else{ 101 102 DBHelper helper = new DBHelper(getApplicationContext()); 103 final Cursor c = helper.query(); 104 helper.insert(values); 105 w++; 106 textview2.setText("你答错了!\n"+r+answer); 107 helper.close(); 108 } 109 if(n<5) 110 { 111 a =new Random().nextInt(100); 112 b =new Random().nextInt(100); 113 op=new Random().nextInt(4); 114 switch(op) 115 { 116 case 0: 117 textview1.setText(a+"+"+b+"="); 118 answer=(double) (a+b); 119 break; 120 case 1: 121 textview1.setText(a+"-"+b+"="); 122 answer=(double) (a-b); 123 break; 124 case 2: 125 a =new Random().nextInt(10); 126 b =new Random().nextInt(10); 127 textview1.setText(a+"*"+b+"="); 128 answer=(double) (a*b); 129 break; 130 case 3: 131 a =new Random().nextInt(10); 132 b =new Random().nextInt(10); 133 while(b==0){ 134 b =new Random().nextInt(10); 135 } 136 textview1.setText(a+"/"+b+"="); 137 BigDecimal x = new BigDecimal((double)a/b); 138 answer = x.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); 139 140 break; 141 } 142 } 143 else 144 { 145 int right=n-w; 146 double rvate=(double)right/n*100; 147 AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity.this); 148 builder.setCancelable(false); 149 builder.setTitle("结束"); 150 builder.setMessage("你答对了"+right+"题"+"\n"+"答错了"+w+"题"+"\n"+"正确率为"+rvate+"%"); 151 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { 152 153 public void onClick(DialogInterface dialog, int whichButton) { 154 Intent intent = new Intent(CalActivity.this, MainActivity.class); 155 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 156 startActivity(intent); 157 } 158 }); 159 builder.create().show(); 160 } 161 } 162 163 }); 164 165 } 166 public boolean checkInteger(String text) { 167 /* 当输入的文本去掉前后空格长度为0时返回false */ 168 if(text.trim().length()==0){ 169 return false; 170 } 171 try{ 172 Double.parseDouble(text); 173 }catch(Exception e){ 174 /* 无法转换为整数时返回false */ 175 return false; 176 } 177 return true; 178 } 179 180 181 }