程式碼·--四則運算的主要核心程式碼

陳汝婷發表於2015-12-07

  之前申請的github賬戶,不知道怎麼回事一直無法儲存程式碼,所以我們決定在這裡展現一些程式碼。

MainActivity程式碼:(關於activity_main.xml介面)

package com.example.brdemo;

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

public class MainActivity extends Activity {
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private EditText  et1;
    private EditText  et2;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            btn1=(Button)findViewById(R.id.b1);
            btn2=(Button)findViewById(R.id.button2);
            btn3=(Button)findViewById(R.id.b2);
            et2=(EditText)findViewById(R.id.editText2);
            et1=(EditText)findViewById(R.id.editText1);
            
            btn1.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent();  
                    Bundle bundle=new Bundle();
                    //設定Intent的class屬性,跳轉到SecondActivity  
                    intent.setClass(MainActivity.this, H.class);  
                    //為intent新增額外的資訊  
                    bundle.putString("userName", et1.getText().toString());  
                    bundle.putString("phoneNum", et2.getText().toString());
                    intent.putExtras(bundle);
                    //啟動Activity  
                    startActivity(intent);  

                }
            });
           btn3.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    Intent intent = new Intent();  
                    Bundle bundle=new Bundle();
                    //設定Intent的class屬性,跳轉到SecondActivity  
                    intent.setClass(MainActivity.this, H.class);  
                    //為intent新增額外的資訊  
                    bundle.putString("userName", et1.getText().toString());  
                    bundle.putString("phoneNum", et2.getText().toString());
                    intent.putExtras(bundle);
                    //啟動Activity  
                    startActivity(intent);  

                }
            });
            btn2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Uri uri=Uri.parse("http://open.taobao.com");
                    Intent it=new Intent(Intent.ACTION_VIEW,uri);
                    startActivity(it);
                }
            });
        }

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

    }
    

CheckActivity.java(關於四則運算)

package com.example.brdemo;

import java.util.Random;

import android.app.ActionBar;
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;

