作业6--再次冲刺即最终冲刺

  1 package com.example.zs;
  2 
  3 import java.util.*;
  4 
  5 import android.support.v7.app.ActionBarActivity;
  6 import android.support.v7.app.ActionBar;
  7 import android.support.v4.app.Fragment;
  8 import android.os.Bundle;
  9 import android.view.LayoutInflater;
 10 import android.view.Menu;
 11 import android.view.MenuItem;
 12 import android.view.View;
 13 import android.view.View.OnClickListener;
 14 import android.view.ViewGroup;
 15 import android.widget.*;
 16 import android.widget.AdapterView.OnItemSelectedListener;
 17 import android.os.Build;
 18 
 19 public class MainActivity extends ActionBarActivity {
 20     
 21     private Button b1,b2,b3;
 22     private LinearLayout l1;
 23     private Spinner sp1;
 24     private TextView[]T=new TextView[9];
 25     private EditText[]E=new EditText[6];
 26     int i,k,n;
 27     long begintime,endtime,time;
 28     int[]a=new int[5];
 29     int[]b=new int[5];
 30     int[]c=new int[5];
 31     int[]d=new int[5];
 32     float[]answer=new float[5];
 33     List<String>list=new ArrayList<String>();
 34 
 35     @Override
 36     protected void onCreate(Bundle savedInstanceState)
 37   {
 38         super.onCreate(savedInstanceState);
 39         setContentView(R.layout.activity_main);
 40         
 41         b1=(Button)findViewById(R.id.button1);
 42         b2=(Button)findViewById(R.id.button2);
 43         b3=(Button)findViewById(R.id.button3);
 44         l1=(LinearLayout)findViewById(R.id.container);
 45         T[0]=(TextView)findViewById(R.id.textView4);
 46         T[1]=(TextView)findViewById(R.id.textView6);
 47         T[2]=(TextView)findViewById(R.id.textView9);
 48         T[3]=(TextView)findViewById(R.id.textView12);
 49         T[4]=(TextView)findViewById(R.id.textView15);
 50         T[5]=(TextView)findViewById(R.id.textView18);
 51         T[6]=(TextView)findViewById(R.id.textView20);
 52         T[7]=(TextView)findViewById(R.id.textView22);
 53         T[8]=(TextView)findViewById(R.id.textView24);
 54         E[0]=(EditText)findViewById(R.id.editText2);
 55         E[1]=(EditText)findViewById(R.id.editText3);
 56         E[2]=(EditText)findViewById(R.id.editText4);
 57         E[3]=(EditText)findViewById(R.id.editText5);
 58         E[4]=(EditText)findViewById(R.id.editText6);
 59         E[5]=(EditText)findViewById(R.id.editText1);
 60         sp1=(Spinner)findViewById(R.id.spinner1);
 61         list.add("默认");
 62         list.add("勤劳的宿舍长");
 63         list.add("飞奔的宿舍长");
 64         list.add("下海的宿舍长");
 65         list.add("发情期的宿舍长");
 66         list.add("遭袭的宿舍长");
 67         list.add("变身后的宿舍长");
 68         ArrayAdapter<String> adapter;
 69         adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
 70         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
 71         sp1.setAdapter(adapter);
 72         
 73         b1.setOnClickListener(new OnClickListener()
 74     {
 75             
 76             @Override
 77         public void onClick(View v)
 78         {
 79                 // TODO Auto-generated method stub
 80             try
 81             {
 82                 for(k=0;k<5;k++)
 83                 {
 84                     T[k].setText(null);
 85                 }
 86                 for(k=0;k<5;k++)
 87                 {
 88                     E[k].setText(null);
 89                 }
 90                 T[5].setText(null);
 91                 T[6].setText(null);
 92                 T[7].setText(null);
 93                 T[8].setText(null);
 94                 i=0;
 95                 if(E[5].getText().length()==0)
 96                 {
 97                     Toast.makeText(MainActivity.this, "请输入题数", 1).show();
 98                 }
 99                 else if(Integer.parseInt(E[5].getText()+"")>5 || Integer.parseInt(E[5].getText()+"")<1)
100                 {
101                     Toast.makeText(MainActivity.this, "题数最多不超过5题,不少于1题", 1).show();
102                 }
103                 else
104                 {
105                     i=Integer.parseInt(E[5].getText()+"");
106                     
107                     for(k=0;k<i;k++)
108                     {
109                         a[k]=(int)(Math.random()*201-100);
110                         b[k]=(int)(Math.random()*201-100);
111                         c[k]=(int)(Math.random()*5);
112                         d[k]=(int)(Math.random()*12+1);
113                         Core core=new Core(a[k],b[k],c[k],d[k]);
114                         answer[k]=core.calc();
115                         T[k].setText(core.toString());
116                         
117                     }
118                     begintime = System.currentTimeMillis();
119                 }
120             }catch(NumberFormatException u)
121             {
122                 Toast.makeText(MainActivity.this, "请输入正确的数字", 1).show();
123             }
124         }
125     });
126         
127         b2.setOnClickListener(new OnClickListener() {
128             
129             @Override
130             public void onClick(View v) {
131                 // TODO Auto-generated method stub
132                 if(i==0)
133                 {
134                     Toast.makeText(MainActivity.this, "请先开始答题", 1).show();
135                 }
136                 else
137                 {
138                     endtime = System.currentTimeMillis();
139                     time=(endtime-begintime)/1000;
140                     n=0;
141                     for(k=0;k<i;k++)
142                     {
143                         if(E[k].getText().length()==0)
144                         {
145                             continue;
146                         }
147                         try
148                         {
149                             if(answer[k]==Float.parseFloat(E[k].getText()+""))
150                             {
151                                 n++;
152                             }
153                         }catch(NumberFormatException u)
154                         {
155                             Toast.makeText(MainActivity.this, "请输入正确的数字", 1).show();
156                         }
157                         
158                     }
159                     T[5].setText(n+"道题");
160                     T[6].setText((i-n)+"道题");
161                     T[7].setText(time+"秒");
162                     T[8].setText("");
163                     for(k=0;k<i;k++)
164                     {
165                         T[8].setText(T[8].getText()+""+answer[k]+";");
166                     }
167                     i=0;
168                 }
169             }
170         });
171         
172         b3.setOnClickListener(new OnClickListener() {
173             
174             @Override
175             public void onClick(View v) {
176                 // TODO Auto-generated method stub
177                 for(k=0;k<5;k++)
178                 {
179                     T[k].setText(null);
180                 }
181                 for(k=0;k<5;k++)
182                 {
183                     E[k].setText(null);
184                 }
185                 E[5].setText(null);
186                 T[5].setText(null);
187                 T[6].setText(null);
188                 T[7].setText(null);
189                 T[8].setText(null);
190                 i=0;
191             }
192         });
193         
194         sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
195             @Override
196             public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
197             {
198                 if(pos==0)
199                     l1.setBackgroundResource(R.drawable.ss2);
200                 if(pos==1)
201                     l1.setBackgroundResource(R.drawable.ss3);
202                 if(pos==2)
203                     l1.setBackgroundResource(R.drawable.ss4);
204                 if(pos==3)
205                     l1.setBackgroundResource(R.drawable.ss5);
206                 if(pos==4)
207                     l1.setBackgroundResource(R.drawable.ss6);
208                 if(pos==5)
209                     l1.setBackgroundResource(R.drawable.ss7);
210                 if(pos==6)
211                     l1.setBackgroundResource(R.drawable.ss8);
212             }
213             @Override
214             public void onNothingSelected(AdapterView<?> parent)
215             {
216                 // Another interface callback
217             }
218         });
219   }
220 
221 
222     @Override
223     public boolean onCreateOptionsMenu(Menu menu) {
224         
225         // Inflate the menu; this adds items to the action bar if it is present.
226         getMenuInflater().inflate(R.menu.main, menu);
227         return true;
228     }
229 
230     @Override
231     public boolean onOptionsItemSelected(MenuItem item) {
232         // Handle action bar item clicks here. The action bar will
233         // automatically handle clicks on the Home/Up button, so long
234         // as you specify a parent activity in AndroidManifest.xml.
235         int id = item.getItemId();
236         if (id == R.id.action_settings) {
237             return true;
238         }
239         return super.onOptionsItemSelected(item);
240     }
241 
242     /**
243      * A placeholder fragment containing a simple view.
244      */
245     public static class PlaceholderFragment extends Fragment {
246 
247         public PlaceholderFragment() {
248         }
249 
250         @Override
251         public View onCreateView(LayoutInflater inflater, ViewGroup container,
252                 Bundle savedInstanceState) {
253             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
254             return rootView;
255         }
256     }
257 
258 }
main类
 1 package com.example.zs;
 2 
 3 public class Core {
 4     int a;
 5     int b;
 6     int c;
 7     int d;
 8     public Core(int a,int b,int c,int d)
 9     {
10         this.a=a;
11         this.b=b;
12         this.c=c;
13         this.d=d;
14     }
15     public float calc()
16     {
17         if(c==0)
18         {
19             return a+b;
20         }
21         else if(c==1)
22         {
23             return a-b;
24         }
25         else if(c==2)
26         {
27             return a*b;
28         }
29         else if(c==3)
30         {
31             if(b==0)
32             {
33                 b++;
34             }
35             return (float)Math.round(((float)a/b)*100)/100;
36         }
37         else
38         {
39             return jiecheng(d);
40         }
41     }
42     public String toString()
43     {
44         String A=new String();
45         String B=new String();
46         if(a<0)
47         {
48             A="("+a+")";
49         }
50         else
51         {
52             A=a+"";
53         }
54         if(b<0)
55         {
56             B="("+b+")";
57         }
58         else
59         {
60             B=b+"";
61         }
62         if(c==0)
63         {
64             return A+"+"+B;
65         }
66         else if(c==1)
67         {
68             return A+"-"+B;
69         }
70         else if(c==2)
71         {
72             return A+"*"+B;
73         }
74         else if(c==3)
75         {
76             return A+"/"+B;
77         }
78         else
79         {
80             return d+"!";
81         }
82     }
83 
84     public float jiecheng(int x)
85     {
86         if(x==0 || x==1)
87         {
88             return 1;
89         }
90         else
91         {
92             return x*jiecheng(x-1);
93         }
94     }
95 }
计算核心类
  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     xmlns:tools="http://schemas.android.com/tools"
  3     android:id="@+id/container"
  4     android:layout_width="match_parent"
  5     android:layout_height="match_parent"
  6     android:orientation="vertical"
  7     android:background="@drawable/ss2" >
  8 
  9     <LinearLayout
 10         android:layout_width="match_parent"
 11         android:layout_height="wrap_content"
 12         android:gravity="center" >
 13 
 14         <TextView
 15             android:id="@+id/textView1"
 16             android:layout_width="wrap_content"
 17             android:layout_height="wrap_content"
 18             android:text="欢迎您,尊敬的用户"
 19             android:textSize="20sp" />
 20 
 21     </LinearLayout>
 22 
 23     <LinearLayout
 24         android:layout_width="match_parent"
 25         android:layout_height="wrap_content"
 26         android:gravity="center" >
 27 
 28         <TextView
 29             android:id="@+id/textView2"
 30             android:layout_width="wrap_content"
 31             android:layout_height="wrap_content"
 32             android:text="请输入题数:"
 33             android:textSize="20sp" />
 34 
 35         <EditText
 36             android:id="@+id/editText1"
 37             android:hint="不超过5题"
 38             android:layout_width="100dp"
 39             android:layout_height="wrap_content"
 40             android:textSize="15sp"
 41              >
 42 
 43             <requestFocus />
 44         </EditText>
 45 
 46         <Button
 47             android:id="@+id/button1"
 48             android:layout_width="wrap_content"
 49             android:layout_height="34dp"
 50             android:text="开始答题"
 51             android:textSize="15sp"
 52             android:textColor="#ee7755" />
 53 
 54     </LinearLayout>
 55 
 56     <LinearLayout
 57         android:layout_width="match_parent"
 58         android:layout_height="wrap_content" >
 59 
 60         <TextView
 61             android:id="@+id/textView3"
 62             android:layout_width="wrap_content"
 63             android:layout_height="wrap_content"
 64             android:text="第一题:"
 65             android:textSize="20sp" />
 66 
 67         <TextView
 68             android:id="@+id/textView4"
 69             android:layout_width="110dp"
 70             android:layout_height="wrap_content"
 71             android:textSize="20sp" />
 72 
 73         <TextView
 74             android:id="@+id/textView5"
 75             android:layout_width="wrap_content"
 76             android:layout_height="wrap_content"
 77             android:text=" = "
 78             android:textSize="25sp" />
 79 
 80         <EditText
 81             android:id="@+id/editText2"
 82             android:layout_width="70dp"
 83             android:layout_height="wrap_content"
 84             android:textSize="15sp" />
 85 
 86     </LinearLayout>
 87     
 88     <LinearLayout
 89         android:layout_width="match_parent"
 90         android:layout_height="wrap_content" >
 91 
 92         <TextView
 93             android:id="@+id/textView25"
 94             android:layout_width="wrap_content"
 95             android:layout_height="wrap_content"
 96             android:text="第二题:"
 97             android:textSize="20sp" />
 98 
 99         <TextView
