四则运算4

      这次编程主要是对我们之前编写的C++四则运算进行移植,将其移植到安卓手机平台,因为之前做的是C++版本的,所以做起来饶了很多弯路,需要现将其改编为Java语言,在进行后续工作。

      这个安卓程序我们命名为《易百分》,它主有三个Activity界面,第一个上边输入想要做的题目数量,用intent将其传值道第二个Activity界面上,然后根据这些条件出题,每个题目由3-5个数组成,最后能算出答案并进行比较,结束后会统计做对的题目数量,然后给出正确率。第三个界面则是对这个软件的介绍了,如何使用和版本等信息。

      我们的代码主要如下:      第一个布局文件:

 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:background="@drawable/background"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:id="@+id/textView1"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_gravity="center"
13         android:layout_marginTop="40dp"
14         android:text="易百分"
15         android:textColor="#0000FF"
16         android:textSize="30dp" />
17 
18     <LinearLayout
19         android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:layout_weight="0.02"
22         android:orientation="vertical" >
23 
24         <TextView
25             android:id="@+id/textView2"
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:layout_gravity="center_horizontal"
29             android:layout_marginTop="30dp"
30             android:text="请输入题目数量"
31             android:textSize="20sp" />
32 
33         <EditText
34             android:id="@+id/edtnumber"
35             android:layout_width="107dp"
36             android:layout_height="wrap_content"
37             android:layout_gravity="center"
38             android:layout_marginTop="10dp"
39             android:ems="10"
40             android:inputType="numberSigned" >
41 
42             <requestFocus />
43         </EditText>
44 
45         <RadioGroup
46             android:id="@+id/radioGroup1"
47             android:layout_width="wrap_content"
48             android:layout_height="wrap_content"
49             android:layout_gravity="center"
50             android:layout_marginTop="20dp" >
51 
52             <RadioButton
53                 android:id="@+id/radio0"
54                 android:layout_width="wrap_content"
55                 android:layout_height="wrap_content"
56                 android:checked="true"
57                 android:text="一位数运算" />
58 
59             <RadioButton
60                 android:id="@+id/radio1"
61                 android:layout_width="wrap_content"
62                 android:layout_height="wrap_content"
63                 android:layout_marginTop="10dp"
64                 android:text="两位数运算" />
65 
66         </RadioGroup>
67 
68         <Button
69             android:id="@+id/btnTijiao"
70             android:layout_width="123dp"
71             android:layout_height="wrap_content"
72             android:layout_gravity="center"
73             android:layout_marginTop="30dp"
74             android:onClick="onClickTijiao2"
75             android:text="提 交" />
76     </LinearLayout>
77 
78 </LinearLayout>

      这个界面对应的Java文件:

 1 package com.ml.caculate;
 2 
 3 import android.app.Activity;
 4 import android.app.AlertDialog;
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.ContextMenu;
 8 import android.view.ContextMenu.ContextMenuInfo;
 9 import android.view.View;