public class CheckActivity extends Activity {
    public Button btn,btn1;
    private EditText  et1;
    private EditText  et2;
    long startTime;
    long endTime;
    private TextView t11;
    private TextView t4;
    private TextView t2;
    private TextView t3;
    private TextView t1;
    private TextView t5;
    private TextView t6;
        int ranswer=0;                 
        int uanswer=0;                 
        String str="";
        String z="";
        private boolean stop = false;
        public int num=5,T=0,F=0,count=1,b=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.szys);
        btn=(Button)findViewById(R.id.bb1);
        btn1=(Button)findViewById(R.id.bb2);
        et2=(EditText)findViewById(R.id.editText2);
        et1=(EditText)findViewById(R.id.editText1);
        t11=(TextView)findViewById(R.id.t11);
        t4=(TextView)findViewById(R.id.textView4);
        t6=(TextView)findViewById(R.id.textView6);
        t2=(TextView)findViewById(R.id.textView12);
        t5=(TextView)findViewById(R.id.textView10);
        t3=(TextView)findViewById(R.id.textView7);
        t1=(TextView)findViewById(R.id.textView14);
        ActionBar actionBar = getActionBar();//獲取ActionBar物件
        actionBar.setDisplayShowHomeEnabled(true);//顯示應用程式圖示
        actionBar.setDisplayHomeAsUpEnabled(true);//將應用程式圖示轉變為可點選圖示,並新增一個返回箭頭。
        btn1.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                 CheckActivity.this.finish();

            }
        });
        btn.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
            
                    T=0;F=0;
                    str="";
                    z="";
                    stop=false;

                   
                    try{
                        String bb = et1.getText().toString();
                        num=Integer.parseInt(bb);
                    if(num>15)
                    {
                        
                       t11.setText("輸入題數錯誤,請重新輸入!(1-15)");
                        et1.setText("");
                    }
                    else
                    {
                        startTime=System.currentTimeMillis();
                       t4.setText(""+T);
                       t2.setText(""+F);
                        t1.setText(""+b);
                       //btn.setVisible(false);
                        //et1.setEditable(false);
                        
                        showtext();
                        
                      
                    }
                   
                    }
             catch(NumberFormatException e1)
             {
                 t11.setText("輸入題數錯誤,請重新輸入!(1~15)");
                et1.setText("");
             }
             
             
         }      
        
        });

        et2.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
            
                try{
                    if(!stop)
                    {
                    
                        String tt = et2.getText().toString();
                    uanswer=Integer.parseInt(tt);;
                    if(num==count)
                    {
                        if(uanswer==ranswer) 
                        {  
                            t4.setText(""+ranswer);
                           t3.setText("答對!"); 
                            
                          
                            T++; 
                          t6.setText(""+T);  
                            b=(100/num)*T;
                            t1.setText(""+b);  
                         
                            if(b>90){
                                Intent intent=new Intent();
                                intent.setClass(CheckActivity.this,Test1.class);
                                startActivity(intent);
                                CheckActivity.this.finish();
                              
                            }
                            count++;    
                        }  
                        else 
                        {  
                            t4.setText(""+ranswer);
                            t3.setText("答錯!");
                            F++;
                            t2.setText(""+F);
                            count++;
                        }
                        stop=true;
                        t3.setText(" 答題結束!");
                        endTime=System.currentTimeMillis();
                        //et2.setEditable(true);
                       // btn.setVisible(true);
                      t5.setText((endTime-startTime)/1000+"s");
                        
                    }
                  
                    else if(uanswer==ranswer) 
                    {   
                       t4.setText(""+ranswer);
                        t3.setText("答對!"); 
                       
                        T++; 
                       t6.setText(""+T); 
                        b=(100/num)*T;
                        t1.setText(""+b); 
                        showtext();
                        if(b>90){
                            Intent intent=new Intent();
                            intent.setClass(CheckActivity.this,Test1.class);
                            startActivity(intent);
                            CheckActivity.this.finish();
                          
                        }
                        count++;
                        
                    }  
                    else
                    {   
                       t4.setText(""+ranswer);
                        t3.setText("答錯!");
                        F++;
                        t2.setText(""+F);
                        showtext();
                        count++;
                    }
                    }
                }
                    
                    catch(NumberFormatException e2)
                    {
                       t11.setText(str+"答案格式錯誤,請重新輸入答案!");
                        et2.setText("");
                    }
                

            }
        });
    }
           void showtext()
            {
                char op='+';
                int a=0,b=0,c=0,e=0,f=0,opnum=0,chioce=0;
                int temp=0;
                Random ra =new Random();
                e=ra.nextInt(10)+1;
                f=ra.nextInt(10)+1;
                chioce=ra.nextInt(2)+1;
                if(e%2==0)
                {
                a=ra.nextInt(10)+2;
                }
                else
                {
                    a=-ra.nextInt(10)+2;
                }
                if(f%2==0)
                {
                b=ra.nextInt(10)+2;
                }
                else
                {
                    b=-ra.nextInt(10)+2;
                }
                c=ra.nextInt(5);
                opnum=Math.abs(ra.nextInt())%4+1;    
                et2.setText("");
                switch(chioce)
                {
                case 1:
                {
                switch(opnum)  
                    {  
                    case 1:    
                        op='+';  
                        break;
                    case 2:  
                        op='-'; 
                        break;
                    case 3:  
                        op='*';    
                        break;
                    case 4:  
                        op='/';  
                        break; 
                    }                                               
                                                                    
                    if((op=='-') && (a<b)) 
                    {   
                        temp=a; 
                        a=b;
                        b=temp;  
                    }
                                                                  
                    if(op=='/')  
                    {   
                        if(b==0)
                        {    
                            b=1;  
                        }   
                        a=a*b; 
                    }  
                    if(a>0&&b>0)
                    {
                        z=String.valueOf(a)+String.valueOf(op)+String.valueOf(b)+"="+"\n";
                    }
                    else if(a<0&&b>0)
                    {
                        z="("+String.valueOf(a)+")"+String.valueOf(op)+String.valueOf(b)+"="+"\n";
                    }
                    else if(a>0&&b<0)
                    {
                        z=String.valueOf(a)+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
                    }
                    else
                    {
                        z="("+String.valueOf(a)+")"+String.valueOf(op)+"("+String.valueOf(b)+")"+"="+"\n";
                    }
                    str=str+z;                                                  
                    t11.setText(str); 
                
                    switch(op) 
                    {  
                    case '+':   ranswer=a+b;   break;
                    case '-':   ranswer=a-b;   break;
                    case '*':   ranswer=a*b;   break;
                    case '/':   ranswer=a/b;   break;  
                    } 
                    break;
                }
                case 2:
                {
                    int jc=1;
                    str=str+String.valueOf(c)+"!="+"\n";                                                
                    t11.setText(str);
                    for(int i=c;i>0;i--)
                    {
                        jc=jc*i;
                    }
                    ranswer=jc;
                    break;
                }
                }
            
        
    }
}

Help.java(關於四則運算的認識)

package com.example.brdemo;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class Help extends Activity {
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.help);
        ActionBar actionBar = getActionBar();//獲取ActionBar物件
        actionBar.setDisplayShowHomeEnabled(true);//顯示應用程式圖示
        actionBar.setDisplayHomeAsUpEnabled(true);//將應用程式圖示轉變為可點選圖示,並新增一個返回箭頭。
    }
    @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) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case R.id.font_10:
            Intent intent1=new Intent(this,CheckActivity.class);
            startActivity(intent1);
            break;
        case R.id.font_12:
            Intent intent2=new Intent(this,About.class);
            startActivity(intent2);
            break;
        case android.R.id.home:
            //建立啟動MainActivity的Intent
            Intent intent=new Intent(this,MainActivity.class);
            //新增額外的Flag,將Activity棧中處於MainActivity之上的Activity彈出
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            break;

        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    }

 

相關文章