目录
1.前言
2.设计与分析
3.踩坑心得
4.改进建议
5.总结
1.前言
题目集1:(主要初次了解Java的一些用法)
1、计算年利率
2、身体质量指数测算
3、九九乘法表(双循环)
4、快递运费
5、去掉重复字符
6、统计一个子串在整串中出现的次数
7、有重复数据
8、从一个字符串中移除包含在另一个字符串中的字符
9、Prime Numbers
10、GPS数据处理
11、求定积分
12、列出最简真分数序列*
题目集2 (过渡阶段)
1、长度质量计算单位换算
2、奇数求和
3、房产税计算2022
4、游戏角色选择
5、学号识别
6、巴比伦法求平方根近似值
7、二进制数值提取
8、判断三角形类型
9、求下一天
题目集3 (使用类的主要题目集)
1、创建圆类型
2、创建账户类Account
3、定义日期类
4、日期类设计
写第一次题目集时,第一次使用java,经过看书与看慕课,只懂就是这样写的格式,不懂为什么这样。后来慢慢懂了java中输入与输出是一个类,直接用即可。第一次题目较简单,了解一些Java知识加上C语言的用法,第一次的作业很容易完成,只是时间问题。主要是第10题,写GPS数据处理这道题跟大家讨论了一段时间后,才慢慢摸索出解决方案。
第二次题目集,主要是了解Java中选择结构与循环结构的用法,穿插了字符用法。总体是不算太难,主要一个问题就是第8题,判断两个三角形类型问题,一开是用勾股定理判断,没有解决,这问题我下面会讲。第9题为第3次题目集做了铺垫,相当于一次题目迭代吧。
第三次题目集,全部使用类,难度直线飙升,毕竟对类这个含义不太懂。1.2题相对3.4题算比较简单的了。但第4题我没有完成,只玩成了部分功能。最后跟同学讨论了这道题的逻辑,发现自己算法是非常复杂的。这也大大证明了我们需要大大的互相交流,得出有效方案。
2.设计与分析
判断三角形类型
这道题大多数人以为是简单题,就不断用if即可,但是最后直角或等腰直角这些测试点过不去,我也碰到这个问题,当天晚上不断搜资料,第二天老师说变量存储是有误差的吧,不是一样的。最后算差值小于0.000001即可。
当时是真的没有人问出这个问题,我就是直接蛮干,最后也没得出结果。有时真的一些别人的见解真的比自己努力更有效。
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); double a,b,c,d; a= in.nextDouble(); b= in.nextDouble(); c= in.nextDouble(); if(a > b){ d = a; a = b; b = d; } if(a > c){ d = a; a = c; c = d; } if(b > c){ d = b; b = c; c = d; } if(a<1||c>200) { System.out.printf("Wrong Format"); } else { if(a+b<=c) System.out.printf("Not a triangle"); else if(a==b&&b==c) System.out.printf("Equilateral triangle"); else if(a==b||b==c) { if(a*a+b*b-c*c<0.000001) System.out.printf("Isosceles right-angled triangle"); else System.out.printf("Isosceles triangle"); } else if(a*a+b*b-c*c<0.000001) { System.out.printf("Right-angled triangle"); } else System.out.printf("General triangle"); } } }
说说第9题,我也是有几个测试点没过。
代码如下
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int year,month,day; year= in.nextInt();month= in.nextInt();day= in.nextInt(); nextDate( year, month, day); } public static boolean isLeapYear(int year) { boolean isLeapYear=((year%4==0)&&(year%100!=0)||year%400==0); return isLeapYear; } public static boolean checkInputValidity(int year,int month,int day) { int []a=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear( year)) { a[2]=29; } boolean checkInputValidity=(year>=1820&&year<=2020&&month>=1&&month<=12&&day>0&&day<=a[month]); { return checkInputValidity; } } public static void nextDate(int year,int month,int day) { int d[]=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; if((isLeapYear( year))) { int a,b,c; a=0;b=0;c=0; d[2]=29; if(checkInputValidity( year, month, day)) { if(month==12) { if(day==31) { a=year+1; b=1; c=1; } if(day>0&&day<d[month]) { a=year; b=month; c=day+1; } } if(month<12) { if(day==d[month]) { a=year; b=month+1; c=1; } if(day>0&&day<d[month]) { a=year; b=month; c=day+1; } } System.out.println("Next date is:"+a +"-" +b + "-"+ c); } else System.out.println("Wrong Format"); } if(year<1820||year>2020) { System.out.println("Wrong Format"); } } }
到现在我也不知道这些测试点全部是哪些,我只知道我除去闰年的输入,我是输出null的,因为我的逻辑是有问题的,最后我想加上去,就是一个else结构即可,但是最后报代码太长了,出错。这次我第一次了解会有代码过长问题。所以一开始就要规划自己思路与有效方案,我们班上有一人只用50行就玩成了这题,看完她的代码,也许这就是差距。
第3次题库集的第一题看看视频,照着模板写就行,主要了解了私有属性。第二题主要是传参的使用,及巩固私有属性的方法输入与输出。不过多解释。主要解释剩下2道题。
定义日期类
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); Date date =new Date(); int year; int month; int day; year= in.nextInt(); month= in.nextInt(); day= in.nextInt(); date.setYear(year); date.setMonth(month); date.setDay(day); if(date.checkInputValidity()){ date.getNextDate() ; System.out.println("Next day is:"+date.getYear() +"-" +date.getMonth() + "-"+ date.getDay()); } else System.out.println("Date Format is Wrong"); } } class Date{ private int year; private int month; private int day; int d[]=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31}; public Date(){ } public Date(int year,int month,int day){ this.year=year; this.month=month; this.day=day; } public void setYear(int year){ this .year=year; } public void setMonth(int month){ this .month=month; } public void setDay(int day){ this .day=day; } public int getYear(){ return this.year; } public int getMonth(){ return this.month; } public int getDay(){ return this.day; } public boolean isLeapYear(int year){ if((year%4==0)&&(year%100!=0)||year%400==0) return true; else return false; } public boolean checkInputValidity(){ if(this.year<1900||this.year>2000) return false; if(this.month<1||this.month>12) return false; if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10||this.month==12) { if(this.day>=1&&this.day<=31) return true; else return false; } else if(this.month==4||this.month==6||this.month==9||this.month==11) { if(this.day>=1&&this.day<=30) return true; else return false; } else { if(isLeapYear(this.year) ) { if(this.day>=1&&this.day<=29) return true; else return false; } else { if(this.day>=1&&this.day<=28) return true; else return false; } } } public void getNextDate() { if(this.month==12) { if(this.day==d[this.month]) { this.year=this.year+1;this.month=1;this.day=1; } else this.day=this.day+1; return; } if(this.month==2) { if(isLeapYear(this .year)) { d[2]=29; if(this.day==d[this.month]) { this.day=1; this.month=this.month+1; } else this.day=this.day+1; } else { if(this.day==d[this.month]) { this.day=1; this.month=this.month+1; } else this.day=this.day+1; } return; } if(this.month==1||this.month==3||this.month==5||this.month==7||this.month==8||this.month==10) { if(this.day==d[this.month]) { this.year=this.year;this.month=this.month+1;this.day=1; } else this.day=this.day+1; return; } if(this.month==4||this.month==6||this.month==9||this.month==11) { if(this.day==d[this.month]) { this.year=this.year;this.month=this.month+1;this.day=1; } else this.day=this.day+1; return; } } }
这道题我感觉我判断月份时,用的方法太死板了,一开始我用的是用一个数组来保存月份数字,但是报错了,然后改成这种死板代码。个人觉得不是很好。
这道题算简单的了,只要注意闰年与2月,月底。一个一个攻破即可
第4题:日期类设计(这到题没写全,只能这样写了)
这里我就是多了算前n天与后n天方法,具体一些算法我也加以改进了,代码如下
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year = 0; int month = 0; int day = 0; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } m = input.nextInt(); if (m < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); System.out.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n = 0; year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); DateUtil date = new DateUtil(year, month, day); if (!date.checkInputValidity()) { System.out.println("Wrong Format"); System.exit(0); } n = input.nextInt(); if (n < 0) { System.out.println("Wrong Format"); System.exit(0); } System.out.print( date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); System.out.println(date.getPreviousNDays(n).showDate()); } else{ System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil{ private int year; private int month; private int day; public DateUtil(int year,int month,int day){ this.year=year; this.month=month; this.day=day; } public void setYear(int year){ this .year=year; } public void setMonth(int month){ this .month=month; } public void setDay(int day){ this .day=day; } public int getYear(){ return year; } public int getMonth(){ return month; } public int getDay(){ return day; } public boolean isLeapYear(int year){ if((year%4==0)&&(year%100!=0)||year%400==0) return true; else return false; } public boolean checkInputValidity() { int []a=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear( year)) { a[2]=29; } if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>0&&day<=a[month]) { return true; } else return false; } public DateUtil getNextNDays(int n){ int year1=year; int month1=month; int day1=day; int []d=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear(year1)) { d[2]=29; day1=day1+n; while(day1>d[month1]) { if(isLeapYear(year1)) { d[2]=29; } else d[2]=28; day1=day1-d[month1]; month1++; if(month1==13) { year1++; month1=1; } } } else{ d[2]=28; day1=day1+n; while(day1>d[month1]) { if(isLeapYear(year1)) { d[2]=29; } else d[2]=28; day1=day1-d[month1]; month1++; if(month1==13) { year1++; month1=1; } } } DateUtil newData = new DateUtil(year1,month1,day1); return newData; } public DateUtil getPreviousNDays(int n){ int year1=year; int month1=month; int day1=day; int []d=new int []{0,31,28,31,30,31,30,31,31,30,31,30,31}; if(isLeapYear( year1)) { d[2]=29; day1=day1-n; while(day1<0) { if(isLeapYear(year1)) { d[2]=29; } else d[2]=28; month1--; if(month1==0) { year1--; month1=12; } day1=day1+d[month1]; } } else{ d[2]=28; day1=day1-n; while(day1<0) { if(isLeapYear(year1)) { d[2]=29; } else d[2]=28; month1--; if(month1==0) { year1--; month1=12; } day1=day1+d[month1]; } } DateUtil newData = new DateUtil(year1,month1,day1); return newData; } public String showDate(){ String a=year+"-"+month+"-"+day; return a; } }
但算后n天有一个测试点没过
这个问题我向一个同学请教了一下,具体原因应该是在getNextNDays方法里最好不要用到累加,用了会导致最终结果大于int的范围,这也算一个重未思考过的问题(问题代码参考如下)
public DateUtil getNextNDays (int n){ DateUtil Date = new DateUtil(this.year, this.month, this.day); for (; n > 0; ) { if (isLeapYear(Date.getYear())) { mon_maxnum[2] = 29; } else { mon_maxnum[2] = 28; } if (Date.getDay() < mon_maxnum[Date.getMonth()]) { Date.setDay(Date.getDay() + 1); n--; } else { if (Date.getMonth() < 12) { Date.setMonth(Date.getMonth() + 1); Date.setDay(1); n--; } else { Date.setMonth(1); Date.setDay(1); Date.setYear(Date.getYear() + 1); n--; } } } return Date; }
接下来是这道题正确解法
这里多了3个方法,compareDates,equalTwoDates,getDaysofDates。前两个方法用于判断输入的数据两者大小或是否相等,然后发送信息给getDaysofDates,这个方法用于算差值。结果最后返回即可(代码就不太好放上去了,大家按这个思路敲,慢慢试)
3.踩坑心得
这三次题库集主要文题就是三角形类型(变量之间比较问题),算天的问题(方法的传参,类的构造,思路问题(代码过长)),有时候可上网查查一些方法(直接调用,绝对比自己写代码快,例如Array,String,这些非常好用)
这里用了Array.sorb方法,直接排序,这不比c语言中冒泡排序简单。
这里我使用to.CharArray,直接把字符串转化数组问题,对数组进行操作,简单快速。
这些手段都需要慢慢积累,使用起来才能得心应手。
对于第2次题目集,第9题我根本就没考虑代码过长这个问题,这也提醒我写代码要简洁。
对于题目集第4题就暴露了好多问题,我就更本没有看懂那给出的三个方法,后问了同学,才勉强懂了它们各自作用,但是当时没懂各自传参以及this到底是啥用途,然后直接放弃了这道题,后面去恶补了这个知识空缺。
4.改进建议
1.不断与同学讨论,能得出比自己更有效方案。
2.需要不断压出自己的时间来了解一些有用的方法(这个应该慢慢积累)
3.完善自己的代码,不要过了测试点就以为可以了,必须注意自己写代码格式,简洁程度(未来会越来越难,一开始就是复杂代码,可能后面不好操作
5.总结
这是我第一次写博客,这次写博客,给了我一个提醒,我之前写的代码真的没眼看。有许多提升手段,这是我最大收获,学习Java要看一些课,查资料(csdn),也必须看书(了解类,方法,属性,对象,这是最直接方式,快速了解Java中的一些构成)。我学习Java才刚刚开始,且必须学好。对于实验,上课,现在没有太大建议,但是希望老师能提供一些类(特别好用的那种),只要肯花时间学习Java,应该会有收获。
标签:总结性,题目,int,31,month,Blog,public,year,day From: https://www.cnblogs.com/fengyufeng/p/17259422.html