10 import android.widget.EditText;
11 import android.widget.RadioButton;
12 import android.widget.RadioGroup;
13 import android.widget.RadioGroup.OnCheckedChangeListener;
14 import android.widget.TextView;
15 
16 public class page2 extends Activity {
17     
18      TextView tv1;
19      EditText edtnum;
20      RadioButton mRadio1;
21      RadioButton mRadio2;
22      RadioGroup genderGroup;
23      String info="0";
24     @Override
25     protected void onCreate(Bundle savedInstanceState) {
26         // TODO Auto-generated method stub
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.help2);
29         tv1=(TextView)findViewById(R.id.textView2);
30         edtnum=(EditText)findViewById(R.id.edtnumber);
31         mRadio1 = (RadioButton) findViewById(R.id.radio0);
32         mRadio2 = (RadioButton) findViewById(R.id.radio1);
33         genderGroup=(RadioGroup)findViewById(R.id.radioGroup1);
34         genderGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
35             
36             @Override
37             public void onCheckedChanged(RadioGroup group, int checkedId) {
38                 // TODO Auto-generated method stub
39                 if (checkedId == mRadio1.getId()) 
40                 {
41                     info="0";
42                 }
43                 else if (checkedId == mRadio2.getId())
44                 {
45                     info="1";
46                 }
47             }
48         });
49 
50         
51     }
52 
53     @Override
54     public void onCreateContextMenu(ContextMenu menu, View v,
55             ContextMenuInfo menuInfo) {
56         // TODO Auto-generated method stub
57         
58         super.onCreateContextMenu(menu, v, menuInfo);
59     }    
60     
61     public void onClickTijiao2(View v)
62     {
63         String S1=edtnum.getText().toString();
64         if(S1.length()==0)         //判断输入框是否为空
65         {
66             new AlertDialog.Builder(this)
67             .setTitle("提示信息")
68             .setMessage("您还没有输入数量")
69             .setPositiveButton("现在输入", null)
70             .show();
71         }
72         else
73         {
74             String R=info;     //传值表示式子类型
75             String number=edtnum.getText().toString();
76             Intent intent=new Intent();
77             intent.setClass(page2.this,MainActivity.class);
78             intent.putExtra("extra", number);     //put传到另一个界面
79             intent.putExtra("redio", R);          //put传到另一个界面
80             
81             //启动
82             this.startActivity(intent); 
83         }
84         
85     }
86     
87     
88 }

       第二个布局文件,也是实现主要功能的布局:

  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:id="@+id/LinearLayout1"
  4     android:layout_width="match_parent"
  5     android:layout_height="match_parent"
  6     android:orientation="vertical"
  7     android:paddingBottom="@dimen/activity_vertical_margin"
  8     android:paddingLeft="@dimen/activity_horizontal_margin"
  9     android:paddingRight="@dimen/activity_horizontal_margin"
 10     android:paddingTop="@dimen/activity_vertical_margin"
 11     tools:context=".MainActivity" >
 12 
 13     <LinearLayout
 14         android:layout_width="wrap_content"
 15         android:layout_height="wrap_content"
 16         android:layout_gravity="center"
 17         android:gravity="center_vertical"
 18         android:orientation="vertical" >
 19 
 20         <TextView
 21             android:id="@+id/textView1"
 22             android:layout_width="wrap_content"
 23             android:layout_height="wrap_content"
 24             android:layout_gravity="center"
 25             android:layout_marginTop="24dp"
 26             android:text="小学生计算题"
 27             android:textSize="26dp" />
 28 
 29     </LinearLayout>
 30 
 31     <LinearLayout
 32         android:layout_width="match_parent"
 33         android:layout_height="wrap_content"
 34         android:layout_marginTop="30dp"
 35         android:gravity="center" >
 36 
 37         <TextView
 38             android:id="@+id/tvNum"
 39             android:layout_width="wrap_content"
 40             android:layout_height="wrap_content"
 41             android:text="请"
 42             android:textSize="20dp" />
 43 
 44         <TextView
 45             android:id="@+id/tvSuanshi"
 46             android:layout_width="wrap_content"
 47             android:layout_height="wrap_content"
 48             android:text="点击下一题开始做题"
 49             android:textSize="20sp" />
 50 
 51     </LinearLayout>
 52 
 53     <LinearLayout
 54         android:layout_width="110dp"
 55         android:layout_height="wrap_content"
 56         android:layout_gravity="center"
 57         android:orientation="vertical" >
 58 
 59         <TextView
 60             android:id="@+id/textView4"
 61             android:layout_width="wrap_content"
 62             android:layout_height="wrap_content"
 63             android:layout_marginTop="30dp"
 64             android:text="输入你的结果"
 65             android:textSize="18dp" />
 66 
 67         <EditText
 68             android:id="@+id/editText1"
 69             android:layout_width="105dp"
 70             android:layout_height="wrap_content"
 71             android:layout_marginTop="20dp"
 72             android:ems="10"
 73             android:inputType="none"
 74             android:text="" />
 75 
 76     </LinearLayout>
 77 
 78     <LinearLayout
 79         android:layout_width="200dp"
 80         android:layout_height="wrap_content"
 81         android:layout_gravity="center"
 82         android:layout_marginTop="25dp"
 83         android:onClick="onClickYes" >
 84 
 85         <Button
 86             android:id="@+id/btnYes"
 87             android:layout_width="100dp"
 88             android:layout_height="wrap_content"
 89             android:onClick="onClickYes"
 90             android:text="确 定" />
 91 
 92         <Button
 93             android:id="@+id/btnClear"
 94             android:layout_width="100dp"
 95             android:layout_height="wrap_content"
 96             android:onClick="ClearClick"
 97             android:text="下一题" />
 98 
 99     </LinearLayout>
