四則運算app總結

39吳梓健發表於2015-06-23

 

 

程式有難度和單項練習,但設計了每次只出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 }
CalActivity

困難選項程式碼:

  1 package com.example.szys;
  2 
  3 import java.math.BigDecimal;
  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 CalActivity1 extends Activity{
 19     int op;
 20     int a;
 21     int b;
 22     int c;
 23     int d;
 24     int n=0;
 25     int w=0;
 26     String r;
 27     Double answer,respon;
 28     TextView textview1,textview2;
 29     EditText editText;
 30     Button button;
 31      @Override
 32      protected void onCreate(Bundle savedInstanceState) {
 33          super.onCreate(savedInstanceState);
 34          setContentView(R.layout.cal_main);
 35          textview1=(TextView)findViewById(R.id.textView1);
 36          textview2=(TextView)findViewById(R.id.textView2);
 37          editText=(EditText)findViewById(R.id.EditText1);
 38              a =new Random().nextInt(100);
 39             b =new Random().nextInt(10);
 40             c =new Random().nextInt(100);
 41             d =new Random().nextInt(10);
 42             op=new Random().nextInt(4);
 43             switch(op)
 44             {
 45             case 0:
 46                 while(d==0){
 47                     d =new Random().nextInt(10);
 48                 }
 49                 textview1.setText("("+a+"+"+b+")"+"/"+d+"=");
 50                 BigDecimal   x   =   new   BigDecimal((double) (a+b)/d);  
 51                 answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
 52                 break;
 53             case 1:
 54                 textview1.setText(d+"*"+a+"-"+b+"=");
 55                 answer=(double) (d*a-b);
 56                 break;
 57             case 2:
 58                 textview1.setText(a+"*"+b+"+"+c+"=");
 59                 answer=(double) (a*b+c);
 60                 break;
 61             case 3:
 62                 while(c==0){
 63                     c =new Random().nextInt(100);
 64                 }
 65                 textview1.setText(a+"/"+c+"=");
 66                 BigDecimal y  =   new   BigDecimal((double)a/c);  
 67                 answer  =   y.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
 68                 
 69                 break;
 70             }
 71             button=(Button)findViewById(R.id.button4);
 72             button.setOnClickListener(new Button.OnClickListener(){
 73 
 74                 @Override
 75                 public void onClick(View arg0) {
 76                     // TODO Auto-generated method stub
 77                     if(!checkInteger(editText.getText().toString()))
 78                     {
 79                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity1.this); 
 80                         builder.setCancelable(false);
 81                         builder.setTitle("錯誤");  
 82                         builder.setMessage("你輸入的資訊有錯");  
 83                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 84                             
 85                             public void onClick(DialogInterface dialog, int whichButton) {  
 86                                 
 87                             }  
 88                         }); 
 89                         builder.create().show();
 90                         editText.setText("");
 91                         return;
 92                     }
 93                     respon=Double.parseDouble(editText.getText().toString());
 94                     r=textview1.getText().toString();
 95                     ContentValues values = new ContentValues();
 96                     values.put("problem", r);  
 97                     values.put("answer", answer); 
 98                     editText.setText("");
 99                     n++;
100                     if(respon.equals(answer))
101                     {
102                         textview2.setText("你答對了!");
103                     }
104                     else{
105                         DBHelper helper = new DBHelper(getApplicationContext());
106                         final Cursor c = helper.query();
107                         helper.insert(values);
108                         w++;
109                         textview2.setText("你答錯了!\n"+r+answer);
110                         helper.close();
111                     }
112                     if(n<5)
113                     {
114                         a =new Random().nextInt(100);
115                         b =new Random().nextInt(100);
116                         op=new Random().nextInt(4);
117                         switch(op)
118                         {
119                         case 0:
120                             while(d==0){
121                                 d =new Random().nextInt(10);
122                             }
123                             textview1.setText("("+a+"+"+b+")"+"/"+d+"=");
124                             BigDecimal   x   =   new   BigDecimal((double) (a+b)/d);  
125                             answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();
126                             break;
127                         case 1:
128                             textview1.setText(d+"*"+a+"-"+b+"=");
129                             answer=(double) (d*a-b);
130                             break;
131                         case 2:
132                             textview1.setText(a+"*"+b+"+"+c+"=");
133                             answer=(double) (a*b+c);
134                             break;
135                         case 3:
136                             while(c==0){
137                                 c =new Random().nextInt(100);
138                             }
139                             textview1.setText(a+"/"+c+"=");
140                             BigDecimal   y   =   new   BigDecimal((double)a/c);  
141                             answer  =   y.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
142                             
143                             break;
144                         }
145                     }
146                     else
147                     {
148                         int right=n-w;
149                         double rvate=(double)right/n*100;
150                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity1.this); 
151                         builder.setCancelable(false);
152                         builder.setTitle("結束");  
153                         builder.setMessage("你答對了"+right+"題"+"\n"+"答錯了"+w+"題"+"\n"+"正確率為"+rvate+"%");  
154                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
155                             
156                             public void onClick(DialogInterface dialog, int whichButton) {  
157                                 Intent intent = new Intent(CalActivity1.this, MainActivity.class); 
158                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
159                                 startActivity(intent);
160                             }  
161                         });  
162                         builder.create().show();
163                     }
164                 }
165                 
166             });
167            
168      }
169      public boolean checkInteger(String text) {
170          /*      當輸入的文字去掉前後空格長度為0時返回false  */
171          if(text.trim().length()==0){
172                  return false;
173          }
174          try{
175                  Double.parseDouble(text);
176          }catch(Exception e){
177                  /*      無法轉換為整數時返回false   */
178                  return false;
179          }
180          return true;
181  }
182 }
CalActivity1

