• “二柱子四则运算”终结版


    “二柱子四则运算”终结版

    我希望老师告诉我:“二柱子”的程序可以告一段落了,谁知道呢,唉……

    这次依旧是结对开发的项目,不过应该是最后一个了吧……,项目要求如下:

    1、生成的题目中计算过程不能产生负数;

    2、题目中生成的除法算式,其结果应该为真分数;

    3、每道题目中出现的运算符的个数不超过3个,括号不限;

    4、程序一次运行生成的题目不能重复

    5、把程序变成一个网页程序或者只能手机程序,用户通过设定参数,就可以得到各种题目,并可以实现在线答题并评判


    一、实验项目简介

          鉴于我跟孔同学的自我认识,我们觉得水平不够因此开始的时候我们打算做以前学过的网页版或者是C#电脑版,但是苦于时间不够加上没有参考我们一直没有完成。后来看到班里的一些牛们发表了安卓小程序,而我这学期也正在学习安卓入门,最后我们决定克服恐惧心里编写安卓程序。孔同学这学期没有选安卓课上,因此我们的分工很明显,她负责整体框架以及功能的设置,还有资料和代码的阅读搜集,然后我负责执行(虽然我也真心不怎么会……)我们分别阅读了同学们和网上相关计算器的代码,并照猫画虎的绘出了我们的界面,在此真心要感谢班级了的同学,你们的实验给了我很多帮助和经验灵感,这也是我们这次得以完成任务的原因之一。

         本次实验的安卓程序名为“四则运算生成器”,是使用JAVA语言在Andriod Eclipse上编写的,主要功能是用户可订制题目数量、数值范围还有进行计算练习题的类型,分别用TextView、RadioButton实现的(在这里声明,在实现计算类型选择的问题时我们参考了同班一个同学的RadioButton实验代码……),然后将控件获取的值传递到第二个界面实现题目的随机生成。然后在计算界面可以显示用户在前一页上进行的选择,然后依次出题,用户作答,之后判断正误,显示正确答案,最后统计正确率。最后我们将项目打包成为了.APK文件,可以跨手机进行安装。

          这次实验是我本学期的第一个安卓实验项目,而且时间紧迫,我和孔同学又是临时决定编写安卓程序的,因此我们在界面上有参考,代码上也有极尽不完美的地方,功能上比如分数的题目等等都有待改进。课余时间我们一定会吸取教训,争取能够完善项目,同时也希望老师和同学们谅解。

    二、实验源代码

     CalculateNow.java文件

      1 package i.love.icalculator;
      2 
      3 import tools.calculate.*;
      4 
      5 import java.util.Random;
      6 
      7 import android.os.Bundle;
      8 import android.app.Activity;
      9 import android.content.Intent;
     10 
     11 import android.view.View;
     12 import android.widget.EditText;
     13 import android.widget.TextView;
     14 import android.widget.Toast;
     15 
     16 import org.luyang.csdn.*;
     17 
     18 public class CalculateNow extends Activity {
     19 
     20     //变量*************************************************************
     21     int level, quizCount, maxNumber, number,count1,count2;
     22     String question,answer;
     23     @Override
     24     protected void onCreate(Bundle savedInstanceState) {
     25         super.onCreate(savedInstanceState);
     26         setContentView(R.layout.activity_calculate_now);
     27 
     28         // 从前面页面得到配置信息==============开始=================
     29         //TextView tv_level = (TextView) findViewById(R.id.tv_level);
     30         TextView tv_quizCount = (TextView) findViewById(R.id.tv_quizCount);
     31         TextView tv_maxNumber = (TextView) findViewById(R.id.tv_maxNumber);
     32         TextView tv_showQuestionNumber = (TextView) findViewById(R.id.tv_showQuestionNumber);
     33 
     34         Intent intent = getIntent();
     35         level = intent.getIntExtra("level", 0);
     36         quizCount = intent.getIntExtra("quizCount", 0);
     37         maxNumber = intent.getIntExtra("maxNumber", 0);
     38         count1=intent.getIntExtra("count1", 0);
     39         count2=intent.getIntExtra("count2", 0);
     40         //tv_level.setText(String.valueOf(level));
     41         tv_quizCount.setText(String.valueOf(quizCount));
     42         tv_maxNumber.setText(String.valueOf(maxNumber));
     43         // 从前面页面得到配置信息==============结束=================
     44 
     45         number = 1;
     46         tv_showQuestionNumber.setText("第" + String.valueOf(number) + "题");
     47 
     48         calculate c = new calculate();
     49 
     50      question = c.Question(level, maxNumber);
     51         answer = String.valueOf(new CalStr(question).getResult());
     52         TextView tv_showQuestion = (TextView) findViewById(R.id.tv_showQuestion);
     53 
     54         tv_showQuestion.setText(question + "=" );
     55     }
     56     
     57 //函数*****************************************************************
     58     public void answerSubmit(View v) //提交
     59     {
     60         
     61         EditText et_answer=(EditText) findViewById(R.id.et_getAnswer);
     62         TextView tv_checkYes=(TextView) findViewById(R.id.checkYes);
     63         try {
     64             if(et_answer.length()==0){
     65                 Toast.makeText(this, "请回答问题", 0).show();
     66             }
     67             else if(Float.parseFloat(et_answer.getText().toString().trim())==Float.parseFloat(answer)){
     68                 count1++;
     69                 tv_checkYes.setText("恭喜你答对了!!!"+"
    "+"一共答对了"+count1+"道题目,答错了"+count2+"道题目。");
     70                 }
     71             else{
     72                 count2++;
     73                 tv_checkYes.setText("很抱歉答错了,答案是"+answer+"
    "+"一共答对了"+count1+"道题目,答错了"+count2+"道题目。");
     74             }
     75             
     76         } catch (NumberFormatException e) {
     77         Toast.makeText(this, "请输入正常的数字", 0).show();
     78         return;
     79             
     80         }
     81         
     82         
     83         if (number < quizCount) {
     84             number++;
     85             calculate c = new calculate();
     86 
     87             TextView tv_showQuestionNumber = (TextView) findViewById(R.id.tv_showQuestionNumber);
     88             TextView tv_showQuestion = (TextView) findViewById(R.id.tv_showQuestion);
     89 
     90             tv_showQuestionNumber.setText("第" + String.valueOf(number) + "题");
     91             question = c.Question(level, maxNumber);
     92             answer = String.valueOf(new CalStr(question).getResult());
     93             tv_showQuestion.setText(question + "=" );
     94 
     95         
     96     
     97     }
     98         else{
     99             Toast.makeText(this, "全部的题已经做完,请重新开始,或者退出系统", 1).show();
    100         }
    101         et_answer.setText("");
    102 
    103 }
    104     public void Clear(View v)//重置
    105     {
    106         TextView txtView = (TextView) findViewById(R.id.et_getAnswer);
    107         txtView.setText(" ");
    108     }
    109     
    110 
    111 }

    CalculatorConfig.java 文件

     1 package i.love.icalculator;
     2 
     3 import android.os.Bundle;
     4 import android.app.Activity;
     5 import android.content.Intent;
     6 
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.EditText;
    10 import android.widget.RadioButton;
    11 import android.widget.Toast;
    12 
    13 public class CalculatorConfig extends Activity {
    14 
    15     Button bt_startQuiz;
    16     RadioButton rb_level1, rb_level2, rb_level3, rb_level4;
    17     EditText et_quizCount, et_maxNumber;
    18 
    19     @Override
    20     protected void onCreate(Bundle savedInstanceState) {
    21         super.onCreate(savedInstanceState);
    22         setContentView(R.layout.activity_calculator_config);
    23 
    24         bt_startQuiz = (Button) findViewById(R.id.bt_startQuiz);
    25         rb_level1 = (RadioButton) findViewById(R.id.rb_level1);
    26         rb_level2 = (RadioButton) findViewById(R.id.rb_level2);
    27         rb_level3 = (RadioButton) findViewById(R.id.rb_level3);
    28         rb_level4 = (RadioButton) findViewById(R.id.rb_level4);
    29         et_quizCount = (EditText) findViewById(R.id.et_quizCount);
    30         et_maxNumber = (EditText) findViewById(R.id.et_maxNumber);
    31 
    32     }
    33 
    34     // 判断得到试题级别
    35     public int checkWhichLevelIsSelected() {
    36         if (rb_level1.isChecked()) {
    37             return 1;
    38         } else if (rb_level2.isChecked()) {
    39             return 2;
    40         } else if (rb_level3.isChecked()) {
    41             return 3;
    42         } else if (rb_level4.isChecked()) {
    43             return 4;
    44         } else {
    45             return 0;
    46         }
    47     }
    48 
    49     // 点击开始答题按钮方法
    50     public void startQuiz(View v) {
    51 
    52         if (et_quizCount.length() == 0) {
    53             Toast.makeText(this, "请输入题目数量", Toast.LENGTH_SHORT).show();
    54             return;
    55         } else if (et_maxNumber.length() == 0) {
    56             Toast.makeText(this, "请输入运算式中数的最大值", Toast.LENGTH_SHORT).show();
    57             return;
    58         } else if (Integer.parseInt(et_quizCount.getText().toString()) == 0) {
    59             Toast.makeText(this, "题目数量不能为0", Toast.LENGTH_SHORT).show();
    60             return;
    61         }  else if (Integer.parseInt(et_maxNumber.getText().toString()) == 0) {
    62             Toast.makeText(this, "最大值不能为0", Toast.LENGTH_SHORT).show();
    63             return;
    64         } else {
    65             int level = checkWhichLevelIsSelected();
    66             int quizCount = Integer.parseInt(et_quizCount.getText().toString()
    67                     .trim());
    68             int maxNumber = Integer.parseInt(et_maxNumber.getText().toString()
    69                     .trim());
    70 
    71             Intent intent = new Intent(this, CalculateNow.class);
    72             intent.putExtra("level", level);
    73             intent.putExtra("quizCount", quizCount);
    74             intent.putExtra("maxNumber", maxNumber);
    75             startActivity(intent);
    76         }
    77     }
    78 }

    三、实验效果实现

     主界面:                                                        开始答题:

             

    点击“提交”按钮:                                                点击“重置”按钮后,文本被清除:

            

    之后每道题目的显示:

             

               

    四、实验心得

         通过这次二柱子的实验,除了在技术上我“逼迫”自己学习安卓有关的知识。能够自己有机会实现老师在课堂上讲的配置id,布置各个控件,编辑控件单击事件等等函数。安卓程序与C#一样都是可视化的编程,但是区别还是蛮明显。C#是用C语言为基本语言编写的,而安卓是用JAVA语言。我们对JAVA语言的熟悉度显然没有C熟悉。还有就是,可视化的界面设计不像C++程序那样所有的功能都是用代码来实现的,这样有利又有弊吧,比如我们可以简单的设计自己的界面,但是这样就不容易像别人学习代码,因为在同样的界面下面将函数加载到不同的地方实现的功能可能完全不一样。

         此外,以为这次作业的任务量比较大,我们都有畏惧心理,所以拖延症又犯了,原计划清明小长假的时候完成的任务拖到昨天今天才弄完。下次的项目我们一定要做好计划,然后每天或者每一阶段严格按照计划实施。

    最后附上同组的孔童鞋博客主页,大家都辛苦了~~~

    http://www.cnblogs.com/kongyuhang/

    Keyboard not found...press F1 to continue:)
  • 相关阅读:
    513. Find Bottom Left Tree Value(LeetCode)
    647. Palindromic Substrings(LeetCode)
    537. Complex Number Multiplication(LeetCode)
    338. Counting Bits(LeetCode)
    190. Reverse Bits(leetcode)
    Java多线程核心技术
    正则表达式
    linux 怎么把^M去掉
    分片与分区的区别
    《MYSQL技术精粹》读书笔记
  • 原文地址:https://www.cnblogs.com/2016helen/p/5360651.html
Copyright © 2020-2023  润新知