四则运算(android版)

Posted 就是潘金莲的野野鬼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了四则运算(android版)相关的知识,希望对你有一定的参考价值。

本次安卓开发,我用了四个activity:

代码 MainActivity:

package com.example.demon;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    private Button b1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b1 = (Button) findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(MainActivity.this,MainActivity1.class);
                startActivity(intent);
            }
            
            
        });
    }


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

MainActivity1:

package com.example.demon;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity1 extends Activity{
    private Button b2;
    private Button b3;
    private EditText ed1,ed2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xuanze);
        ed1 = (EditText) findViewById(R.id.editText1); 
        ed2 = (EditText) findViewById(R.id.editText2); 

        b2 = (Button) findViewById(R.id.button4);
        b3 = (Button) findViewById(R.id.button3);
        b2.setOnClickListener(new  ButtonListener());
        b3.setOnClickListener(new  ButtonListener1());
    }
    class ButtonListener implements OnClickListener
    {
            @Override
            public void onClick(View v){
                Intent intent = new Intent();
                intent.setClass(MainActivity1.this,MainActivity.class);
                MainActivity1.this.startActivity(intent);
            }
    }

            class ButtonListener1 implements OnClickListener
            {
                    @Override
                    public void onClick(View d){
                        Intent intent1 = new Intent();
                        String str1 = ed1.getText().toString(); 
                        String str2 = ed2.getText().toString(); 
                        if ((!"".equals(str1)) && (!"".equals(str2))) { 
                
                        // Intent的方法,现在把它理解成是一个Map就好了,Map.put(key,value); 
                        intent1.putExtra("num1", str1); 
                        intent1.putExtra("num2", str2); 


                        intent1.setClass(MainActivity1.this,MainActivity2.class);
                        MainActivity1.this.startActivity(intent1);
                    
                    }
            }
            
        
    }
}

        
            
            
       
            
    
        
    

MainActivity2:

package com.example.demon;