100 
101     <TextView
102         android:id="@+id/textView2"
103         android:layout_width="wrap_content"
104         android:layout_height="wrap_content"
105         android:layout_gravity="center"
106         android:layout_marginTop="90dp"
107         android:text="版本号:V 15.04.0001" />
108 
109 </LinearLayout>

       对应的Java文件:

  1 package com.ml.caculate;
  2 
  3 
  4 import java.math.BigDecimal;
  5 import java.util.Stack;
  6 import android.os.Bundle;
  7 import android.app.Activity;
  8 import android.app.AlertDialog;
  9 import android.content.DialogInterface;
 10 import android.content.Intent;
 11 import android.content.DialogInterface.OnClickListener;
 12 import android.view.Menu;
 13 import android.view.MenuItem;
 14 import android.view.View;
 15 import android.widget.EditText;
 16 import android.widget.TextView;
 17 
 18 public class MainActivity extends Activity {
 19     
 20     /* (non-Javadoc)
 21      * @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
 22      */
 23     
 24 
 25     @Override
 26     public boolean onOptionsItemSelected(MenuItem item) {
 27         // TODO Auto-generated method stub
 28         int id=item.getItemId();
 29         
 30         if(id==R.id.action_settings)
 31         {
 32             Intent i = new Intent(MainActivity.this , page1.class);
 33             //启动帮助说明界面
 34             startActivity(i); 
 35             return true;
 36         }
 37         return super.onOptionsItemSelected(item);
 38         
 39     }
 40 
 41     TextView ss,suanshi,zhengque,nandu;
 42     EditText edtResult;
 43     int j=10;
 44     boolean i=true;
 45     int num=0;
 46     String str=Way_1();
 47     int shumu=0;
 48     
 49     int trr=0,err=0;
 50     
 51     
 52     
 53     @Override
 54     protected void onCreate(Bundle savedInstanceState) {
 55         super.onCreate(savedInstanceState);
 56         setContentView(R.layout.activity_main);
 57         ss=(TextView)findViewById(R.id.tvNum);
 58         zhengque=(TextView)findViewById(R.id.textView1);
 59         nandu=(TextView)findViewById(R.id.textView4);
 60         suanshi=(TextView)findViewById(R.id.tvSuanshi);
 61         edtResult=(EditText)findViewById(R.id.editText1);
 62     }
 63 
 64     @Override
 65     public boolean onCreateOptionsMenu(Menu menu) {
 66         // Inflate the menu; this adds items to the action bar if it is present.
 67         getMenuInflater().inflate(R.menu.main, menu);
 68         ss.setText("");
 69         return true;
 70     }
 71 
 72     //产生算式的函数
 73    private static int Operands_2() /*1-9*/
 74     {
 75         int Operand=(int)(Math.random()*9)+1;
 76         return Operand;
 77     }
 78     private static int Operands_3() /*1-99*/
 79     {
 80         int Operand=(int)(Math.random()*99)+1;
 81         return Operand;
 82     }
 83     private static String Operator_1(String formula)/*随机产生运算符   有乘除*/
 84     {
 85         int f=(int)(Math.random()*4);
 86         if(f==0) 
 87         {
 88             formula+="+";
 89         }
 90         if(f==1)
 91         {
 92             formula+="-";
 93         }
 94         if(f==2)
 95         {
 96             formula+="*";
 97         }
 98         if(f==3)
 99         {
100             formula+="/";
101         }
102         return formula;
103     }
104     private static String Way_1() /*最多可支持10个数参与计算   有乘除 有括号 10以内 加减有负数 除法有余数*/
105     {
106         String formula="";
107         int length=(int)(Math.random()*1)+3; /*随机产生表达式中操作数的个数 3-4 个*/
108         int left=0,right=0,flag=0,left_1;
109         for(int i=0;i<length-1;i++)
110         {
111             left_1=left;
112            // left=Bracket_l(left,formula);
113             //---------
114             int f=(int)(Math.random()*3);
115             if(f<2)
116             {
117             }
118             if(f==2)
119             {
120                 formula+="(";
121                 left++;
122             }
123             //******
124             if(left_1!=left)     /*产生了左括号  flag=i*/
125             {
126                 flag=i;
127             }
128             formula+=Operands_2();
129             if(left>right&&flag!=i) /*左括号大于右括号的个数 and 产生左括号和右括号不在一个循环里  即不会产生“(随机数)” 这种无效情况*/
130             {
131                 //right=Bracket_r(right,formula);
132                 int r=(int)(Math.random()*5);
133                 if(r==0)
134                 {
135                 }
136                 if(r>0) /*产生右括号的概率大 */
137                 {
138                     formula+=")";
139                     right++;
140                 }
141             }
142             formula=Operator_1(formula);  /*有乘除*/
143         }
144         /*因为 一个操作数一个运算符   还缺少一个操作数 (0 -- 9)*/
145         formula+=Operands_2();
146         for(int i=0;i<left-right;i++)
147         {
148             formula+=")";
149         }
150         return formula+="#";
151     }
152     private static String Way_2() /*   有乘除 有括号 100以内 加减有负数 除法有余数*/
153     {
154         String formula="";
155         int length=(int)(Math.random()*1)+3; /*随机产生表达式中操作数的个数3-4 个*/
156         int left=0,right=0,flag=0,left_1;
157         for(int i=0;i<length-1;i++)
158         {
159             left_1=left;
160            // left=Bracket_l(left,formula);
161             //---------
162             int f=(int)(Math.random()*3);
163             if(f<2)
164             {
165             }
166             if(f==2)
167             {
168                 formula+="(";
169                 left++;
170             }
171             //******
172             if(left_1!=left)     /*产生了左括号  flag=i*/
173             {
174                 flag=i;
175             }
176             formula+=Operands_3();
177             if(left>right&&flag!=i) /*左括号大于右括号的个数 and 产生左括号和右括号不在一个循环里  即不会产生“(随机数)” 这种无效情况*/
178             {
179                 //right=Bracket_r(right,formula);
180                 int r=(int)(Math.random()*5);
181                 if(r==0)
182                 {
183                 }
184                 if(r>0) /*产生右括号的概率大 */
185                 {
186                     formula+=")";
187                     right++;
188                 }
189             }
190             formula=Operator_1(formula);  /*有乘除*/
191         }
192         /*因为 一个操作数一个运算符   还缺少一个操作数 (0 -- 9)*/
193         formula+=Operands_3();
194         for(int i=0;i<left-right;i++)
195         {
196             formula+=")";
197         }
198         return formula+="#";
199     }
200     //----------------------------------------------
201     static double jisuan(double x,double y,char oper)
202     { 
203         switch(oper)
204         {
205         case '+':
206             {
207                 return x+y;
208             } 
209         case '-':
210             {
211                 return x-y;
212             }
213         case '*':
214             {
215                 return x*y;
216             } 
217         case '/':
218             {
219                 return x/y;
220             }  
221         }
222         return 0;
223     }
224 
225     static char precede(char ch1,char ch2)
226     {
227         int i = 0,j=0;
228         char[][] a={        //优先级对照表,依次为+ - * / ( ) #
229             //  +   -   *   /   (   )   #
230                 {'>','>','<','<','<','>','>'},    // +
231                 {'>','>','<','<','<','>','>'},    // - 
232                 {'>','>','>','>','<','>','>'},    // * 
233                 {'>','>','>','>','<','>','>'},    // /
234                 {'<','<','<','<','<','=','0'},    // (
235                 {'>','>','>','>','0','>','>'},    //
236                 {'<','<','<','<','<','0','='}     // #
237         };
238         switch(ch1)
239         {
240             //优先级与矩阵匹配
241         case '+':
242             {
243                 i=0;
244                 break;
245             }
246         case '-':
247             {
248                 i=1;
249                 break;
250             }
251         case '*':
252             {
253                 i=2;
254                 break;
255             }
256         case '/':
257             {
258                 i=3;
259                 break;
260             }
261         case '(':
262             {
263                 i=4;
264                 break;
265             }
266         case ')':
267             {
268                 i=5;
269                 break;
270             }
271         case '#':
272             {
273                 i=6;
274                 break;
275             }
276         }
277         switch(ch2)
278         {
279             //优先级与矩阵匹配
280         case '+':
281             {
282                 j=0;
283                 break;
284             }
285         case '-':
286             {
287                 j=1;
288                 break;
289             }
290         case '*':
291             {
292                 j=2;
293                 break;
294             }
295         case '/':
296             {
297                 j=3;
298                 break;
299             }
300         case '(':
301             {
302                 j=4;
303                 break;
304             }
305         case ')':
306             {
307                 j=5;
308                 break;
309             }
310         case '#':
311             {
312                 j=6;
313                 break;
314             }
315         }
316         return a[i][j];
317     }
318 
319     static double fun(String str)
320     {
321         double result = 0;
322         Stack<Character>  optr = new Stack<Character> ();  
323         Stack<Double>  opnd = new Stack<Double>();  
324         //SqStack optr,opnd;     //运算符   操作数
325         //InitStack(optr);
326         //push(optr,'#');
327         optr.push('#');
328         //InitStack(opnd);
329         int length=str.length();
330         int i=0;
331         char ch;
332         char s[] = str.toCharArray();
333         ch=s[i];
334         while(ch!='#'||optr.peek()!='#'&&i<length)
335         {
336             if(ch>='0'&&ch<='9')
337             {
338                 char chh=s[++i];
339                 if(chh>='0'&&chh<='9')
340                 {
341                     double c=(ch-48)*10+chh-48;
342                     opnd.push(c);
343                     ch=s[++i];
344                 }
345                 else
346                 {
347                     double c=ch-48;
348                     opnd.push(c);
349                     ch=chh;
350                 }
351                 
352             }
353             else
354                 switch(precede(optr.peek(),ch)){
355                 case '<':
356                     {
357                         //push(optr,ch);
358                         optr.push(ch);
359                         ch=s[++i];
360                         break;
361                     }
362                 case '>':
363                     {
364                         //double y=pop1(opnd);
365                         //double x=pop1(opnd);
366                         double y=opnd.pop();
367                         double x=opnd.pop();
368                         char c=optr.pop();
369                         result=jisuan(x,y,c);
370                         //push1(opnd,result+48);
371                         opnd.push(result);
372                         break;
373                     }
374                 case '=':
375                     {
376                         //pop(optr);
377                         optr.pop();
378                         ch=s[++i];
379                         break;
380                     }
381             }
382         }
383         return result;
384     }
385 
386     public void onClickYes(View v)  //确定按钮
387     {
388         Intent intent = getIntent();
389         String nn=intent.getStringExtra("extra");
390         int number=Integer.parseInt(nn);    //从前一个界面传过来的值
391         if(shumu==number)
392         {
393             new AlertDialog.Builder(this)
394             .setTitle("提示信息")
395             .setMessage("您已经做完"+number+"道题,共答对"+trr+"道题")
396             .setPositiveButton("OK", null)
397             .show(); 
398         }
399         String S1=edtResult.getText().toString();
400         if(ss.getText()=="请"||S1.length()==0)         //判断输入框是否为空
401         {
402             new AlertDialog.Builder(this)
403             .setTitle("提示信息")
404             .setMessage("您还没有输入答案或还没开始")
405             .setPositiveButton("现在输入", null)
406           //.setNegativeButton("是", null)
407             .show(); 
408         }
409         else
410         {
411             String str=ss.getText().toString();
412             double result1 = fun(str);
413             BigDecimal bg = new BigDecimal(result1);
414             double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
415       //      ss.setText(str);
416             double answer=Double.valueOf(edtResult.getText().toString());
417             if (answer!=f1)
418             {
419                 new AlertDialog.Builder(this)
420                 .setTitle("错误提示")
421                 .setMessage("很遗憾 ,回答错误!")
422                 .setNegativeButton("下一题",new OnClickListener() {
423                     
424                     @Override
425                     public void onClick(DialogInterface arg0, int arg1) {
426                         // TODO Auto-generated method stub
427                         Intent intent = getIntent();
428                         String rr=intent.getStringExtra("redio");
429                         int red=Integer.parseInt(rr);
430                         if(red==0)
431                         {
432                             String str=Way_1();
433                             ss.setText(str);
434                             zhengque.setText("小学生计算题");
435                             suanshi.setText(" ?");
436                         }
437                         else
438                         {
439                             String str=Way_1();
440                             ss.setText(str);
441                             zhengque.setText("小学生计算题");
442                             suanshi.setText(" ?");
443                         }
444                     }
445                 })
446                 .show(); 
447                 shumu++;
448             }
449             else if(answer==f1)
450             {
451                 suanshi.setText(" ?");
452                 zhengque.setText("恭喜回答正确!!!");
453                 ss.setText("请点击下一道题,继续...");
454                 trr++;
455             }
456             edtResult.setText("");
457         }
458     }   
459     
460     public void ClearClick(View v)  //下一题按钮
461 
462     {
463         Intent intent = getIntent();
464         String nn=intent.getStringExtra("extra");
465         String rr=intent.getStringExtra("redio");
466         int number=Integer.parseInt(nn);    //从前一个界面传过来的值
467         int red=Integer.parseInt(rr);
468         if(shumu<number)
469         {
470             edtResult.setText("");
471             if(red==0)
472             {
473                 String str=Way_1();
474                 ss.setText(str);
475                 zhengque.setText("小学生计算题");
476                 suanshi.setText(" ?");
477                 shumu++;
478             }
479             else
480             {
481                 String str=Way_2();
482                 ss.setText(str);
483                 zhengque.setText("小学生计算题");
484                 suanshi.setText(" ?");
485                 shumu++;
486             }
487         }
488         else
489         {
490             new AlertDialog.Builder(this)
491             .setTitle("提示信息")
492             .setMessage("您已经做完"+number+"道题,共答对"+trr+"道题")
493             .setPositiveButton("OK", null)
494             .show(); 
495             ss.setText("");
496             String s2=trr+"/"+number+"#";
497             double s=fun(s2);
498             BigDecimal bg = new BigDecimal(s);
499             double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
500             suanshi.setText("正确率:"+f1*100+"%");
501         }
502     }
503 }
504 
505 
506 
507     

      帮助说明布局文件:

  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 
  7     <TextView
  8         android:id="@+id/textView1"
  9         android:layout_width="wrap_content"
 10         android:layout_height="wrap_content"
 11         android:layout_gravity="center"
 12         android:layout_marginTop="40dp"
 13         android:text="帮助说明"
 14         android:textSize="24dp" />
 15 
 16     <LinearLayout
 17         android:layout_width="match_parent"
 18         android:layout_height="wrap_content"
 19         android:layout_marginTop="35dp"
 20         android:orientation="vertical" >
 21 
 22         <TextView
 23             android:id="@+id/textView2"
 24             android:layout_width="238dp"
 25             android:layout_height="wrap_content"
 26             android:layout_gravity="center"
 27             android:layout_marginTop="16dp"
 28             android:layout_weight="0.59"
 29             android:text="1.您可以选择题目数量。"
 30             android:textSize="20dp" />
 31 
 32         <TextView
 33             android:id="@+id/textView3"
 34             android:layout_width="238dp"
 35             android:layout_height="wrap_content"
 36             android:layout_gravity="center"
 37             android:layout_marginTop="16dp"
 38             android:layout_weight="0.59"
 39             android:text="2.您可以选择有无乘除。"
 40             android:textSize="20dp" />
 41 
 42          <TextView
 43             android:id="@+id/textView4"
 44             android:layout_width="238dp"
 45             android:layout_height="wrap_content"
 46             android:layout_gravity="center"
 47             android:layout_marginTop="16dp"
 48             android:layout_weight="0.59"
 49             android:text="3.结果是小数的,保留两位小数。"
 50             android:textSize="20dp" />
 51          
 52          
 53           <TextView
 54             android:id="@+id/textView5"
 55             android:layout_width="238dp"
 56             android:layout_height="wrap_content"
 57             android:layout_gravity="center"
 58             android:layout_marginTop="16dp"
 59             android:layout_weight="0.59"
 60             android:text="4.结果是整数的,保留一位小数。"
 61             android:textSize="20dp" />
 62           
 63                     <TextView
 64             android:id="@+id/textView6"
 65             android:layout_width="238dp"
 66             android:layout_height="wrap_content"
 67             android:layout_gravity="center"
 68             android:layout_marginTop="16dp"
 69             android:layout_weight="0.59"
 70             android:text="5.预祝你取得好成绩!"
 71             android:textSize="20dp" />
 72     </LinearLayout>
 73 
 74     <LinearLayout
 75         android:layout_width="match_parent"
 76         android:layout_height="wrap_content"
 77         android:layout_marginTop="60dp"
 78         android:gravity="center_horizontal" >
 79 
 80         <TextView
 81             android:id="@+id/textView7"
 82             android:layout_width="wrap_content"
 83             android:layout_height="wrap_content"
 84             android:layout_gravity="center"
 85             android:text="版本号:" />
 86 
 87         <TextView
 88             android:id="@+id/textView8"
 89             android:layout_width="wrap_content"
 90             android:layout_height="wrap_content"
 91             android:text="V 15.04.0001" />
 92     </LinearLayout>
 93 
 94     <LinearLayout
 95         android:layout_width="match_parent"
 96         android:layout_height="wrap_content"
 97         android:gravity="center_horizontal">
 98 
 99         <TextView