加法練習程式碼:

  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 CalActivity2 extends Activity {
 19     int a;
 20     int b;
 21     int n=0;
 22     int w=0;
 23     String r;
 24     Double answer,respon;
 25     TextView textview1,textview2;
 26     EditText editText;
 27     Button button;
 28      @Override
 29      protected void onCreate(Bundle savedInstanceState) {
 30          super.onCreate(savedInstanceState);
 31          setContentView(R.layout.cal_main);
 32          textview1=(TextView)findViewById(R.id.textView1);
 33          textview2=(TextView)findViewById(R.id.textView2);
 34          editText=(EditText)findViewById(R.id.EditText1);
 35              a =new Random().nextInt(100);
 36             b =new Random().nextInt(100);
 37            
 38                 textview1.setText(a+"+"+b+"=");
 39                 answer=(double) (a+b);
 40                
 41             button=(Button)findViewById(R.id.button4);
 42             button.setOnClickListener(new Button.OnClickListener(){
 43 
 44                 @Override
 45                 public void onClick(View arg0) {
 46                     // TODO Auto-generated method stub
 47                     if(!checkInteger(editText.getText().toString()))
 48                     {
 49                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity2.this); 
 50                         builder.setCancelable(false);
 51                         builder.setTitle("錯誤");  
 52                         builder.setMessage("你輸入的資訊有錯");  
 53                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 54                             
 55                             public void onClick(DialogInterface dialog, int whichButton) {  
 56                                 
 57                             }  
 58                         }); 
 59                         builder.create().show();
 60                         editText.setText("");
 61                         return;
 62                     }
 63                     respon=Double.parseDouble(editText.getText().toString());
 64                     r=textview1.getText().toString();
 65                     ContentValues values = new ContentValues();
 66                     values.put("problem", r);  
 67                     values.put("answer", answer); 
 68                     editText.setText("");
 69                     n++;
 70                     if(respon.equals(answer))
 71                     {
 72                         textview2.setText("你答對了!");
 73                     }
 74                     else{
 75                         DBHelper helper = new DBHelper(getApplicationContext());
 76                         final Cursor c = helper.query();
 77                         helper.insert(values);
 78                         w++;
 79                         textview2.setText("你答錯了!\n"+r+answer);
 80                         helper.close();
 81                     }
 82                     if(n<5)
 83                     {
 84                         a =new Random().nextInt(100);
 85                         b =new Random().nextInt(100);
 86                        
 87                             textview1.setText(a+"+"+b+"=");
 88                             answer=(double) (a+b);
 89                            
 90                     }
 91                     else
 92                     {
 93                         int right=n-w;
 94                         double rvate=(double)right/n*100;
 95                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity2.this); 
 96                         builder.setCancelable(false);
 97                         builder.setTitle("結束");  
 98                         builder.setMessage("你答對了"+right+"題"+"\n"+"答錯了"+w+"題"+"\n"+"正確率為"+rvate+"%");  
 99                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
100                             
101                             public void onClick(DialogInterface dialog, int whichButton) {  
102                                 Intent intent = new Intent(CalActivity2.this, MainActivity.class); 
103                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
104                                 startActivity(intent);
105                             }  
106                         });  
107                         builder.create().show();
108                     }
109                 }
110                 
111             });
112 
113      }
114      public boolean checkInteger(String text) {
115          /*      當輸入的文字去掉前後空格長度為0時返回false  */
116          if(text.trim().length()==0){
117                  return false;
118          }
119          try{
120                  Double.parseDouble(text);
121          }catch(Exception e){
122                  /*      無法轉換為整數時返回false   */
123                  return false;
124          }
125          return true;
126  }
127 }
CalActivity2

