四则运算,安卓版

题目:四则运算安卓版

设计思想:安卓版四则运算相对来说思路较为简单,分为三部分:简单,一般,复杂。简单里面主要是加减法,一般里面是乘除法,复杂里面是涉及四则运算的,有加减乘除。分为四个界面(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);
//    }
}

 

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);
    }
}
<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>
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);
    }
}
<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>

 

 

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();
                }
            });
        }
}
<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>

 

试验截图:

缺陷日志:本次试验虽然实现了主要功能但是还是存在很大的缺陷,因为在运算除法的时候小数点的位数无法控制。界面也不是很友好,存在很大的问题,还需要改进。

实验总结:

posted @ 2016-04-06 17:54  apan008  阅读(201)  评论(2编辑  收藏  举报