四則運算APP最後階段

38鄭勝斌發表於2015-12-17

四則運算APP最後階段

【開發環境】:eclipse

【開發專案】:小學生四則運算APP

【開發人員】:鄭勝斌 http://www.cnblogs.com/zsb1/            

                   孔德穎 http://www.cnblogs.com/kong21/

                   李豌湄 http://www.cnblogs.com/leewanmei/    

                   江丹儀 http://www.cnblogs.com/JDY64/

【Github】:https://github.com/zhengshengbin/sizeyunsuan

 

在最後階段中,我們對APP出題部分進行修改,例如把*號改為×,/號改為÷,這樣會跟加直觀。另外在初級的出題上也進行了修改,不會出現很難解答的題目。然後在提交答案後,我們對提交答案部分進行了判斷,若提交的答案是錯的,就會顯示紅色。修改的程式碼和截圖如下:

  

package com.de.sizhe;

import java.math.RoundingMode;
import java.text.DecimalFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;

public class ChujiActivity extends Activity {

    private TableLayout tableLayout;
    private TextView tv1;
    private Button b1;
    private int count=0;
    private int t=0;
    static DecimalFormat decimal = new DecimalFormat("#.##");
    
    private Handler handler=new Handler();
    private Runnable runnable=new Runnable() {
        
        @Override
        public void run() {
            // TODO Auto-generated method stub
            ++t;
            tv1.setText("時間:"+String.valueOf(t));
            handler.postDelayed(runnable, 1000);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tgaoji);
        handler.postDelayed(runnable, 1000);
        
        final Intent intent1=new Intent(ChujiActivity.this,TDeFenActivity.class);
        tableLayout=(TableLayout) findViewById(R.id.GTab);
        b1=(Button) findViewById(R.id.b1);
        tv1=(TextView) findViewById(R.id.shijian);
        
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        count=bundle.getInt("tishu");//獲取題目數量        
        
        String str1 = new String();    //題目
        final String s[] = new String[count]; //題目陣列
        final String[] str=new String[count];  //使用者輸入的答案陣列
        final String[] bjanswer = new String[count];// 標準答案
        int[] num1 = new int[4];   //計算的各個數值

        final EditText[] answer=new EditText[count];
        TextView[] show=new TextView[count];

        for(int i=0;i<count;i++)
        {
            int n=2;//4個運算元
            char[] op = { '+', '-', '×', '÷' };
            int[] no = new int[4];
            int cs;// 正負引數

            TableRow tableRow=new TableRow(this);
            show[i]=new TextView(this);
            answer[i]=new EditText(this);

            tableRow.addView(show[i]);
            tableRow.addView(answer[i]);

            for (int j = 0; j < n; j++) 
            {
                cs = (int) (Math.random() * 2);
                if (cs == 0)// 負數
                    num1[j] = -(int) (Math.random() * 10);// 控制隨機數數值
                else// 正數
                    num1[j] = (int) (Math.random() * 10);// 控制隨機數數值
            }

            for (int k = 0; k < n - 1; k++) 
            {
                no[k] = (int) (Math.random() * 4);// 隨機產生操作符
                if (no[k] == 3 && num1[k + 1] == 0) 
                {
                    do {
                        num1[k + 1] = (int) (Math.random() * 100);// 如果除號後為0,則重新取數。
                        } while (num1[k + 1] == 0);
                }
            }
            
            for (int h = 0; h < n; h++) 
            {
                if (h != n - 1)
                {
                    if (num1[h] < 0)
                        str1 = str1 +"  (" + String.valueOf(num1[h])+")" + String.valueOf(op[no[h]]);
                    else
                        str1 = str1 +"  "+ String.valueOf(num1[h])+ String.valueOf(op[no[h]]);
                } 
                else
                {
                    if (num1[h] < 0)
                        str1 = str1 +"  (" + String.valueOf(num1[h]) +")=";
                    else
                        str1 = str1+"  " + String.valueOf(num1[h]) + "=";
                }
            }
            s[i] = str1;
            str1 = new String();

            
            
            // 計算標準答案
            int sign; // 累加運算時的符號
            float left, right;// 儲存蹭結果
            decimal.setRoundingMode(RoundingMode.HALF_UP);
            left = 0;
            right = num1[0];
            sign = 1;

            for (int g = 0; g < n - 1; g++)
            {
                switch (op[no[g]])
                {
                case '+':
                    left = left + sign * right;
                    sign = 1;
                    right = num1[g + 1];
                    break;
                case '-':
                    left = left + sign * right;
                    sign = -1;
                    right = num1[g + 1];
                    break;
                case '×':
                    right = right * num1[g + 1];
                    break;
                case '÷':
                    right = right / num1[g + 1];
                    break;
                }
            }
            bjanswer[i] = String.valueOf(decimal.format(left + sign * right));

            show[i].setText(s[i]);
            show[i].setTextSize(18);
            answer[i].setEms(5);
            answer[i].setId(i);
            tableLayout.addView(tableRow);
        }
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                for(int i=0;i<count;i++)
                {
                    str[i]=answer[i].getText().toString();        //使用者輸入的答案陣列            
                }
                Bundle bundle=new Bundle();
                bundle.putStringArray("timu", s);
                bundle.putStringArray("useranswer", str);
                bundle.putStringArray("bjanswer", bjanswer);
                bundle.putInt("Ttishu", count);
                bundle.putString("shijian", String.valueOf(t));
                intent1.putExtras(bundle);
                startActivity(intent1);                
                handler.removeCallbacks(runnable);
                finish();
            }
        });
    }


}
chujiActivity
package com.de.sizhe;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class TDeFenActivity extends Activity {

    private Button cxbtn,tcbtn;
    private TableLayout dtab1;
    private TextView textView;
    private int count=0;
    private String sj=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tde_fen);        
        dtab1=(TableLayout) findViewById(R.id.DTab1);
        cxbtn=(Button) findViewById(R.id.chongxin);
        tcbtn=(Button) findViewById(R.id.tuichu);
        textView=(TextView) findViewById(R.id.showtime);
        
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        count=bundle.getInt("Ttishu");
        
        String[] sty=new String[count];
        String[] stb=new String[count];
        String[] stimu=new String[count];
        
        sty=bundle.getStringArray("useranswer");
        stb=bundle.getStringArray("bjanswer");
        sj=bundle.getString("shijian");
        stimu=bundle.getStringArray("timu");
        
        
        textView.setText("完成時間為:"+sj+"s");
        int tn=(count+1)*3;
        TextView[] Tshow=new TextView[tn];
        
        TableRow tableRow=new TableRow(this);
        Tshow[0]=new TextView(this);
        Tshow[1]=new TextView(this);
        Tshow[2]=new TextView(this);
        Tshow[0].setText("                   題目");
        Tshow[1].setText("      你的答案");
        Tshow[2].setText("      正確答案");
        tableRow.addView(Tshow[0]);
        tableRow.addView(Tshow[1]);
        tableRow.addView(Tshow[2]);
        dtab1.addView(tableRow);
        for(int i=0;i<count;i++)
        {
            TableRow tableRow1=new TableRow(this);
            Tshow[(i+1)*3]=new TextView(this);
            Tshow[(i+1)*3+1]=new TextView(this);
            Tshow[(i+1)*3+2]=new TextView(this);
            tableRow1.addView(Tshow[(i+1)*3]);
            tableRow1.addView(Tshow[(i+1)*3+1]);
            tableRow1.addView(Tshow[(i+1)*3+2]);
            
            if(sty[i].equals(stb[i]))
            {
                Tshow[(i+1)*3].setText(stimu[i]);
                Tshow[(i+1)*3+1].setText(sty[i]);
                Tshow[(i+1)*3+1].setGravity(Gravity.RIGHT);
                Tshow[(i+1)*3+2].setText(stb[i]);
                Tshow[(i+1)*3+2].setGravity(Gravity.RIGHT);
            }
            else
            {
                Tshow[(i+1)*3].setText(stimu[i]);
                Tshow[(i+1)*3+1].setText(sty[i]);
                Tshow[(i+1)*3+1].setGravity(Gravity.RIGHT);
                Tshow[(i+1)*3+1].setTextColor(Color.RED);
                Tshow[(i+1)*3+2].setText(stb[i]);
                Tshow[(i+1)*3+2].setGravity(Gravity.RIGHT);
            }
            dtab1.addView(tableRow1);
        }
        
        cxbtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent=new Intent(TDeFenActivity.this,TixingActivity.class);
                startActivity(intent);
                finish();
            }
        });
        
        tcbtn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                finish();
            }
        });
        
    }

}
TDeFenActivity

 

相關文章