減法練習程式碼:

  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 CalActivity3 extends Activity {
 19     
 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            
 39                 textview1.setText(a+"-"+b+"=");
 40                 answer=(double) (a-b);
 41                
 42             button=(Button)findViewById(R.id.button4);
 43             button.setOnClickListener(new Button.OnClickListener(){
 44 
 45                 @Override
 46                 public void onClick(View arg0) {
 47                     // TODO Auto-generated method stub
 48                     if(!checkInteger(editText.getText().toString()))
 49                     {
 50                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity3.this); 
 51                         builder.setCancelable(false);
 52                         builder.setTitle("錯誤");  
 53                         builder.setMessage("你輸入的資訊有錯");  
 54                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 55                             
 56                             public void onClick(DialogInterface dialog, int whichButton) {  
 57                                 
 58                             }  
 59                         }); 
 60                         builder.create().show();
 61                         editText.setText("");
 62                         return;
 63                     }
 64                     respon=Double.parseDouble(editText.getText().toString());
 65                     r=textview1.getText().toString();
 66                     ContentValues values = new ContentValues();
 67                     values.put("problem", r);  
 68                     values.put("answer", answer); 
 69                     editText.setText("");
 70                     n++;
 71                     if(respon.equals(answer))
 72                     {
 73                         textview2.setText("你答對了!");
 74                     }
 75                     else{
 76                         DBHelper helper = new DBHelper(getApplicationContext());
 77                         final Cursor c = helper.query();
 78                         helper.insert(values);
 79                         w++;
 80                         textview2.setText("你答錯了!\n"+r+answer);
 81                         helper.close();
 82                     }
 83                     if(n<5)
 84                     {
 85                         a =new Random().nextInt(100);
 86                         b =new Random().nextInt(100);
 87                        
 88                             textview1.setText(a+"-"+b+"=");
 89                             answer=(double) (a-b);
 90                     }
 91                     else
 92                     {
 93                         int right=n-w;
 94                         double rvate=(double)right/n*100;
 95                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity3.this);  
 96                         builder.setCancelable(false);
 97                         builder.setTitle("結束");  
 98                         builder.setMessage("你答對了"+right+"題"+"\n"+"答錯了"+w+"題"+"\n"+"正確率為"+rvate+"%");  
 99                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
100                             
101                             public void onClick(DialogInterface dialog, int whichButton) {  
102                                 Intent intent = new Intent(CalActivity3.this, MainActivity.class); 
103                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
104                                 startActivity(intent);
105                             }  
106                         });  
107                         builder.create().show();
108                     }
109                 }
110                 
111             });
112 
113      }
114      public boolean checkInteger(String text) {
115          /*      當輸入的文字去掉前後空格長度為0時返回false  */
116          if(text.trim().length()==0){
117                  return false;
118          }
119          try{
120                  Double.parseDouble(text);
121          }catch(Exception e){
122                  /*      無法轉換為整數時返回false   */
123                  return false;
124          }
125          return true;
126  }
127 }
CalActivity3

