RayShea

导航

基于android平台的出题软件---- 每日30题

本app共编写了3个activity,1.Mainactivity作为主界面。2.surface,用来显示随机出的题。3.showresult,用来打印所有做过的题(含结果),一个类function用来随机出题。APK下载链接http://pan.baidu.com/s/1o7fiFNG

开发时间约6个小时

具体调用、跳转如图1

图一  程序整体结构

 

 

 

接下来具体展示每一个模块。

 模块1:class:function

使用java自带函数,Random.nextInt()来获得随机数,类中共有两个public Long类型的数据Former和Latter,Former与Latter分别表示算术中运算符前及运算符之后的两个操作数。类中共有五个函数,分别是

  • public long getRandom(int num)
  • public long setPlus(long former,long latter)
  • public long setMinus(long former,long latter)
  • public long setMutiply(long former,long latter)
  • public long setDivide(long former,long latter)

getrandom函数用来获取随机数,剩余四个用来设置加减乘除的算式,并返回运算结果。具体代码如下

 1 package com.example.shea.caculator;
 2  
 3 import java.util.Random;
 4 
 5 /**
 6  * Created by shea on 2016/3/10.
 7  */
 8 public class function {
 9     public  long Former=0;
10     public  long Latter=0;
11     public long getRandom(int num){
12         int NUM=num;
13         Random random=new Random();
14         int number=random.nextInt(NUM)+1;
15         return number;
16     }
17     public long setPlus(long former,long latter){
18         return former+latter;
19     }
20     public long setMinus(long former,long latter){
21         long result=0;
22         boolean Flag=true;
23         do {
24             if (former>latter)
25             {
26                 Former=former;
27                 Latter=latter;
28                 Flag=false;
29             }
30             else
31             if (former<latter)
32             {
33                 Former=latter;
34                 Latter=former;
35                 Flag=false;
36             }
37             else {
38                 Former=getRandom(100);
39                 Latter=getRandom(100);
40                 Flag=true;
41             }
42 
43 
44         }while (Flag);
45         result=Former-Latter;
46         return result;
47     }
48     public long setMutiply(long former,long latter){
49         return former*latter;
50     }
51     public long setDivide(long former,long latter)
52     {
53         long result=0;
54         boolean FLAG=true;
55         do{
56 
57             if (former%latter==0)
58             {
59                 result=former/latter;
60                 FLAG=false;
61             }
62             else{
63                 Former=getRandom(100)+1;
64                 Latter=getRandom(19)+1;
65                 former=Former;
66                 latter=Latter;
67                 result=Former/Latter;
68                 System.out.println("============"+Former+"==========="+Latter+"===================");
69             }
70 
71         }while (FLAG);
72         return result;
73     }
74 }

 

模块2:Activity:surface

本模块负责打印由function获得的随机加减乘除运算,真机运行截图如下。图4是完成三十道题之后的弹窗,此时可以选择再做30道,或者回到首页。做使用过程中,可以点击打印题目按钮获得目前做过的题目及答案,也可以点击回到主页按钮,回到主页。

本模块

public long setquestion()函数负责出题,并且使用textview呈现在surface.java上,
在其中使用int CHOOSENUM=random.nextInt(3);随机获得0-3的某个int类型的数,分别对应加减乘除,使用switch case跳转至对应出题方式。

图二

图三

图四

