作业题量相较于上学期的C语言课程减少了很多,但是花费的时间更多了。
对比上个学期的C语言作业,本学期的java难度提高了很多,无论是在程序的设计上面还是在边界的判断方面都有很大的提升,尤其是对于程序的边界判断有很大的难度提升 。
public class Main { public static void main(String[] args) { try (java.util.Scanner input = new java.util.Scanner(System.in)) { String arr; int count = 0; arr = input.nextLine(); int len = arr.length(); boolean flag = false;// 判断是否为无效数据 for (int i = 0; i < arr.length(); i++) { if (arr.charAt(i) == '0') { flag = true; break; } } if (len < 11) { System.out.println("null data"); } else { if (!flag) { System.out.println("null data"); } else { int i = 0; for (; i < arr.length(); i++) { int cnt = 0; if (arr.charAt(i) == '0') { i++; boolean judge1 = true; boolean judge2 = true; String ans = ""; for (; i < arr.length() && ans.length() != 8; i++) { if (arr.charAt(i) == '1') { cnt++; } ans = ans + arr.charAt(i); } // 检测边界问题 正常跳出时 已经为校验为 if (i >= arr.length()) { break; } if (cnt % 2 == 1) { if (arr.charAt(i) != '0') { judge1 = false; } } else { if (arr.charAt(i) != '1') { judge1 = false; } } i++; if (i >= arr.length()) { break; } if (arr.charAt(i) != '1') { judge2 = false; } if (judge1 && judge2) {// 1:11101011 System.out.println((++count) + ":" + ans); } else if (judge1 == false && judge2 == true) { System.out.println((++count) + ":" + "parity check error"); } else if (judge2 == false) { System.out.println((++count) + ":" + "validate error"); } } } } } } } }
>具体RS232串口通信协议链接为:https://www.cnblogs.com/ybqjymy/p/12175994.html
public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); double a = Double.parseDouble(input.next()); double b = Double.parseDouble(input.next()); double c = Double.parseDouble(input.next()); if (a == 0) { System.out.println("Wrong Format"); System.exit(0); } // create a QuadraticEquation object QuadraticEquation equation = new QuadraticEquation(a, b, c); // get value of b * b - 4 * a * c double discriminant = equation.getDiscriminant(); System.out.println("a=" + equation.getA() + ",b=" + equation.getB() + ",c=" + equation.getC() + ":"); if (discriminant < 0) { System.out.println("The equation has no roots."); } else if (discriminant == 0) { System.out.println("The root is " + String.format("%.2f", equation.getRoot1())); } else // (discriminant >= 0) { System.out.println("The roots are " + String.format("%.2f", equation.getRoot1()) + " and " + String.format("%.2f", equation.getRoot2())); } } } class QuadraticEquation { // your code private double Root1, Root2; private double a, b, c; QuadraticEquation(double a, double b, double c) { this.a = a; this.b = b; this.c = c; } public double getDiscriminant() { double ans; ans = Math.pow(b, 2) - 4 * a * c; return ans; } public double getRoot1() { this.Root1 = (-1 * b + Math.sqrt(this.getDiscriminant())) / 2 / a; return this.Root1; } public double getA() { return a; } public void setA(double a) { this.a = a; } public double getB() { return b; } public void setB(double b) { this.b = b; } public double getC() { return c; } public void setC(double c) { this.c = c; } public double getRoot2() { this.Root2 = (-1 * b - Math.sqrt(this.getDiscriminant())) / 2 / a; return this.Root2; } }
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 if (choice == 3) { // test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:" + fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else { System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { private int year, month, day; public DateUtil(int year, int month, int day) { this.year = year; this.month = month; this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public boolean checkInputValidity() {// 检测输入的年、月、日是否合法 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31) { boolean judge = isLeapYear(year); if (judge) {// 闰年 if (leapdays[month - 1] >= day) { return true; } else { return false; } } else { if (commonday[month - 1] >= day) { return true; } else { return false; } } } else { return false; } } public boolean isLeapYear(int year) { if ((year % 4 != 0) || (year % 100 == 0 && year % 400 != 0)) { return false; } else { return true; } } public DateUtil getNextNDays(int n) {// 取得year-month-day的下n天日期 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if(n>100){ if(isLeapYear(year)){ n=n-leapdays[this.month-1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } }else{ n = n - commondays[this.month - 1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } } } this.day = this.day + n; while (true) { if (isLeapYear(this.year)) { if (this.day > leapdays[this.month - 1]) { this.day = this.day - leapdays[this.month - 1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } } else { break; } } else { if (this.day > commondays[this.month - 1]) { this.day = this.day - commondays[this.month - 1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } } else { break; } } } DateUtil nextday = new DateUtil(this.year, this.month, this.day); return nextday; } public DateUtil getPreviousNDays(int n) {// 取得year-month-day的前n天日期 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; this.day = this.day - n; while (true) { if (isLeapYear(this.year)) { if (this.day <= 0) { this.month--; if (this.month <= 0) { this.month = 12; this.year--; } this.day = this.day + leapdays[this.month - 1]; } else { if (this.day <= leapdays[this.month - 1]) { break; } else { this.day = this.day - leapdays[this.month - 1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } break; } } } else { if (this.day <= 0) { this.month--; if (this.month <= 0) { this.month = 12; this.year--; } this.day = this.day + commondays[this.month - 1]; } else { if (this.day <= commondays[this.month - 1]) { break; } else { this.day = this.day - commondays[this.month - 1]; this.month++; if (this.month > 12) { this.month = 1; this.year++; } break; } } } } DateUtil beforeday = new DateUtil(this.year, this.month, this.day); return beforeday; } public boolean compareDates(DateUtil date) {// 比较当前日期与date的大小(先后) boolean flag = false; if (this.year > date.year) { flag = true; } else if (this.year == date.year) { if (this.month > date.month) { flag = true; } else if (this.month == date.month) { if (this.day > date.day) { flag = true; } else { flag = false; } } else { flag = false; } } else { flag = false; } return flag; } public boolean equalTwoDates(DateUtil date) {// 判断两个日期是否相等 if (this.day == date.day && this.month == date.month && this.year == date.year) { return true; } else { return false; } } public int getDaysofDates(DateUtil date) {// 求当前日期与date之间相差的天数 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int ans = 0; if (this.equalTwoDates(date)) { ans = 0; } else { if (this.compareDates(date)) {// this更大 for (int i = date.year; i < this.year; i++) { if (isLeapYear(i)) { ans += 366; } else { ans += 365; } } int day1 = 0, day2 = 0; if (isLeapYear(this.year)) { for (int i = 0; i < this.month - 1; i++) { day1 += leapdays[i]; } } else { for (int i = 0; i < this.month - 1; i++) { day1 += commondays[i]; } } day1 += this.day; if (isLeapYear(date.year)) { for (int i = 0; i < date.month - 1; i++) { day2 += leapdays[i]; } } else { for (int i = 0; i < date.month - 1; i++) { day2 += commondays[i]; } } day2 += date.day; ans += (day1 - day2); } else { for (int i = this.year; i < date.year; i++) { if (isLeapYear(i)) { ans += 366; } else { ans += 365; } } int day1 = 0, day2 = 0; if (isLeapYear(this.year)) { for (int i = 0; i < this.month - 1; i++) { day1 += leapdays[i]; } } else { for (int i = 0; i < this.month - 1; i++) { day1 += commondays[i]; } } day1 += this.day; if (isLeapYear(date.year)) { for (int i = 0; i < date.month - 1; i++) { day2 += leapdays[i]; } } else { for (int i = 0; i < date.month - 1; i++) { day2 += commondays[i]; } } day2 += date.day; ans += (day2 - day1); } } return ans; } public String showDate() {// 以“year-month-day”格式返回日期值 Integer y = this.year; Integer m = this.month; Integer d = this.day; String Date = y.toString() + "-" + m.toString() + "-" + d.toString(); return Date; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); int year; int month; int day; int choice = input.nextInt(); if (choice == 1) { // test getNextNDays method int m; 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.println(date.getNextNDays(m).showDate()); } else if (choice == 2) { // test getPreviousNDays method int n; 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.println(date.getPreviousNDays(n).showDate()); } else if (choice == 3) { // test getDaysofDates method year = Integer.parseInt(input.next()); month = Integer.parseInt(input.next()); day = Integer.parseInt(input.next()); int anotherYear = Integer.parseInt(input.next()); int anotherMonth = Integer.parseInt(input.next()); int anotherDay = Integer.parseInt(input.next()); DateUtil fromDate = new DateUtil(year, month, day); DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { System.out.println(fromDate.getDaysofDates(toDate)); } else { System.out.println("Wrong Format"); System.exit(0); } } else { System.out.println("Wrong Format"); System.exit(0); } } } class DateUtil { private int year, month, day; private Day value; public Day getDay() { return value; } public void setDay(Day value) { this.value = value; } public DateUtil() { } public DateUtil(int y, int m, int d) { this.day = d; this.month = m; this.year = y; this.setDay(new Day(this.year, this.month, this.day)); } public boolean checkInputValidity() {// 检测输入的年、月、日是否合法 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (this.year >= 1900 && this.year <= 2050 && this.month >= 1 && this.month <= 12 && this.day >= 1 && this.day <= 31) { boolean judge = this.value.getMonth().getYear().isLeapYear(); if (judge) {// 闰年 return leapdays[month - 1] >= day; } else { return commonday[month - 1] >= day; } } else { return false; } } public DateUtil getNextNDays(int n) { int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (n > 100) { if (this.getDay().getMonth().getYear().isLeapYear()) { n = n - leapdays[this.value.getMonth().getValue() - 1]; this.getDay().getMonth().monthIncrement(); } else { n = n - commondays[this.getDay().getMonth().getValue() - 1]; this.getDay().getMonth().monthIncrement(); } } this.day = this.day + n; this.getDay().setValue(this.day);// ?? while (true) { if (this.getDay().getMonth().getYear().isLeapYear()) { if (this.getDay().getValue() > leapdays[this.getDay().getMonth().getValue() - 1]) { this.getDay() .setValue(this.getDay().getValue() - leapdays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthIncrement(); } else { break; } } else { if (this.getDay().getValue() > commondays[this.getDay().getMonth().getValue() - 1]) { this.getDay() .setValue(this.getDay().getValue() - commondays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthIncrement(); } else { break; } } } return new DateUtil(this.getDay().getMonth().getYear().getValue(), this.getDay().getMonth().getValue(), this.getDay().getValue()); } public DateUtil getPreviousNDays(int n) { int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; this.day = this.day - n; this.getDay().setValue(this.day); while (true) { if (this.getDay().getMonth().getYear().isLeapYear()) { if (this.getDay().getValue() <= 0) { this.getDay().getMonth().monthReduction(); this.getDay() .setValue(this.getDay().getValue() + leapdays[this.getDay().getMonth().getValue() - 1]); } else { if (this.getDay().getValue() > leapdays[this.getDay().getMonth().getValue() - 1]) { this.getDay() .setValue(this.getDay().getValue() - leapdays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthIncrement(); } break; } } else { if (this.getDay().getValue() <= 0) { this.getDay().getMonth().monthReduction(); this.getDay() .setValue(this.getDay().getValue() + commondays[this.getDay().getMonth().getValue() - 1]); } else { if (this.getDay().getValue() > commondays[this.getDay().getMonth().getValue() - 1]) { this.getDay().setValue( this.getDay().getValue() - commondays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthReduction(); } break; } } } return new DateUtil(this.getDay().getMonth().getYear().getValue(), this.getDay().getMonth().getValue(), this.getDay().getValue()); } public boolean compareDates(DateUtil date) {// 比较当前日期与date的大小(先后) boolean flag; if (this.year > date.year) { flag = true; } else if (this.year == date.year) { if (this.month > date.month) { flag = true; } else if (this.month == date.month) { flag = this.day > date.day; } else { flag = false; } } else { flag = false; } return flag; } public boolean equalTwoDates(DateUtil date) {// 判断两个日期是否相等 return this.day == date.day && this.month == date.month && this.year == date.year; } public int getDaysofDates(DateUtil date) {// 求当前日期与date之间相差的天数 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int ans = 0; if (this.equalTwoDates(date)) { } else { if (this.compareDates(date)) {// this更大 Year i = new Year(date.year); Year a = new Year(this.year); Year b = new Year(date.year); for (; i.getValue() < a.getValue(); i.yearIncrement()) { if (i.isLeapYear()) { ans += 366; } else { ans += 365; } } int day1 = 0, day2 = 0; if (a.isLeapYear()) { for (int j = 0; j < this.month - 1; j++) { day1 += leapdays[j]; } } else { for (int j = 0; j < this.month - 1; j++) { day1 += commondays[j]; } } day1 += this.day; if (b.isLeapYear()) { for (int j = 0; j < date.month - 1; j++) { day2 += leapdays[j]; } } else { for (int j = 0; j < date.month - 1; j++) { day2 += commondays[j]; } } day2 += date.day; ans += (day1 - day2); } else { Year i = new Year(this.year); Year a = new Year(this.year); Year b = new Year(date.year); for (; i.getValue() < b.getValue(); i.yearIncrement()) { if (i.isLeapYear()) { ans += 366; } else { ans += 365; } } int day1 = 0, day2 = 0; if (a.isLeapYear()) {// this for (int j = 0; j < this.month - 1; j++) { day1 += leapdays[j]; } } else { for (int j = 0; j < this.month - 1; j++) { day1 += commondays[j]; } } day1 += this.day; if (b.isLeapYear()) {// date for (int j = 0; j < date.month - 1; j++) { day2 += leapdays[j]; } } else { for (int j = 0; j < date.month - 1; j++) { day2 += commondays[j]; } } day2 += date.day; ans += (day2 - day1); } } return ans; } public String showDate() {// 以“year-month-day”格式返回日期值 Integer y = this.getDay().getMonth().getYear().getValue(); Integer m = this.getDay().getMonth().getValue(); Integer d = this.getDay().getValue(); return y + "-" + m + "-" + d; } } class Day { private int yearValue, monthValue, dayValue; private Month value; public int getValue() { return dayValue; } public void setValue(int dayValue) { this.dayValue = dayValue; } public Month getMonth() { return value; } public void setMonth(Month value) { this.value = value; } public Day(int yearValue, int monthValue, int dayValue) { this.yearValue = yearValue; this.monthValue = monthValue; this.dayValue = dayValue; this.setMonth(new Month(this.yearValue, this.monthValue)); } public Day() { } public void resetMin() { this.dayValue = 1; } public void resetMax() { int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (this.value.getYear().isLeapYear()) { this.dayValue = leapdays[this.value.getValue() - 1]; } else { this.dayValue = commonday[this.value.getValue() - 1]; } } public boolean validate() { int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (this.getMonth().getYear().isLeapYear()) { return this.dayValue >= 1 && this.dayValue <= leapdays[this.value.getValue() - 1]; } else { return this.dayValue >= 1 && this.dayValue <= commonday[this.value.getValue() - 1]; } } public void dayIncrement() { this.dayValue++;// 加一天 } public void dayReduction() { this.dayValue--; } } class Month { private int monthValue; private Year value; public Month() { } public Month(int yearValue, int monthValue) { this.monthValue = monthValue; this.setYear(new Year(yearValue)); } public int getValue() { return monthValue; } public void setValue(int monthValue) { this.monthValue = monthValue; } public Year getYear() { return value; } public void setYear(Year value) { this.value = value; } public void resetMin() { this.monthValue = 1; } public void resetMax() { this.monthValue = 12; } boolean validate() { return this.monthValue <= 12 && this.monthValue >= 1; } public void monthIncrement() { this.monthValue++; if (this.monthValue>12) { resetMin(); value.yearIncrement(); } } public void monthReduction() { this.monthValue--; if (this.monthValue<=0) { resetMax(); value.yearReduction(); } } } class Year { private int value; public int getValue() { return this.value; } public void setValue(int value) { this.value = value; } public Year() { } public Year(int value) { this.value = value; } public boolean isLeapYear() { return (this.value % 4 == 0) && (this.value % 100 != 0 || this.value % 400 == 0); } public boolean validate() { if (this.value <= 2050 && this.value >= 1900) { return true; } else { return false; } } void yearIncrement() { this.value++; } void yearReduction() { this.value--; } }
java public class Main { public static void main(String[] args) { System.out.println(0.06 + 0.01); System.out.println(1.0 - 0.42); System.out.println(4.014 * 100); System.out.println(303.1 / 1000); } }
- 该段代码应该输出为
- 0.07 0.58 401.4 0.3031
- 实际输出为:
与所需要输出有误差,不可用于作比较,会出现很大的误差
总结:在利用java进行高精度运算时,应该使用BigInteger或者BigDecimal类,使用double类极其容易出现误差,从而影响计算的结果
心得:在进行代码的设计时应该列一个大纲,设计类图,了解自己要写的代码以及各个类之间的调用关系
在每次的计算边界之前给每个边界一个特判,并且在每个边界都要给出边界值进行转换和复位
public DateUtil getPreviousNDays(int n) { int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commondays = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; this.day = this.day - n; this.getDay().setValue(this.day); while (true) { if (this.getDay().getMonth().getYear().isLeapYear()) { if (this.getDay().getValue() <= 0) { this.getDay().getMonth().monthReduction(); this.getDay() .setValue(this.getDay().getValue() + leapdays[this.getDay().getMonth().getValue() - 1]); } else { if (this.getDay().getValue() > leapdays[this.getDay().getMonth().getValue() - 1]) { this.getDay() .setValue(this.getDay().getValue() - leapdays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthIncrement(); } break; } } else { if (this.getDay().getValue() <= 0) { this.getDay().getMonth().monthReduction(); this.getDay() .setValue(this.getDay().getValue() + commondays[this.getDay().getMonth().getValue() - 1]); } else { if (this.getDay().getValue() > commondays[this.getDay().getMonth().getValue() - 1]) { this.getDay().setValue( this.getDay().getValue() - commondays[this.getDay().getMonth().getValue() - 1]); this.getDay().getMonth().monthReduction(); } break; } } } return new DateUtil(this.getDay().getMonth().getYear().getValue(), this.getDay().getMonth().getValue(), this.getDay().getValue()); }
心得:设计类的时候简化类之间的调用关系,让类之间的耦合度降低,增加其可读性,减少其代码修改的成本
public boolean checkInputValidity() {// 检测输入的年、月、日是否合法 int[] leapdays = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31) { boolean judge = isLeapYear(year); if (judge) {// 闰年 if (leapdays[month - 1] >= day) { return true; } else { return false; } } else { if (commonday[month - 1] >= day) { return true; } else { return false; } } } else { return false; } }
public boolean checkInputValidity() {// 检测输入的年、月、日是否合法 int[] commonday = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (year >= 1820 && year <= 2020 && month >= 1 && month <= 12 && day >= 1 && day <= 31) { boolean judge = isLeapYear(year); boolean flag; if (judge) {// 闰年 if(month==2){ if (leapdays[month - 1]+1 >= day) { flag = true; } else { flag = false; } }else{ if (leapdays[month - 1]+1 >= day) { flag = true; } else { flag = false; } } } else { if (commonday[month - 1] >= day) { flag = true; } else { flag = false; } } } else { flag = false; } return flag; }
设置单一出口,增加代码的易读性
只是用一个月份数组,避免空间的浪费
public String showDate() {// 以“year-month-day”格式返回日期值 Integer y = this.year; Integer m = this.month; Integer d = this.day; String Date = y.toString() + "-" + m.toString() + "-" + d.toString(); return Date; }
修改后为
public String showDate() { return this.year + "-" + this.month + "-" + this.day; }
减少多余的代码,减少代码量,做到代码的简洁和严谨