乘法練習程式碼:

  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 CalActivity4 extends Activity {
 19     
 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             
 39                 textview1.setText(a+"*"+b+"=");
 40                 answer=(double) (a*b);
 41                
 42             button=(Button)findViewById(R.id.button4);
 43             button.setOnClickListener(new Button.OnClickListener(){
 44 
 45                 @Override
 46                 public void onClick(View arg0) {
 47                     // TODO Auto-generated method stub
 48                     if(!checkInteger(editText.getText().toString()))
 49                     {
 50                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity4.this); 
 51                         builder.setCancelable(false);
 52                         builder.setTitle("錯誤");  
 53                         builder.setMessage("你輸入的資訊有錯");  
 54                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 55                             
 56                             public void onClick(DialogInterface dialog, int whichButton) {  
 57                                 
 58                             }  
 59                         }); 
 60                         builder.create().show();
 61                         editText.setText("");
 62                         return;
 63                     }
 64                     respon=Double.parseDouble(editText.getText().toString());
 65                     r=textview1.getText().toString();
 66                     ContentValues values = new ContentValues();
 67                     values.put("problem", r);  
 68                     values.put("answer", answer); 
 69                     editText.setText("");
 70                     n++;
 71                     if(respon.equals(answer))
 72                     {
 73                         textview2.setText("你答對了!");
 74                     }
 75                     else{
 76                         DBHelper helper = new DBHelper(getApplicationContext());
 77                         final Cursor c = helper.query();
 78                         helper.insert(values);
 79                         w++;
 80                         textview2.setText("你答錯了!\n"+r+answer);
 81                         helper.close();
 82                     }
 83                     if(n<5)
 84                     {
 85                         a =new Random().nextInt(100);
 86                         b =new Random().nextInt(100);
 87                        
 88                             textview1.setText(a+"*"+b+"=");
 89                             answer=(double) (a*b);
 90                         
 91                     }
 92                     else
 93                     {
 94                         int right=n-w;
 95                         double rvate=(double)right/n*100;
 96                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity4.this);  
 97                         builder.setCancelable(false);
 98                         builder.setTitle("結束");  
 99                         builder.setMessage("你答對了"+right+"題"+"\n"+"答錯了"+w+"題"+"\n"+"正確率為"+rvate+"%");  
100                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
101                             
102                             public void onClick(DialogInterface dialog, int whichButton) {  
103                                 Intent intent = new Intent(CalActivity4.this, MainActivity.class); 
104                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
105                                 startActivity(intent);
106                             }  
107                         });  
108                         builder.create().show();
109                     }
110                 }
111                 
112             });
113 
114      }
115      public boolean checkInteger(String text) {
116          /*      當輸入的文字去掉前後空格長度為0時返回false  */
117          if(text.trim().length()==0){
118                  return false;
119          }
120          try{
121                  Double.parseDouble(text);
122          }catch(Exception e){
123                  /*      無法轉換為整數時返回false   */
124                  return false;
125          }
126          return true;
127  }
128 }
CalActivity4

除法練習程式碼:

  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 CalActivity5 extends Activity {
 19     
 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             
 39                 while(b==0){
 40                     b =new Random().nextInt(100);
 41                 }
 42                 textview1.setText(a+"/"+b+"=");
 43                 BigDecimal   x   =   new   BigDecimal((double)a/b);  
 44                 answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
 45                 
 46                
 47             button=(Button)findViewById(R.id.button4);
 48             button.setOnClickListener(new Button.OnClickListener(){
 49 
 50                 @Override
 51                 public void onClick(View arg0) {
 52                     // TODO Auto-generated method stub
 53                     if(!checkInteger(editText.getText().toString()))
 54                     {
 55                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity5.this); 
 56                         builder.setCancelable(false);
 57                         builder.setTitle("錯誤");  
 58                         builder.setMessage("你輸入的資訊有錯");  
 59                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 60                             
 61                             public void onClick(DialogInterface dialog, int whichButton) {  
 62                                 
 63                             }  
 64                         }); 
 65                         builder.create().show();
 66                         editText.setText("");
 67                         return;
 68                     }
 69                     respon=Double.parseDouble(editText.getText().toString());
 70                     r=textview1.getText().toString();
 71                     ContentValues values = new ContentValues();
 72                     values.put("problem", r);  
 73                     values.put("answer", answer); 
 74                     editText.setText("");
 75                     n++;
 76                     if(respon.equals(answer))
 77                     {
 78                         textview2.setText("你答對了!");
 79                     }
 80                     else{
 81                         DBHelper helper = new DBHelper(getApplicationContext());
 82                         final Cursor c = helper.query();
 83                         helper.insert(values);
 84                         w++;
 85                         textview2.setText("你答錯了!\n"+r+answer);
 86                         helper.close();
 87                     }
 88                     if(n<5)
 89                     {
 90                         a =new Random().nextInt(100);
 91                         b =new Random().nextInt(100);
 92                        
 93                             while(b==0){
 94                                 b =new Random().nextInt(100);
 95                             }
 96                             textview1.setText(a+"/"+b+"=");
 97                             BigDecimal   x   =   new   BigDecimal((double)a/b);  
 98                             answer  =   x.setScale(2,   BigDecimal.ROUND_HALF_UP).doubleValue();  
 99                            
100                         
101                     }
102                     else
103                     {
104                         int right=n-w;
105                         double rvate=(double)right/n*100;
106                         AlertDialog.Builder builder = new AlertDialog.Builder(CalActivity5.this);   
107                         builder.setCancelable(false);
108                         builder.setTitle("結束");  
109                         builder.setMessage("你答對了"+right+"題"+"\n"+"答錯了"+w+"題"+"\n"+"正確率為"+rvate+"%");  
110                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
111                             
112                             public void onClick(DialogInterface dialog, int whichButton) {  
113                                 Intent intent = new Intent(CalActivity5.this, MainActivity.class); 
114                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
115                                 startActivity(intent);
116                             }  
117                         });  
118                         builder.create().show();
119                     }
120                 }
121                 
122             });
123 
124      }
125      public boolean checkInteger(String text) {
126          /*      當輸入的文字去掉前後空格長度為0時返回false  */
127          if(text.trim().length()==0){
128                  return false;
129          }
130          try{
131                  Double.parseDouble(text);
132          }catch(Exception e){
133                  /*      無法轉換為整數時返回false   */
134                  return false;
135          }
136          return true;
137  }
138 }
CalActivity5

