用安卓实现计算“三天打鱼两天晒网”问题
来源:互联网 发布:淘宝买枪的暗语 编辑:程序博客网 时间:2024/04/30 21:54
题目:中国有句俗语叫“三天打鱼两天晒网”,某人从2010年1月1日其开始“三天打鱼两天晒网”,问这个人在以后的某一天是“打渔”还是“晒网”。
分析:这个题目的整体思路就是求出两个时间中间间隔的时间然后求余,在3以内就是打渔,3以后就是晒网。算法方面没有什么难度,但是这个程序想要做得比较完美的话应该有个输入数据检验和支持读入文件来进行测试的功能。
恰巧由于最近在学习安卓,算个刚入门的新手,所以就尝试者做一个app来实现这个程序,尽管可能有点多此一举,但是多练习对代码的驾驭能力应该是有不少的提升的。
第一步:
需求分析
1:可以输入年月日来显示这天是“打渔”还是“晒网”。
2:可以读取文件来显示结果。
3:输入的数据必须有验证过程,对于不正确的数据给出提示。
第二步:
分析需要的技术
1:为了防止用户自己输入的错误,这里引用了一个开源项目,来显示一个日历让用户通过点击来输入。
2:既然要从文件读取数据来计算,对文件的操作和对文件管理器的访问必不可少。
3:对于两个日期之间天数的计算方法。
第三步:
有了上面两个的分析,应该就没什么需要思考的了,下面开始开心的写代码。
主界面布局
布局整体采用LinearLayout,上部分为了实现Material风格的app效果,所以用了Toolbar,上面有两个按钮,一个是点击弹出日历来提供输入,另一个一看就是清空显示区。然后中间部分是整个TextView来显示结果,右下方有一个FloatingActionButton,点击可以打开文件管理器来读入测试文件。
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="@color/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" /> <TextView android:id="@+id/result" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:padding="@dimen/activity_horizontal_margin" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/activity_horizontal_margin" android:src="@drawable/add" /></LinearLayout>
有了布局剩下就是添加逻辑了,逻辑方面没什么好说的,放几个程序中比较重要的算法。
1:算两个日期之间的天数并且显示结果
/** * 计算该日期并显示结果 */ private void calculate(int year, int month, int dayOfMonth) { if (year < 2010 || month > 12 || dayOfMonth > 31) { textView.append(year + "年" + month + "月" + dayOfMonth + "日" + ":该数据有误\n"); } else { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); try { long start = sdf.parse("2010-1-1").getTime(); Log.e(TAG, "到2010-1-1经历的毫秒数 " + start); long end = sdf.parse(year + "-" + month + "-" + dayOfMonth).getTime(); Log.e(TAG, "到" + year + "-" + month + "-" + dayOfMonth + "经历的毫秒数 " + end); //天数=毫秒数/每天的毫秒数 int day = (int) ((end - start) / (1000 * 24 * 60 * 60)); Log.e(TAG, "相隔的天数 " + day); if ((day % 5) <= 3 && (day % 5) != 0) { textView.append(year + "年" + month + "月" + dayOfMonth + "日" + ":这天打渔\n"); } else { textView.append(year + "年" + month + "月" + dayOfMonth + "日" + ":这天晒网\n"); } } catch (ParseException e) { textView.append(year + "年" + month + "月" + dayOfMonth + "日" + ":该数据有误\n"); } } }
2:解析文件数据
/** * 解析文件 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { if (requestCode == 1) { //得到文件URI Uri uri = data.getData(); File file = new File(uri.getPath());// Log.e(TAG, "onActivityResult: " + file.getPath()); FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br = null; try { fis = new FileInputStream(file); isr = new InputStreamReader(fis); br = new BufferedReader(isr); String line = ""; while ((line = br.readLine())!= null) { Log.e(TAG, "文件中的日期: " + line); if (line.matches(regex)) { String[] s = line.split("-"); //如果匹配成功就进行计算 calculate(Integer.parseInt(s[0]), Integer.parseInt(s[1]) , Integer.parseInt(s[2])); } else { textView.append(line + "该数据有误\n"); } } } catch (Exception e) { Log.e(TAG, "文件读取异常: " + e.toString()); } finally { try { if (fis != null) { fis.close(); } if (isr != null) { isr.close(); } if (br != null) { br.close(); } } catch (Exception e) { Log.e(TAG, "关闭文件异常: " + e.toString()); } } } } }最后的效果就是这样子
0 0
- 用安卓实现计算“三天打鱼两天晒网”问题
- 三天打鱼两天晒网问题
- “三天打鱼两天晒网”问题
- 三天打鱼两天晒网问题
- 三天打鱼 两天晒网 问题
- 三天打鱼两天晒网问题
- 三天打鱼两天晒网问题C++版本
- 关于“三天打鱼两天晒网”的问题
- 三天打鱼两天晒网
- 三天打鱼两天晒网
- 三天打鱼两天晒网
- 三天打鱼,两天晒网
- 三天打鱼,两天晒网
- 三天打鱼两天晒网
- 三天打鱼,两天晒网
- 三天打鱼两天晒网
- 三天打鱼两天晒网
- 三天打鱼两天晒网
- 数据结构基础篇(4)--树
- bootstrap bagging boosting adboost oob
- 洛谷 2330 最小生成树
- c# 添加引用DLL。CS
- MAVEN常用命令
- 用安卓实现计算“三天打鱼两天晒网”问题
- 1715 计算面积
- Weak 访问数据库
- Maven实战研读(一)内存的修改
- 排序
- 感知机--模型与策略
- ROS中给turtlebot加两个传感器:kinect+hokuyo,进行gazebo仿真(一)
- request.getsession()和ActionContext.getContext().getSession()的区别
- retrofit sendto failed: ECONNRESET (Connection reset by peer)【android客户端Post请求参数长度限制(参数过大)】解决办法