题目:四则运算安卓版
设计思想:安卓版四则运算相对来说思路较为简单,分为三部分:简单,一般,复杂。简单里面主要是加减法,一般里面是乘除法,复杂里面是涉及四则运算的,有加减乘除。分为四个界面(1)首页,(2)简单页面,(3)一般难度页面,(4)复杂页面。在首页可以选择自己相应的难度,从而调用事件跳转到对应的界面,自动出题,填出结果,然后提交判断是否正确。如果想继续做,可以点击下一题。
代码:
package com.jingpan.calculating; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; public class MainActivity extends Activity { private Button b1; private Button b2; private Button b3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); b1=(Button)findViewById(R.id.button1); b2=(Button)findViewById(R.id.button2); b3=(Button)findViewById(R.id.button3); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,SimpleActivity.class); startActivity(intent); finish(); } }); b2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,CommonActivity.class); startActivity(intent); finish(); } }); b3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,ComplexActivity.class); startActivity(intent); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } //@Override // public boolean onOptionsItemSelected(MenuItem item) { // // Handle action bar item clicks here. The action bar will // // automatically handle clicks on the Home/Up button, so long // // as you specify a parent activity in AndroidManifest.xml. // int id = item.getItemId(); // if (id == R.id.action_settings) { // return true; // } // return super.onOptionsItemSelected(item); // } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 package com.jingpan.calculating; import java.util.Random; import android.app.Activity; import android.content.Intent; import android.graphics.Interpolator; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class SimpleActivity extends Activity { private TextView t; private EditText e; private Button bn; private EditText et; private int r; private Button bnext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simplemain); t=(TextView)findViewById(R.id.textView2); e=(EditText)findViewById(R.id.editText1); bn=(Button)findViewById(R.id.button1); et=(EditText)findViewById(R.id.editText2); bnext=(Button)findViewById(R.id.button2); int a,b; /*int n; n=Integer.valueOf(e.getText().toString());*/ a=(int) (1+Math.random()*10); b=(int) (1+Math.random()*10); int c; char ch; c=(int)(1+Math.random()*10); if(c/2==0) { ch='+'; r=a+b; } else{ ch='-'; r=a-b; } t.setText(""+a+" "+ch+" "+b+" "+"="); bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub int result; result=Integer.parseInt(et.getText().toString()); String t=""; if(result==r) { t="答对了!!!"; } else { t="答错了!!!正确答案是"+r; } Toast.makeText(SimpleActivity.this, t, Toast.LENGTH_LONG).show(); } }); bnext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(SimpleActivity.this,SimpleActivity.class); startActivity(intent); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jingpan.calculating.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="95dp" android:layout_marginTop="22dp" android:text="简单" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/editText1" android:layout_marginTop="41dp" android:text="计算式:" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView4" android:layout_alignBottom="@+id/textView4" android:layout_alignLeft="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:layout_marginTop="38dp" android:text="结果:" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView5" android:layout_alignBottom="@+id/textView5" android:layout_toRightOf="@+id/textView5" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="51dp" android:text="确认" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="19dp" android:layout_toRightOf="@+id/textView2" android:text="下一题" /> </RelativeLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 package com.jingpan.calculating; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class CommonActivity extends Activity { private TextView t; private EditText e; private Button bn; private EditText et; private double r; private Button bnext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.commonmain); t=(TextView)findViewById(R.id.textView2); e=(EditText)findViewById(R.id.editText1); bn=(Button)findViewById(R.id.button1); et=(EditText)findViewById(R.id.editText2); bnext=(Button)findViewById(R.id.button2); float a,b; /*int n; n=Integer.valueOf(e.getText().toString());*/ a=(int) (1+Math.random()*10); b=(int) (1+Math.random()*10); int c; char ch; c=(int)(1+Math.random()*10); if(c/2==0) { ch='*'; r=(float)(a*b); } else{ ch='/'; r=(float)(a/b); } t.setText(""+a+" "+ch+" "+b+" "+"="); bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub float result; result=Float.parseFloat(et.getText().toString()); String t=""; if(result==r) { t="答对了!!!"; } else { t="答错了!!!正确答案是"+r; } Toast.makeText(CommonActivity.this, t, Toast.LENGTH_LONG).show(); } }); bnext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(CommonActivity.this,CommonActivity.class); startActivity(intent); finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jingpan.calculating.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="95dp" android:layout_marginTop="22dp" android:text="一般" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_toRightOf="@+id/textView3" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/editText1" android:layout_marginTop="41dp" android:text="计算式:" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView4" android:layout_alignBottom="@+id/textView4" android:layout_alignLeft="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:layout_marginTop="38dp" android:text="结果:" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView5" android:layout_alignBottom="@+id/textView5" android:layout_toRightOf="@+id/textView5" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="51dp" android:text="确认" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="19dp" android:layout_toRightOf="@+id/textView2" android:text="下一题" /> </RelativeLayout> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 package com.jingpan.calculating; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class ComplexActivity extends Activity{ private TextView t; private EditText e; private Button bn; private EditText et; private double r; private Button bnext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.complexmain); t=(TextView)findViewById(R.id.textView2); e=(EditText)findViewById(R.id.editText1); bn=(Button)findViewById(R.id.button1); et=(EditText)findViewById(R.id.editText2); bnext=(Button)findViewById(R.id.button2); float a,b,d,e; /*int n; n=Integer.valueOf(e.getText().toString());*/ a=(int) (1+Math.random()*10); b=(int) (1+Math.random()*10); d=(int) (1+Math.random()*10); int c; char ch = 0; char ch1 = 0; c=(int)(1+Math.random()*10); e=(int)(1+Math.random()*10); if(c/4==0) { ch='+'; if(e/4==0) { ch1='+'; r=(float)(a+b+d); } else if(e/4==1) { ch1='-'; r=(float)(a-b-d); } else if(e/4==2) { ch1='*'; r=(float)(a*b*d); } else if(e/4==3) { ch1='*'; r=(float)(a*b*d); } t.setText(""+a+" "+ch+" "+b+" "+ch1+" "+d+" "+"="); } if(c/4==1) { ch='-'; if(e/4==0) { ch1='+'; r=(float)(a-b+d); } else if(e/4==1) { ch1='-'; r=(float)(a-b-d); } else if(e/4==2) { ch1='*'; r=(float)(a-b*d); } else if(e/4==3) { ch1='/'; r=(float)(a-b/d); } t.setText(""+a+" "+ch+" "+b+" "+ch1+" "+d+" "+"="); } if(c/4==2) { ch='*'; if(e/4==0) { ch1='+'; r=(float)(a*b+d); } else if(e/4==1) { ch1='-'; r=(float)(a*b-d); } else if(e/4==2) { ch1='*'; r=(float)(a*b*d); } else if(e/4==3) { ch1='/'; r=(float)(a*b/d); } t.setText(""+a+" "+ch+" "+b+" "+ch1+" "+d+" "+"="); } bn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub float result; result=Float.parseFloat(et.getText().toString()); String t=""; if(result==r) { t="答对了!!!"; } else { t="答错了!!!正确答案是"+r; } Toast.makeText(ComplexActivity.this, t, Toast.LENGTH_LONG).show(); } }); bnext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(ComplexActivity.this,ComplexActivity.class); startActivity(intent); finish(); } }); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.jingpan.calculating.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="95dp" android:layout_marginTop="22dp" android:text="复杂" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/editText1" android:layout_marginTop="41dp" android:text="计算式:" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView4" android:layout_alignBottom="@+id/textView4" android:layout_alignLeft="@+id/textView1" android:text="TextView" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView4" android:layout_below="@+id/textView4" android:layout_marginTop="38dp" android:text="结果:" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView5" android:layout_alignBottom="@+id/textView5" android:layout_toRightOf="@+id/textView5" android:ems="10" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="51dp" android:text="确认" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_marginLeft="19dp" android:layout_toRightOf="@+id/textView2" android:text="下一题" /> </RelativeLayout>
测试结果:
实验总结:头一次进行安卓编程,感觉收获很大。
开发日志:
星期 | 听课 | 上网查资料 | 编程 | 读书 | 总计 |
周二 | 1 | 1 | 2 | ||
周三 | 0 | ||||
周四 | 2 | 2 | |||
周五 | 1 | 3 | |||
周一 | 1 | 1 | 1 | 3 | |
周二 | 2 | 1 | 3 | ||
周三 | 1 | 2 | 1 | 4 | |
总计 | 2 | 5 | 5 | 3 | 15 |
时间记录日志:
日期 | 开始时间 | 结束时间 | 休息时间 | 工作时间 | 活动内容 |
4/1 | 19:30 | 20:30 | 0 | 60 | 结对开发 |
4/4 | 16:30 | 17:30 | 0 | 60 | 结对开发 |
4/5 | 19:00 | 20:00 | 0 | 60 | 结对开发 |
4/6 | 13:50 | 15:30 | 20 | 100 | 结对开发 |
缺陷日志:
在运算除法的时候小数点的位数无法控制。界面也不是很友好,存在很大的问题,还需要改进,没有范围,数量控制,以后将完善。
队友博客:http://home.cnblogs.com/u/apan008/