主頁面程式碼:

  1 package com.example.szys;
  2 
  3 import android.app.Activity;
  4 import android.app.AlertDialog;
  5 import android.content.DialogInterface;
  6 import android.content.Intent;
  7 import android.database.Cursor;
  8 import android.os.Bundle;
  9 import android.view.View;
 10 import android.widget.Button;
 11 
 12 public class MainActivity extends Activity {
 13     
 14     Button start,end,wa;
 15     int yourChose=-1;  
 16 
 17     private void showSinChosDia()  
 18     {  
 19         final String[] mList={"普通","困難","加法練習","減法練習","乘法練習","除法練習"};  
 20         yourChose=-1;  
 21         AlertDialog.Builder sinChosDia=new AlertDialog.Builder(MainActivity.this);  
 22         sinChosDia.setTitle("難度/單項練習");  
 23         sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {  
 24               
 25             @Override  
 26             public void onClick(DialogInterface dialog, int which) {  
 27                 // TODO Auto-generated method stub  
 28                 yourChose=which;  
 29                   
 30             }  
 31         });  
 32         sinChosDia.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 33               
 34             @Override  
 35             public void onClick(DialogInterface dialog, int which) {  
 36                 // TODO Auto-generated method stub  
 37                 
 38                 
 39                 if(yourChose==0)  
 40                 {  
 41                     Intent intent = new Intent(MainActivity.this, CalActivity.class); 
 42                     startActivity(intent); 
 43                 }  
 44                 if(yourChose==1)
 45                 {
 46                     Intent intent = new Intent(MainActivity.this, CalActivity1.class); 
 47                     startActivity(intent);
 48                 }
 49                 if(yourChose==2)
 50                 {
 51                     Intent intent = new Intent(MainActivity.this, CalActivity2.class); 
 52                     startActivity(intent);
 53                 }
 54                 if(yourChose==3)
 55                 {
 56                     Intent intent = new Intent(MainActivity.this, CalActivity3.class); 
 57                     startActivity(intent);
 58                 }
 59                 if(yourChose==4)
 60                 {
 61                     Intent intent = new Intent(MainActivity.this, CalActivity4.class); 
 62                     startActivity(intent);
 63                 }
 64                 if(yourChose==5)
 65                 {
 66                     Intent intent = new Intent(MainActivity.this, CalActivity5.class); 
 67                     startActivity(intent);
 68                 }
 69             }  
 70         });  
 71         sinChosDia.create().show();  
 72     }  
 73    
 74     @Override
 75     
 76     protected void onCreate(Bundle savedInstanceState) {
 77         super.onCreate(savedInstanceState);
 78         setContentView(R.layout.activity_main);
 79         final DBHelper helper = new DBHelper(this);
 80         final Cursor c = helper.query();
 81         start=(Button) findViewById(R.id.button1);
 82         start.setOnClickListener(new Button.OnClickListener(){
 83         
 84             @Override
 85             
 86             public void onClick(View v) {
 87                 // TODO Auto-generated method stub
 88                 
 89                 showSinChosDia();
 90                 
 91             }
 92         });
 93         wa=(Button) findViewById(R.id.button2);
 94         wa.setOnClickListener(new Button.OnClickListener(){
 95             
 96             @Override
 97             public void onClick(View v) {
 98                 // TODO Auto-generated method stub
 99                 if(!c.moveToNext())
100                 {
101                     helper.close();
102                     AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
103                     builder.setCancelable(false);
104                     builder.setTitle("結束");  
105                     builder.setMessage("無錯題");  
106                     builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
107                         
108                         public void onClick(DialogInterface dialog, int whichButton) {  
109                         }  
110                     });  
111                     builder.create().show();
112                 }
113                 else{
114                     Intent intent = new Intent(MainActivity.this, ShowDB.class); 
115                     startActivity(intent);
116                 }
117             }
118         });
119         end=(Button) findViewById(R.id.button3);
120         end.setOnClickListener(new Button.OnClickListener(){
121             
122             @Override
123             public void onClick(View v) {
124                 // TODO Auto-generated method stub
125                 System.exit(0);
126             }
127         });
128     }
129    
130 }
MainActivity