import java.math.BigDecimal;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity2 extends Activity {
    private Button b1;
    private Button b2;
    private Button b3;
    private Button b4;
    int z;
    
    
    private TextView t;
    private TextView txtview1;
    private EditText edtview1;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main1);
        Intent fromPlusIntent = getIntent(); 
        String num1Str = fromPlusIntent.getStringExtra("num1"); 
        String num2Str = fromPlusIntent.getStringExtra("num2"); 
        int num1 = Integer.parseInt(num2Str);
        z = num1;
        final int num2 = Integer.parseInt(num1Str); 
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        b3 = (Button) findViewById(R.id.button3);
        b4 = (Button) findViewById(R.id.button4);
        txtview1 = (TextView) findViewById(R.id.tv_2);
        edtview1 = (EditText) findViewById(R.id.editText1);
        // String str1 = edtview1.getText().toString();
        // int num1 = Integer.parseInt(str1);
        
        
        
        
        b4.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                
                createf(num2);
                t = (TextView) findViewById(R.id.tv_1);
                    t.setText(sty);
                
                
            }
        });
        b3.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v) {
                createf(num2);
                t = (TextView) findViewById(R.id.tv_1);
                    t.setText(sty);
                    
            }
        });
        t = (TextView) findViewById(R.id.tv_1);
        
        t.setText(sty);
        
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity2.this,
                        MainActivity1.class);
                startActivity(intent);
            }

        });
        // if (num1 == result) {
        b2.setOnClickListener(new OnClickListener() {
        

            @Override
            public void onClick(View v) {
                z = z-1;
                // 点击按钮后设置文字标签
                String ansstring = edtview1.getText().toString();
                // double num=Double.valueOf(ansstring);
                double num = Integer.valueOf(ansstring);
                String t = "";                
                
                int b = 0;
                if (num == f1) {
                    t = "答对了,真棒";
                    b=b+1;
                } else {
                    t = "答错了";
                }
                Toast.makeText(MainActivity2.this, t, Toast.LENGTH_LONG)
                        .show();
                if(z==0)
                {
                    
                    Intent intent = new Intent(MainActivity2.this,
                            MainActivity3.class);
//                    intent.putExtra("num5", b); 
                    startActivity(intent);
                }

            }
                

        });
        
        // }
        // if (num1 != result) {
        // b2.setOnClickListener(new OnClickListener() {
        // @Override
        // public void onClick(View v) {
        // // 点击按钮后设置文字标签
        //
        // txtview1.setText("答错了");
        // // 点击按钮后弹出提示框,里面的参数是(绑定活动,提示内容,显示时间)
        // Toast.makeText(MainActivity2.this, "答错了", Toast.LENGTH_LONG)
        // .show();
        //
        // }
        //
        // });
        // }
    }

    //
    // protected void onCreate1(Bundle savedInstanceState) {
    // super.onCreate(savedInstanceState);
    // setContentView(R.layout.activity_main1);
    // b2 = (Button) findViewById(R.id.button2);
    // createf();
    // t=(TextView)findViewById(R.id.tv_1);
    // if(sty!=null) t.setText(sty);
    // // b2.setOnClickListener(new OnClickListener() {
    // // @Override
    // // public void onClick(View v) {
    // // Intent intent = new Intent(MainActivity2.this,
    // // MainActivity1.class);
    // // startActivity(intent);
    // // }
    // // });
    // }

    String sty="zzzzz";
    double result = 0;
    double f1;
    String k[]={"+","-","*","/"};
    void createf(int num1) {
        double x = (int) (Math.random() * num1);
        double y = (int) (Math.random() * num1);
        double h = (int) (Math.random() * num1);
        int i = (int) ((Math.random() * num1)+3);
        
        int z = (int) (Math.random() * 4);
        int z1 = (int) (Math.random() * 4);
        
        if ( i == 2 )
        {
            sty = x + k[z] + y;
            if(k[z]=="+")
            {result=x+y;}
            if(k[z]=="-")
            {result=x-y;}
            if(k[z]=="*")
            {result=x*y;}
            if(k[z]=="/")
            {result=x/y;}
        }
        if ( i == 3 )
        {
            sty = x + k[z] + y +k[z1] + h;
            if(k[z]=="+")
            {
                if(k[z1]=="+")
                    result = x + y + h ; 
                if(k[z1]=="-")
                    result = x + y - h ;
                if(k[z1]=="*")
                    result = x + (y * h) ;
                if(k[z1]=="/")
                    result = x +( y / h );
            }
            if(k[z]=="-")
            {
                if(k[z1]=="+")
                    result = x - y + h ; 
                if(k[z1]=="-")
                    result = x - y - h ;
                if(k[z1]=="*")
                    result = x -( y * h) ;
                if(k[z1]=="/")
                    result = x - (y / h) ;
            }
            if(k[z]=="*")
            {
                if(k[z1]=="+")
                    result = (x * y) + h ; 
                if(k[z1]=="-")
                    result = (x * y) - h ;
                if(k[z1]=="*")
                    result = (x * y) * h ;
                if(k[z1]=="/")
                    result = (x * y) / h ;
            }
            if(k[z]=="/")
            {
                if(k[z1]=="+")
                    result = (x / y) + h ; 
                if(k[z1]=="-")
                    result = (x / y) - h ;
                if(k[z1]=="*")
                    result = (x / y) * h ;
                if(k[z1]=="/")
                    result = (x / y) / h ;
            }
        }
        
        if ( i == 4 )
        {
            int i1 = (int) (Math.random() * num1);
            if(k[z]=="+")
            {
                if(k[z1]=="+"){
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = x + y + h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x + (y + h) ;
                    }
                }
            
                    
                            
                if(k[z1]=="-")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = x + y - h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x + (y - h) ;
                    }
                }
                if(k[z1]=="*")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x + y) * h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x + (y * h) ;
                    }
                }
                if(k[z1]=="/")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x + y) / h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x + (y / h) ;
                    }
                }
            }
            if(k[z]=="-")
            {
                if(k[z1]=="+")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x - y) * h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x - (y + h) ;
                    }
                }
                if(k[z1]=="-")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x - y) - h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x - (y - h) ;
                    }
                }
                if(k[z1]=="*")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x - y) * h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x -( y * h) ;
                    }
                }
                if(k[z1]=="/")
                {
                    if(i1==0){
                        sty = "("+x+k[z]+y+")"+k[z1]+z + "=";
                        result = (x - y) / h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x - (y / h) ;
                    }
                }
            }
            if(k[z]=="*")
            {
                if(k[z1]=="+")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x * y + h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x * (y + h) ;
                    }
                }
                if(k[z1]=="-")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x * y + h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x * (y + h) ;
                    }
                }
                if(k[z1]=="*")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x * y * h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x * (y * h) ;
                    }
                }
                if(k[z1]=="/")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x * y / h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x * (y / h) ;
                    }
                }
            }
            if(k[z]=="/")
            {
                if(k[z1]=="+")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x / y + h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x / (y + h) ;
                    }
                }
                if(k[z1]=="-")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x / y - h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x / (y - h) ;
                    }
                }
                if(k[z1]=="*")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x / y * h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x / (y * h) ;
                    }
                }
                if(k[z1]=="/")
                {
                    if(i1==0){
                        sty = x+k[z]+y+k[z1]+z + "=";
                        result = x / y / h ;
                    }
                    if(i1==1){
                        sty = x+k[z]+"("+y+k[z1]+z +")"+ "=";
                        result = x / (y / h) ;
                    }
                }
            }
        
        }
        BigDecimal bg = new BigDecimal(result);
        f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();




        

        
    }

        
    

    // {
    // Button btn = (Button) findViewById(R.id.button3);// 获取按钮
    // btn.setOnClickListener(new View.OnClickListener() { // 设置按钮单击事件
    // @Override
    // public void onClick(View v) {
    //
    // TextView tv = (TextView) findViewById(R.id.editText2);// 获取textview组件
    //
    // tv.setText(sty);// 在textview中显示
    // }
    // });
    // }

}

MainActivity3:

package com.example.demon;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity3  extends Activity{
    private Button b1;
    private Button b2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
//        Intent fromPlusIntent = getIntent(); 
//        String num1Str = fromPlusIntent.getStringExtra("num5"); 
//        int num2 = Integer.parseInt(num1Str);
//        TextView t = (TextView) findViewById(R.id.textView1);
//            t.setText(num2);
        b1.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(MainActivity3.this,MainActivity1.class);
                startActivity(intent);
            }
            
            
        });
        b2.setOnClickListener(new OnClickListener(){
            @Override
            public void onClick(View v){
                Intent intent = new Intent(MainActivity3.this,MainActivity.class);
                startActivity(intent);
            }
            
            
        });
    }

}

 

开始界面:

出题界面:

答题界面:

返回界面:

同组:20133064 唐野野 http://www.cnblogs.com/tyyhph/

以上是关于四则运算(android版)的主要内容,如果未能解决你的问题,请参考以下文章

Android 片段复制

[vscode]--HTML代码片段(基础版,reactvuejquery)

Android 插件化VirtualApp 源码分析 ( 目前的 API 现状 | 安装应用源码分析 | 安装按钮执行的操作 | 返回到 HomeActivity 执行的操作 )(代码片段

Android代码片段

Android课程---Android Studio使用小技巧:提取方法代码片段

Android 实用代码片段