100             android:id="@+id/textView9"
101             android:layout_width="wrap_content"
102             android:layout_height="wrap_content"
103             android:layout_marginTop="10dp"
104             android:text="关注博客园:_小学生and小青年" />
105 
106     </LinearLayout>
107 
108 </LinearLayout>

      对应的Java文件:

 1 package com.ml.caculate;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.ContextMenu;
 7 import android.view.ContextMenu.ContextMenuInfo;
 8 import android.view.View;
 9 import android.widget.RadioButton;
10 import android.widget.RadioGroup;
11 import android.widget.TextView;
12 
13 public class page1 extends Activity {
14     
15      public RadioGroup rg;
16      public RadioButton mRadio1, mRadio2,mRadio3;
17      String []info=new String [3];
18      TextView tv1;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         // TODO Auto-generated method stub
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.help);
25         tv1=(TextView)findViewById(R.id.textView1);
26     }
27 
28     @Override
29     public void onCreateContextMenu(ContextMenu menu, View v,
30             ContextMenuInfo menuInfo) {
31         // TODO Auto-generated method stub
32         super.onCreateContextMenu(menu, v, menuInfo);
33     }
34     
35     public void onClickTijiao1(View v)
36     {
37         Intent i = new Intent("android.intent.action.PAGE2");
38         i.putExtra(info[0], info[0]); 
39         //启动
40         startActivity(i); 
41     }
42 }

        运行效果:

 

 

        这次试验有很多细节的地方,比如判空语句,比如判断对错的对话框,光判空语句就占了将近1/4的代码量,做完了这个小软件,有一点小小的成就感。