資料庫程式碼:

 1 package com.example.szys;
 2 
 3 import android.content.ContentValues;
 4 import android.content.Context;
 5 import android.database.Cursor;
 6 import android.database.sqlite.SQLiteDatabase;
 7 import android.database.sqlite.SQLiteOpenHelper;
 8 public class DBHelper extends SQLiteOpenHelper {
 9     private static final String DB_NAME = "coll.db";
10     private static final String TBL_NAME = "CollTbl";
11     private static final String CREATE_TBL = " create table "
12             + " CollTbl(_id integer primary key autoincrement,problem text,answer text) ";
13     
14     private SQLiteDatabase db;
15     DBHelper(Context c) {
16         super(c, DB_NAME, null, 2);
17     }
18     @Override
19     public void onCreate(SQLiteDatabase db) {
20         this.db = db;
21         db.execSQL(CREATE_TBL);
22     }
23     public void insert(ContentValues values) {
24         SQLiteDatabase db = getWritableDatabase();
25         db.insert(TBL_NAME, null, values);
26         db.close();
27     }
28     public Cursor query() {
29         SQLiteDatabase db = getWritableDatabase();
30         Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
31         return c;
32     }
33     public void del(int id) {
34         if (db == null)
35             db = getWritableDatabase();
36         db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });
37     }
38     public void close() {
39         if (db != null)
40             db.close();
41     }
42     @Override
43     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44     }
45 }
DBHelper