本模块代码如下:

  1 package com.example.shea.caculator;
  2 //ray's work
  3 //2013040101016
  4 //雷翔宇
  5 
  6 import android.app.AlertDialog;
  7 import android.content.DialogInterface;
  8 import android.content.Intent;
  9 import android.graphics.Color;
 10 import android.support.v7.app.AppCompatActivity;
 11 import android.os.Bundle;
 12 import android.view.View;
 13 import android.widget.Button;
 14 import android.widget.EditText;
 15 import android.widget.TextView;
 16 import android.widget.Toast;
 17 
 18 import java.util.ArrayList;
 19 import java.util.Random;
 20 
 21 public class surface extends AppCompatActivity {
 22     private long former=0;
 23     private long latter=0;
 24     private long result=0;
 25     private long count=1;
 26     private long RIGHT=0;
 27     private float percentage=0;
 28     ArrayList<String> arrayList=new ArrayList<String>();
 29 
 30     @Override
 31     protected void onCreate(Bundle savedInstanceState) {
 32         super.onCreate(savedInstanceState);
 33         setContentView(R.layout.surface_layout);
 34         final EditText editText= (EditText) findViewById(R.id.INPUTTEXT);
 35         Button next= (Button) findViewById(R.id.next);
 36         Button homepage= (Button) findViewById(R.id.homepage);
 37         Button submit=(Button)findViewById(R.id.submit);
 38         final TextView countnum= (TextView) findViewById(R.id.countnum);
 39         final Button back2home=(Button)findViewById(R.id.backtohome);
 40         setquestion();
 41         back2home.setOnClickListener(new View.OnClickListener() {
 42             @Override
 43             public void onClick(View v) {
 44 
 45                 Intent back=new Intent(surface.this,MainActivity.class);
 46                 startActivity(back);
 47             }
 48         });
 49 
 50         homepage.setOnClickListener(new View.OnClickListener() {
 51             @Override
 52             public void onClick(View v) {
 53                /* Intent intent = new Intent(surface.this, MainActivity.class);
 54                 startActivity(intent);*/
 55                 StringBuffer partinfo=new StringBuffer("");
 56                 StringBuffer ALLINFO=new StringBuffer("");
 57                 for (int i = 0; i <arrayList.size() ; i++) {
 58                     //allquestions.add(arrayList.get(i));
 59                     String info=  arrayList.get(i);
 60                     partinfo.append(info+"\n");
 61                     //ALLINFO.append(partinfo);
 62                 }
 63                 String allinfo=partinfo.toString();
 64                 Intent intent=new Intent(surface.this,showresult.class);
 65                 intent.putExtra("data",allinfo);
 66                 startActivity(intent);
 67 
 68 
 69             }
 70         });
 71         submit.setOnClickListener(new View.OnClickListener() {
 72             @Override
 73             public void onClick(View v) {
 74                 String getnum = editText.getText().toString();
 75                 int GETNUM = Integer.valueOf(getnum);
 76                 if (GETNUM == result) {
 77                     Toast.makeText(surface.this, "回答正确~", Toast.LENGTH_SHORT).show();
 78                     RIGHT++;
 79                 } else {
 80                     Toast.makeText(surface.this,"做错了呢~请再细心计算一遍",Toast.LENGTH_SHORT).show();
 81                 }
 82 
 83             }
 84         });
 85         next.setOnClickListener(new View.OnClickListener() {
 86             @Override
 87             public void onClick(View v) {
 88                 editText.setText("");
 89                 countnum.setText("已经做了"+count+"道题,做对"+RIGHT+"道\n");count++;
 90                 if (count>30) {
 91                     /*Toast.makeText(surface.this, "今天的30道题已经完成啦,你真棒!~", Toast.LENGTH_LONG).show();*/
 92                     count=30;
 93                     new AlertDialog.Builder(surface.this).setMessage("今天的任务完成啦!").setPositiveButton("查看今天的题目", new DialogInterface.OnClickListener() {
 94                         @Override
 95                         public void onClick(DialogInterface dialog, int which) {
 96                             StringBuffer partinfo=new StringBuffer("");
 97                             StringBuffer ALLINFO=new StringBuffer("");
 98                             for (int i = 0; i <arrayList.size() ; i++) {
 99                                 //allquestions.add(arrayList.get(i));
100                                 String info=  arrayList.get(i);
101                                 partinfo.append(info+"\n");
102                                 //ALLINFO.append(partinfo);
103                             }
104                             String allinfo=partinfo.toString();
105                             Intent intent=new Intent(surface.this,showresult.class);
106                             intent.putExtra("data",allinfo);
107                             startActivity(intent);
108                         }
109                     }).setNegativeButton("再来三十道!", new DialogInterface.OnClickListener() {
110                         @Override
111                         public void onClick(DialogInterface dialog, int which) {
112                             count=1;
113                             countnum.setText("");
114                             setquestion();
115                         }
116                     }).create().show();
117 
118 
119                         }
120                 else
121                 result=setquestion();
122             }
123         });
124 
125     }
126     public long setquestion(){
127         TextView textView= (TextView) findViewById(R.id.textView);
128         function myfunc=new function();
129         former=myfunc.getRandom(100)+1;
130         latter=myfunc.getRandom(100)+1;
131         Random random=new Random();
132         int CHOOSENUM=random.nextInt(3);
133         //int CHOOSENUM = 1;
134         switch (CHOOSENUM)
135         {
136             case 0:{result=myfunc.setPlus(former,latter);
137                 textView.setText(former + "+" + latter+"=");
138                 String text=former + "+" + latter+"="+result;
139                 arrayList.add(text);
140             }
141             break;
142             case 1:{result=myfunc.setDivide(former,latter);
143                 textView.setText(myfunc.Former + "÷" + myfunc.Latter+"=");
144                 String text=myfunc.Former + "÷" + myfunc.Latter+"="+result;
145                 arrayList.add(text);
146             }
147             break;
148             case 2:{
149                 result=myfunc.setMinus(former,latter);
150                 textView.setText(myfunc.Former + "-" + myfunc.Latter+"=");
151                 String text=myfunc.Former + "-" + myfunc.Latter+"="+result;
152                 arrayList.add(text);
153             }
154             break;
155             case 3:{
156                 result=myfunc.setMutiply(former,latter);
157                 textView.setText(myfunc.Former + "×" + myfunc.Latter+"=");
158                 String text=myfunc.Former + "×" + myfunc.Latter+"="+result;
159                 arrayList.add(text);
160             }
161             break;
162         }
163         return result;
164 
165     }
166 }

 

模块3 Activity showresult

本模块负责打印已经做过的所有题目,因为在ACTIVITY:surface中使用intent进行跳转,故使用intent的putextra方法传递所有题目数据,在activity中添加了scrollview及嵌套的textview来展示结果。效果图如下

本模块代码如下

 1 package com.example.shea.caculator;
 2 //ray's work
 3 //2013040101016
 4 //雷翔宇
 5 
 6 import android.content.Intent;
 7 import android.support.v7.app.AppCompatActivity;
 8 import android.os.Bundle;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.ListView;
12 import android.widget.TextView;
13 
14 import java.util.ArrayList;
15 
16 public class showresult extends AppCompatActivity {
17 
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_showresult);
23         Button back2homepage= (Button) findViewById(R.id.back2homepage);
24         TextView textview= (TextView) findViewById(R.id.textView2);
25         Intent i=getIntent();
26         String info=i.getStringExtra("data");
27         textview.setText(info);
28         back2homepage.setOnClickListener(new View.OnClickListener() {
29             @Override
30             public void onClick(View v) {
31                 Intent back2homepage=new Intent(showresult.this,MainActivity.class);
32                 startActivity(back2homepage);
33             }
34         });
35     }
36 }

 

因时间关系,程序内存在一些错误未能及时更正,还请海涵。

 

posted on 2016-03-12 17:52  RayShea  阅读(685)  评论(4编辑  收藏  举报