项目计划总结:      
日期 听课 编写程序 查阅资料 日总计
星期一  2 2 2 6
星期二        
星期三   3 1 4
星期四  2 2   4
星期五   2   2
星期六   3 1 4
星期日   3 1 4
周总计   17 7 24

       

时间记录日志:          
日期 开始时间 结束时间 中断时间 净时间 活动 备注
4月1日 14:00 16:10 10 120 编写编程 修改程序
4月2日 8:20 10:40 30 110 编写程序 将程序改为Java语言
4月3日 16:30 20:30 30 210 查阅资料 查资料和调试
4月4日 15:00 21:40 100 3000 编程 修改和调试
4月5日 10:00 17:30 90 3600 调试 最终调试
4月6日 18:00 18:30   30 博客 撰写博客

       

缺陷记录日志:
日期 编号 引入阶段 排除阶段 调试过程中遇到的BUG以及解决办法
4月1日 1 编码 编译 对C++的四则运算进行了修改,变为Java语言
4月2日 2 编码 编译 在安卓里边,每次都在判断下一个结果,修改执行顺序。
4月3日 3 编码 编译 判断时,为了不出现循环小数,我们保留两位小数。
4月4日 4 编码 编译 问题带“#”输出,让其在计算的时候加“#”,输出时没有。
4月4日 5 编码 编译 加入了一个新界面,用来获取用户输入的题目数量和难度。
4月6日 6 编码 编译 页面之间传值过不去,仔细看了一下怎样用Intent传参,问题解决。

        小伙伴博客地址:http://www.cnblogs.com/L-Damon-v/

        点击此链接下载App易百分

原文地址:https://www.cnblogs.com/cnyulei/p/5360840.html