錯題鞏固程式碼:

  1 package com.example.szys;
  2 
  3 import java.math.BigDecimal;
  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.database.sqlite.SQLiteDatabase;
 13 import android.os.Bundle;
 14 import android.view.View;
 15 import android.widget.Button;
 16 import android.widget.EditText;
 17 import android.widget.TextView;
 18 
 19 public class ShowDB extends Activity {
 20     int op;
 21     int a;
 22     int b;
 23     int n=0;
 24     int w=0;
 25     String r;
 26     Double respon,answer;
 27     String problem;
 28     TextView textview1,textview2;
 29     EditText editText;
 30     Button button,button1,button2,button3;
 31      @Override
 32      protected void onCreate(Bundle savedInstanceState) {
 33          super.onCreate(savedInstanceState);
 34          setContentView(R.layout.show_maiin);
 35          textview1=(TextView)findViewById(R.id.textView1);
 36          textview2=(TextView)findViewById(R.id.textView2);
 37          editText=(EditText)findViewById(R.id.EditText1);
 38          final DBHelper helper = new DBHelper(this);
 39          final Cursor c = helper.query();
 40             c.moveToNext();
 41             problem = c.getString(c.getColumnIndex("problem"));  
 42             answer = Double.parseDouble(c.getString(c.getColumnIndex("answer")));  
 43             textview1.setText(problem);
 44             button=(Button)findViewById(R.id.button6);
 45             button.setOnClickListener(new Button.OnClickListener(){
 46 
 47                 @Override
 48                 public void onClick(View arg0) {
 49                     // TODO Auto-generated method stub
 50                     if(!checkInteger(editText.getText().toString()))
 51                     {
 52                         AlertDialog.Builder builder = new AlertDialog.Builder(ShowDB.this); 
 53                         builder.setCancelable(false);
 54                         builder.setTitle("錯誤");  
 55                         builder.setMessage("你輸入的資訊有錯");  
 56                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 57                             
 58                             public void onClick(DialogInterface dialog, int whichButton) {  
 59                                 
 60                             }  
 61                         }); 
 62                         builder.create().show();
 63                         editText.setText("");
 64                         return;
 65                     }
 66                     respon=Double.parseDouble(editText.getText().toString());
 67                     if(respon.equals(answer))
 68                     {
 69                         textview2.setText("你答對了!");
 70                         editText.setText("");
 71                     }
 72                     else{
 73                         w++;
 74                         textview2.setText("你答錯了!\n"+problem+answer);
 75                         editText.setText("");
 76                     }
 77                     if(!c.moveToNext())
 78                     {
 79                         AlertDialog.Builder builder = new AlertDialog.Builder(ShowDB.this); 
 80                         builder.setCancelable(false);
 81                         builder.setTitle("結束");  
 82                         builder.setMessage("");  
 83                         builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
 84                             
 85                             public void onClick(DialogInterface dialog, int whichButton) {  
 86                                 Intent intent = new Intent(ShowDB.this, MainActivity.class); 
 87                                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 88                                 startActivity(intent);
 89                             }  
 90                         });  
 91                         builder.create().show();
 92                     }
 93                 }
 94                 
 95             });
 96             button1=(Button)findViewById(R.id.button7);
 97             button1.setOnClickListener(new Button.OnClickListener(){
 98 
 99                 @Override
100                 public void onClick(View arg0) {
101                     // TODO Auto-generated method stub
102                     
103                     if(c.moveToNext())
104                     {
105                         editText.setText("");
106                         problem = c.getString(c.getColumnIndex("problem"));  
107                         answer = Double.parseDouble(c.getString(c.getColumnIndex("answer")));   
108                         textview1.setText(problem);
109                     }
110                 }
111                 
112             });
113             button2=(Button)findViewById(R.id.button8);
114             button2.setOnClickListener(new Button.OnClickListener(){
115 
116                 @Override
117                 public void onClick(View arg0) {
118                     // TODO Auto-generated method stub
119                     helper.del(c.getInt(0));
120                     AlertDialog.Builder builder = new AlertDialog.Builder(ShowDB.this); 
121                     builder.setCancelable(false);
122                     builder.setTitle("提示");  
123                     builder.setMessage("刪除成功");  
124                     builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
125                         
126                         public void onClick(DialogInterface dialog, int whichButton) {  
127                             if(c.moveToNext())
128                             {
129                                 editText.setText("");
130                                 problem = c.getString(c.getColumnIndex("problem"));  
131                                 answer = Double.parseDouble(c.getString(c.getColumnIndex("answer")));   
132                                 textview1.setText(problem);
133                             }
134                             else if(!c.moveToNext())
135                             {
136                                 textview1.setText("");
137                                 AlertDialog.Builder builder = new AlertDialog.Builder(ShowDB.this); 
138                                 builder.setCancelable(false);
139                                 builder.setTitle("結束");  
140                                 builder.setMessage("已無錯題");  
141                                 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {  
142                                     
143                                     public void onClick(DialogInterface dialog, int whichButton) {  
144                                         Intent intent = new Intent(ShowDB.this, MainActivity.class); 
145                                         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
146                                         startActivity(intent);
147                                     }  
148                                 });  
149                                 builder.create().show();
150                             }
151                         }  
152                     });  
153                     builder.create().show();
154                     }
155                 
156                 
157             });
158             button3=(Button)findViewById(R.id.button9);
159             button3.setOnClickListener(new Button.OnClickListener(){
160 
161                 @Override
162                 public void onClick(View arg0) {
163                     // TODO Auto-generated method stub
164                     
165                     Intent intent = new Intent(ShowDB.this, MainActivity.class); 
166                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
167                     startActivity(intent);
168                 }
169                 
170             });
171            
172      }
173      public boolean checkInteger(String text) {
174          /*      當輸入的文字去掉前後空格長度為0時返回false  */
175          if(text.trim().length()==0){
176                  return false;
177          }
178          try{
179                  Double.parseDouble(text);
180          }catch(Exception e){
181                  /*      無法轉換為整數時返回false   */
182                  return false;
183          }
184          return true;
185  }
186 
187 }
ShowDB

 