100             android:id="@+id/textView6"
101             android:layout_width="110dp"
102             android:layout_height="wrap_content"
103             android:textSize="20sp" />
104 
105         <TextView
106             android:id="@+id/textView7"
107             android:layout_width="wrap_content"
108             android:layout_height="wrap_content"
109             android:text=" = "
110             android:textSize="25sp" />
111 
112         <EditText
113             android:id="@+id/editText3"
114             android:layout_width="70dp"
115             android:layout_height="wrap_content"
116             android:textSize="15sp" />
117 
118     </LinearLayout>
119     
120     <LinearLayout
121         android:layout_width="match_parent"
122         android:layout_height="wrap_content" >
123 
124         <TextView
125             android:id="@+id/textView8"
126             android:layout_width="wrap_content"
127             android:layout_height="wrap_content"
128             android:text="第三题:"
129             android:textSize="20sp" />
130 
131         <TextView
132             android:id="@+id/textView9"
133             android:layout_width="110dp"
134             android:layout_height="wrap_content"
135             android:textSize="20sp" />
136 
137         <TextView
138             android:id="@+id/textView10"
139             android:layout_width="wrap_content"
140             android:layout_height="wrap_content"
141             android:text=" = "
142             android:textSize="25sp" />
143 
144         <EditText
145             android:id="@+id/editText4"
146             android:layout_width="70dp"
147             android:layout_height="wrap_content"
148             android:textSize="15sp" />
149 
150     </LinearLayout>
151     
152     <LinearLayout
153         android:layout_width="match_parent"
154         android:layout_height="wrap_content" >
155 
156         <TextView
157             android:id="@+id/textView11"
158             android:layout_width="wrap_content"
159             android:layout_height="wrap_content"
160             android:text="第四题:"
161             android:textSize="20sp" />
162 
163         <TextView
164             android:id="@+id/textView12"
165             android:layout_width="110dp"
166             android:layout_height="wrap_content"
167             android:textSize="20sp" />
168 
169         <TextView
170             android:id="@+id/textView13"
171             android:layout_width="wrap_content"
172             android:layout_height="wrap_content"
173             android:text=" = "
174             android:textSize="25sp" />
175 
176         <EditText
177             android:id="@+id/editText5"
178             android:layout_width="70dp"
179             android:layout_height="wrap_content"
180             android:textSize="15sp" />
181 
182     </LinearLayout>
183     
184     <LinearLayout
185         android:layout_width="match_parent"
186         android:layout_height="wrap_content" >
187 
188         <TextView
189             android:id="@+id/textView14"
190             android:layout_width="wrap_content"
191             android:layout_height="wrap_content"
192             android:text="第五题:"
193             android:textSize="20sp" />
194 
195         <TextView
196             android:id="@+id/textView15"
197             android:layout_width="110dp"
198             android:layout_height="wrap_content"
199             android:textSize="20sp" />
200 
201         <TextView
202             android:id="@+id/textView16"
203             android:layout_width="wrap_content"
204             android:layout_height="wrap_content"
205             android:text=" = "
206             android:textSize="25sp" />
207 
208         <EditText
209             android:id="@+id/editText6"
210             android:layout_width="70dp"
211             android:layout_height="wrap_content"
212             android:textSize="15sp" />
213 
214     </LinearLayout>
215 
216     <LinearLayout
217         android:layout_width="match_parent"
218         android:layout_height="wrap_content"
219         android:gravity="right" >
220 
221         <TextView
222             android:id="@+id/textView27"
223             android:layout_width="wrap_content"
224             android:layout_height="wrap_content"
225             android:text="(除法运算结果保留两位小数)"
226             android:textSize="15sp" />
227 
228         <Button
229             android:id="@+id/button2"
230             android:layout_width="wrap_content"
231             android:layout_height="34dp"
232             android:text="提交"
233             android:textSize="15sp" />
234 
235     </LinearLayout>
236 
237     <LinearLayout
238         android:layout_width="match_parent"
239         android:layout_height="wrap_content" >
240 
241         <TextView
242             android:id="@+id/textView17"
243             android:layout_width="wrap_content"
244             android:layout_height="wrap_content"
245             android:text="你答对了:"
246             android:textSize="20sp" />
247 
248         <TextView
249             android:id="@+id/textView18"
250             android:layout_width="100dp"
251             android:layout_height="wrap_content"
252             android:textSize="20sp" />
253 
254     </LinearLayout>
255     
256      <LinearLayout
257         android:layout_width="match_parent"
258         android:layout_height="wrap_content" >
259 
260         <TextView
261             android:id="@+id/textView19"
262             android:layout_width="wrap_content"
263             android:layout_height="wrap_content"
264             android:text="你答错了:"
265             android:textSize="20sp" />
266 
267         <TextView
268             android:id="@+id/textView20"
269             android:layout_width="100dp"
270             android:layout_height="wrap_content"
271             android:textSize="20sp" />
272 
273     </LinearLayout>
274     
275      <LinearLayout
276         android:layout_width="match_parent"
277         android:layout_height="wrap_content" >
278 
279         <TextView
280             android:id="@+id/textView21"
281             android:layout_width="wrap_content"
282             android:layout_height="wrap_content"
283             android:text="用时:"
284             android:textSize="20sp" />
285 
286         <TextView
287             android:id="@+id/textView22"
288             android:layout_width="100dp"
289             android:layout_height="wrap_content"
290             android:textSize="20sp" />
291 
292     </LinearLayout>
293     
294      <LinearLayout
295         android:layout_width="match_parent"
296         android:layout_height="wrap_content" >
297 
298         <TextView
299             android:id="@+id/textView23"
300             android:layout_width="wrap_content"
301             android:layout_height="wrap_content"
302             android:text="参考答案:"
303             android:textSize="20sp" />
304 
305         <TextView
306             android:id="@+id/textView24"
307             android:layout_width="wrap_content"
308             android:layout_height="wrap_content"
309             android:textSize="20sp" />
310 
311     </LinearLayout>
312 
313      <Button
314          android:id="@+id/button3"
315          android:layout_width="wrap_content"
316          android:layout_height="34dp"
317          android:text="重置"
318          android:textSize="15sp"
319          android:layout_gravity="right" />
320 
321      <LinearLayout
322          android:layout_width="wrap_content"
323          android:layout_height="wrap_content" >
324 
325          <TextView
326              android:id="@+id/textView26"
327              android:layout_width="wrap_content"
328              android:layout_height="wrap_content"
329              android:text="选择皮肤:"
330              android:textSize="20sp" />
331 
332          <Spinner
333              android:id="@+id/spinner1"
334              android:layout_width="200dp"
335              android:layout_height="34dp"
336              android:layout_weight="1"
337              android:prompt="@string/aaa1"
338              android:spinnerMode="dialog" />
339 
340      </LinearLayout>
341 
342 </LinearLayout>
界面
1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3 
4     <string name="app_name">ZS</string>
5     <string name="hello_world">Hello world!</string>
6     <string name="action_settings">Settings</string>
7     <string name="aaa1">选择皮肤</string>
8 
9 </resources>
strings.xml

依照惯例先上代码,再上运行截图:

原文地址:https://www.cnblogs.com/zhanshenlianmeng/p/4597663.html