软件工程-构建之法 小学生四则运算的出题程序,android版本
一、前言
上次软件工程第一次作业,自己用的是java写的,那时候想过用android写,怕自己写不好,所以自己等把作业写完后,自己用了周末的两天从新从Hello world开始学习android。
记得参加ACM中,自己那时候啥也不会,自己总会问别人,怎们学习ACM,怎么提高自己的编程能力,那时候记得刘昆老师说,学习那就从Hello world开始,每当自己觉得自己不行的时候,自己遇到编程时的BUG时,自己总会回忆那时候老师说的话,不会就从Hello world开始。
回到Hello world开始,回到编程的原点,自己一直在努力学习编程,自己喜欢编程,回到编程的原点,以后工作中遇到编程的瓶颈时,我觉得我也应该从Hello world开始。
二、背景
阿超有个儿子上小学二年级,老师每天让家长给孩子出30道加减法题,虽然不多,但是每天都做也算是个负担,阿超作为一个老牌程序员当然想用计算机来解决这个小问题,目前对于这个问题对于任何语言都不是问题,比如:
C/C++、C#、Java、Python、VB、JavaScript、Perl……
具体要求如下:
- 能自动生成小学四则运算题目(注意是给小学生用的,要是结果出现负数的话他们会迷茫的!)
- 除了整数外,还要支持真分数的四则运算
请大家用任何一种自己擅长的语言来编写这段程序,并把程序的介绍和自己编写的过程写一个博客
三、设计分析
(1)生成随机数的函数 Math.random()
生成在一定范围(1--100)的随机数方法,Math.random()*100+1;
int j=1; int p=100; a = String.valueOf((int)(Math.random() *p+j)) ; //随机数a,b; b = String.valueOf((int)(Math.random() *p+j)) ;
(2)生成随机运算符的函数(自定义的随机函数)自己定义一个字符串数组,然后随机生成数组的脚标的数字,显示的时候调用字符串数组。
String[] op=new String[]{"+","-","*","/"}; Random r = new Random(); int d = r.nextInt(4);
调用显示过程。symbol.setText(op[d]);
(3)比较过程,不是==;而是使用equals()函数比较
if(symbol.getText().toString().equals("/")==true)
(4)在每一个activity中需要在Androidmainfest中注册每一个activity,这是我们容易忘记的。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="safeguard.pupilcalculate" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Appstart" //所以首先启动的是Appstar.java这个Activity。 android:label="@string/app_name" > <intent-filter> //有intent-filter的地方是APP运行的时候第一个启动activity。 <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity"/>//这两个是注册语句 <activity android:name=".Welcome"/>//这两个是注册语句 </application> </manifest>
(5)activity之间的调用使用的是Intent intent = new Intent (Appstart.this,MainActivity.class);
同时要记住是intent是activity之间传递的其中一种方法。
public void run(){ Intent intent = new Intent (Appstart.this,MainActivity.class); //这就是在Appstart这个activity跳转到 MainActivity startActivity(intent); //这个是通过intent传递 Appstart.this.finish(); //启动Mainactivity后,关闭Appstart这个activity。 }
(6)事件监听器的设置,就是说有多少个按钮就会设置多少个按钮点击事件的监听器。
监听的意思就是:当你点击这个按钮后,会执行哪些事件,
例如:点击开始按钮点击事件监听器,
1、会判断时候输入的题目数量时候为空或者是为0;
2、不为空时,清空TextView内容
3、产生随机数和随机运算符
4、将产生的随机数和运算符在TextView中显示
//开始按钮的点击事件监听器 start.setOnClickListener ( new OnClickListener() { public void onClick(View v) { Toast toast; //int NInt=Integer.parseInt(N.getText().toString()); if(N.getText().toString().equals("")==true||NInt==0){ toast = Toast.makeText(getApplicationContext(), "请输入题目数量!", Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.zemoji_e6); toastView.addView(imageCodeProject, 0); toast.show(); } else{ String a=""; String b=""; // TextView tv=new TextView(this); int k=0; int p=50; a = String.valueOf((int)(Math.random() *p+k)) ; //随机数a,b; b = String.valueOf((int)(Math.random() *p+k)) ; /*int factorOneInt=Integer.parseInt(factorOneStr) ; int factorTwoInt=Integer.parseInt(factorTwoStr) ; */ String[] op=new String[]{"+","-","*","/"}; Random r = new Random(); int d = r.nextInt(4); factorOne.setText(a); factorTwo.setText(b); symbol.setText(op[d]); num.setText(String.valueOf(j)); } } } );
同时要记住按钮监听。如果没有设置这个,会在程序运行中意外退出。
//按钮监听 public void buttonListener(Button button,final int id) { button.setOnClickListener ( new OnClickListener() { @Override public void onClick(View v) { // } } ); } }
四、优点和不足
(1)优点
1、”下一题“的前提是:你把你的答案填入到框中
2、“检查答案”的前提是:你把你的答案填入到框中,
3、“检查答案”会告诉你答案是多少。
(2)缺点
1、除运算只有在存在得到整数情况下才能判断。
2、分数运算不支持,有待完善
五、运行截图
六、APK运行视频http://pan.baidu.com/s/1mht0DRU
APK下载地址:http://pan.baidu.com/s/1o7nqNJc
七、源码分享
(1)Appstart.java
package safeguard.pupilcalculate;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class Appstart extends Activity {
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.appstart);
new Handler().postDelayed(new Runnable(){
@Override
public void run(){
Intent intent = new Intent (Appstart.this,MainActivity.class);
startActivity(intent);
Appstart.this.finish();
}
}, 2000);
}
}
(2)MainActivity.java
package safeguard.pupilcalculate;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
//声明一些控件
private TextView textView2;//小学生四则运算
private TextView textView1;//请输入你要出的题
private TextView factorOne;//第一个数
private TextView symbol;//符号
private TextView factorTwo;//第二个数
private TextView num;
private Button start;//开始
private Button next;//下一题
private Button check;//检查是否正确
private EditText i;
private EditText N;
private int value1;
private int value2;
private int result; //实际计算结果
private int j=1;
private int NInt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//跳转到main界面
initButton();
//开始按钮的点击事件监听器
start.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
Toast toast;
// int NInt=Integer.parseInt(N.getText().toString());
if(N.getText().toString().equals("")==true||Integer.parseInt(N.getText().toString())==0){
toast = Toast.makeText(getApplicationContext(), "请输入题目数量!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e6);
toastView.addView(imageCodeProject, 0);
toast.show();
}
else{
String a="";
String b="";
// TextView tv=new TextView(this);
int k=0;
int p=50;
a = String.valueOf((int)(Math.random() *p+k)) ; //随机数a,b;
b = String.valueOf((int)(Math.random() *p+k)) ;
/*int factorOneInt=Integer.parseInt(factorOneStr) ;
int factorTwoInt=Integer.parseInt(factorTwoStr) ;
*/
String[] op=new String[]{"+","-","*","/"};
Random r = new Random();
int d = r.nextInt(4);
factorOne.setText(a);
factorTwo.setText(b);
symbol.setText(op[d]);
num.setText(String.valueOf(j));
}
}
}
);
//检查按钮的点击事件的监听
check.setOnClickListener
(
new OnClickListener()
{
@Override
public void onClick(View v)
{
Toast toast;
if(i.getEditableText().toString().equals("")==true)
{
toast = Toast.makeText(getApplicationContext(), "请输入答案!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e6);
toastView.addView(imageCodeProject, 0);
toast.show();
}
else
{
value1 =Integer.parseInt(factorOne.getText().toString()) ;
value2 = Integer.parseInt(factorTwo.getText().toString());
if(symbol.getText().toString().equals("+")==true){
result=value1+value2;
int number1 = Integer.parseInt(i.getText().toString());
if(result==number1){
toast = Toast.makeText(getApplicationContext(),"恭喜你答对了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e1);
toastView.addView(imageCodeProject, 0);
toast.show();}
else
{
toast = Toast.makeText(getApplicationContext(), "抱歉,你答错了!"+"\n"+"答案是"+result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e25);
toastView.addView(imageCodeProject, 0);
toast.show();}
}
if(symbol.getText().toString().equals("-")==true){
result=value1-value2;
int number1 = Integer.parseInt(i.getText().toString());
if(result<0){
toast = Toast.makeText(getApplicationContext(), "答案是负数!"+"请按下一题", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e6);
toastView.addView(imageCodeProject, 0);
toast.show(); }
else{
if(result==number1){
toast = Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e1);
toastView.addView(imageCodeProject, 0);
toast.show();}
else
{
toast = Toast.makeText(getApplicationContext(),"抱歉,你答错了!"+"\n"+"答案是"+result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e25);
toastView.addView(imageCodeProject, 0);
toast.show();}
}
}
if(symbol.getText().toString().equals("*")==true){
result=value1*value2;
int number1 = Integer.parseInt(i.getText().toString());
if(result==number1){
toast = Toast.makeText(getApplicationContext(), "恭喜你答对了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e1);
toastView.addView(imageCodeProject, 0);
toast.show();}
else
{
toast = Toast.makeText(getApplicationContext(), "抱歉,你答错了!"+"\n"+"答案是"+result, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e25);
toastView.addView(imageCodeProject, 0);
toast.show();}
}
if(symbol.getText().toString().equals("/")==true){
if(value2==0){
toast = Toast.makeText(getApplicationContext(), "除数是零!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e6);
toastView.addView(imageCodeProject, 0);
toast.show(); }
else{
result=value1/value2;
int number1 = Integer.parseInt(i.getText().toString());
if(result==number1){
toast = Toast.makeText(getApplicationContext(),"恭喜你答对了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e1);
toastView.addView(imageCodeProject, 0);
toast.show();}
else
{
toast = Toast.makeText(getApplicationContext(),"抱歉,你答错了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e25);
toastView.addView(imageCodeProject, 0);
toast.show();
}
}
}
}
}
}
);
//下一题点击事件监听器
next.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v)
{
Toast toast;
if(i.getEditableText().toString().equals("")==true)
{
toast = Toast.makeText(getApplicationContext(), "请输入答案!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e6);
toastView.addView(imageCodeProject, 0);
toast.show();
}
else
{
if(j>=Integer.parseInt(N.getText().toString()))
{
toast = Toast.makeText(getApplicationContext(), "恭喜你,你的题目已经做完了!", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.zemoji_e1);
toastView.addView(imageCodeProject, 0);
toast.show();
}
else
{
factorOne.setText("");
factorTwo.setText("");
symbol.setText("");
i.setText("");
num.setText("");
String a="";
String b="";
// TextView tv=new TextView(this);
int k=0;
int p=50;
a = String.valueOf((int)(Math.random() *p+k)) ; //随机数a,b;
b = String.valueOf((int)(Math.random() *p+k)) ;
/*int factorOneInt=Integer.parseInt(factorOneStr) ;
int factorTwoInt=Integer.parseInt(factorTwoStr) ;
*/
j++;
String[] op=new String[]{"+","-","*","/"};
Random r = new Random();
int d = r.nextInt(4);
factorOne.setText(a);
factorTwo.setText(b);
symbol.setText(op[d]);
num.setText(String.valueOf(j));
}
}
}
}
);
}
//初始化控件
public void initButton()
{ //初始化控件资源
//从布局中获取控件
textView2=(TextView)findViewById(R.id.textView2);
textView1=(TextView)findViewById(R.id.textView1);
factorOne=(TextView)findViewById(R.id.factorOne);
symbol=(TextView)findViewById(R.id.symbol);
factorTwo=(TextView)findViewById(R.id.factorTwo);
num=(TextView)findViewById(R.id.num);
start=(Button)findViewById(R.id.start);
next=(Button)findViewById(R.id.next);
check=(Button)findViewById(R.id.check);
i=(EditText)findViewById(R.id.i);
N=(EditText)findViewById(R.id.N);
}
//按钮监听
public void buttonListener(Button button,final int id)
{
button.setOnClickListener
(
new OnClickListener()
{
@Override
public void onClick(View v)
{
factorOne.setText("");
factorTwo.setText("");
symbol.setText("");
i.setText("");
num.setText("");
}
}
);
}
}
(3)appstart.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/welcome" >
</LinearLayout>
(4)activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:layout_marginLeft="5dip"
android:gravity="center_vertical|right"
android:layout_gravity="center_horizontal"
android:text="小学生四则运算练习"
android:textSize="30dip"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="top|center_horizontal"
android:orientation="horizontal"
android:padding="1dp"
android:paddingTop="1dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingTop="1dp"
android:text="请输入你要出的题目数目:"
android:textSize="20dip"
android:textStyle="bold" />
<EditText
android:id="@+id/N"
android:layout_width="wrap_content"
android:layout_height="33dp"
android:layout_marginTop="15dp"
android:ems="10"
android:inputType="numberSigned"
android:textSize="15dip" >
<requestFocus />
</EditText>
</LinearLayout>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始训练"
android:textSize="15sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/factorOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Text"
android:textSize="20sp" />
<TextView
android:id="@+id/symbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/factorOne"
android:layout_alignBottom="@+id/factorOne"
android:layout_toRightOf="@+id/factorOne"
android:text="Text"
android:textSize="20sp" />
<TextView
android:id="@+id/factorTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/symbol"
android:layout_alignBottom="@+id/symbol"
android:layout_centerHorizontal="true"
android:text="Text"
android:textSize="20sp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/factorTwo"
android:layout_toRightOf="@+id/textView1"
android:text="="
android:textSize="20sp" />
<EditText
android:id="@+id/i"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberSigned"
android:textSize="25sp" />
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第"
android:textSize="20dip" />
<TextView
android:id="@+id/num"
android:layout_width="26dp"
android:layout_height="wrap_content"
android:textSize="20dip" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="题"
android:textSize="20dip" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal" >
<Button
android:id="@+id/check"
android:layout_width="124dp"
android:layout_height="wrap_content"
android:layout_weight="1.15"
android:text="检查是否正确"
android:textSize="15sp" />
<Button
android:id="@+id/next"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:text="下一题"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
(5)about.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
文中有错误的希望大家能指出来。大家相互学习,共同进步。