XML程式碼

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/container"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:background="@drawable/bg1">
 8 
 9     <LinearLayout
10         android:layout_width="fill_parent"
11         android:layout_height="wrap_content"
12         android:orientation="vertical" 
13         android:layout_gravity="center">
14         
15         <Button
16         android:id="@+id/button1"
17         android:layout_width="fill_parent"
18         android:layout_height="wrap_content"
19         android:text="開始" />
20 
21             <Button
22         android:id="@+id/button2"
23         android:layout_width="fill_parent"
24         android:layout_height="wrap_content"
25         android:text="錯題鞏固" />
26 
27             <Button
28         android:id="@+id/button3"
29         android:layout_width="fill_parent"
30         android:layout_height="wrap_content"
31         android:text="退出" />
32 
33     </LinearLayout>
34 
35     
36 
37 </FrameLayout>
activity_main
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:background="@drawable/bg1">
 7 
 8     <LinearLayout
 9        android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:orientation="vertical" 
12         android:layout_gravity="center">
13 
14         <LinearLayout
15             android:layout_width="match_parent"
16             android:layout_height="wrap_content"
17             android:layout_gravity="center" >
18 
19             <TextView
20                 android:id="@+id/textView1"
21                 android:layout_width="93dp"
22                 android:layout_height="wrap_content"
23                 android:editable="true"
24                 android:text="" />
25 
26             <EditText
27                 android:id="@+id/EditText1"
28                 android:layout_width="wrap_content"
29                 android:layout_height="wrap_content"
30                 android:digits="1234567890.-"
31                 android:ems="10"
32                 android:numeric="decimal"
33                 android:text="" >
34 
35                 <requestFocus />
36             </EditText>
37         </LinearLayout>
38 
39         <Button
40         android:id="@+id/button4"
41         android:layout_width="match_parent"
42         android:layout_height="wrap_content"
43         android:text="確定" />
44 
45         <TextView
46             android:id="@+id/textView2"
47             android:layout_width="match_parent"
48             android:layout_height="70dp"
49             android:editable="true"
50             android:text="" />
51         
52     </LinearLayout>
53 
54 </LinearLayout>
cal_main
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:background="@drawable/bg1">
 7 
 8     <LinearLayout
 9        android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:orientation="vertical" 
12         android:layout_gravity="center">
13 
14         <LinearLayout
15             android:layout_width="match_parent"
16             android:layout_height="wrap_content"
17             android:layout_gravity="center" >
18 
19             <TextView
20                 android:id="@+id/textView1"
21                 android:layout_width="93dp"
22                 android:layout_height="wrap_content"
23                 android:editable="true"
24                 android:text="" />
25 
26             <EditText
27                 android:id="@+id/EditText1"
28                 android:layout_width="wrap_content"
29                 android:layout_height="wrap_content"
30                 android:digits="1234567890.-"
31                 android:ems="10"
32                 android:numeric="decimal"
33                 android:text="" >
34 
35                 <requestFocus />
36             </EditText>
37         </LinearLayout>
38 
39         <Button
40         android:id="@+id/button6"
41         android:layout_width="match_parent"
42         android:layout_height="wrap_content"
43         android:text="確定" />
44         <Button
45         android:id="@+id/button7"
46         android:layout_width="match_parent"
47         android:layout_height="wrap_content"
48         android:text="下一題" />
49         <Button
50         android:id="@+id/button8"
51         android:layout_width="match_parent"
52         android:layout_height="wrap_content"
53         android:text="從錯題庫刪除" />
54         <Button
55         android:id="@+id/button9"
56         android:layout_width="match_parent"
57         android:layout_height="wrap_content"
58         android:text="返回" />
59 
60         <TextView
61             android:id="@+id/textView2"
62             android:layout_width="match_parent"
63             android:layout_height="70dp"
64             android:editable="true"
65             android:text="" />
66         
67     </LinearLayout>
68 
69 </LinearLayout>
show_main

 

 

可以下載apk:

http://pan.baidu.com/s/1eQjfcSq

 

相關文章