Blog作業(yè)1
黃楊濤的第一個(gè)月java學(xué)習(xí):
1.前言:
經(jīng)過了一個(gè)月對(duì)Java編程語言的學(xué)習(xí),應(yīng)該算是正式入門了。接下來就對(duì)這一個(gè)月的學(xué)習(xí)成果做一個(gè)總結(jié)吧!
PTA01:總共九道題目,整體難度中等,題量偏多。還是用了c語言的編程習(xí)慣,一個(gè)main解決所有問題。。。完成這次作業(yè)之后,至少能夠知道Scanner類的用法,以及大部分基礎(chǔ)語法和c語言是類似的。
PTA02:總共三道題目,主要考察的是對(duì)字符串的截取處理。對(duì)String類的一些常用方法有了了解。整體難度比第一次有所提升,題量適中。
PTA03:總共四道題目,第一題是沒難度的。后三題是同一個(gè)題的三種不同寫法。在老師特定框架下,解決題目。題目難度明顯提升,不過只要第一次邏輯沒問題,后面也沒有太大問題。應(yīng)該是帶我們進(jìn)入從面向?qū)ο蟮氖澜绨伞2坏貌慌宸蠋熝簧普T人,一步一步帶我們感受。題量適中。
2.設(shè)計(jì)與分析:
PTA01_1-PTA01_9:(由于有些題目過于簡(jiǎn)單,這里省略,只放有心得的代碼)
PTA01_9:
1 import java.util.Scanner; 2 public class Main { 3 public static void main(String[] args) { 4 Scanner in = new Scanner(System.in); 5 double a=in.nextDouble(); 6 double b=in.nextDouble(); 7 double c=in.nextDouble(); 8 if(a >= 1 && a <= 200 && b >= 1 && b <= 200 && c>=1 &&c<=200){ 9 if(a+b>c&&a+c>b&&b+c>a){ 10 if(a==b&&b==c) 11 System.out.println("Equilateral triangle"); 12 else { 13 if(a == b || b == c || a == c){ 14 if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1))//不要用a*a+b*b==c*c 15 System.out.println("Isosceles right-angled triangle"); 16 else 17 System.out.println("Isosceles triangle"); 18 } 19 else{ 20 if((a*a+b*b-c*c<0.1)||(a*a+c*c-b*b<0.1)||(b*b+c*c-a*a<0.1)) 21 System.out.println("Right-angled triangle"); 22 else 23 System.out.println("General triangle"); 24 } 25 } 26 } 27 else 28 System.out.println("Not a triangle"); 29 } 30 else 31 System.out.println("Wrong Format"); 32 } 33 }
小結(jié):本題考查對(duì)字符串的截取和有效信息的截取;在本次作業(yè)中對(duì)一些String類的方法有了更深刻的理解。
檢索下標(biāo)為i的字符str.charAt(int i);
檢索字符串長度str.length();
截取字符串:str.substing(int i, int j);返回一個(gè)String對(duì)象,括號(hào)左開右閉。
奇校驗(yàn)偶校驗(yàn):奇偶校驗(yàn)碼就是在信息碼后面加一位校驗(yàn)碼,添加一位校驗(yàn)碼后,使得整個(gè)碼字里面1的個(gè)數(shù)是奇數(shù)(偶數(shù))。接收端收到數(shù)據(jù)后就校驗(yàn)數(shù)據(jù)里1的個(gè)數(shù),若檢測(cè)到奇數(shù)(偶數(shù))個(gè)1,則認(rèn)為傳 輸沒有出錯(cuò);
本題思路也是并未用面向?qū)ο蟮乃季S解決,所以沒有類圖。該題題目難度中等,眾多的分支結(jié)構(gòu)比較考驗(yàn)思維邏輯。
PTA03_2 - PTA03-4:
02:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 32 System.out.print(date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " next " + m + " days is:"); 33 System.out.println(date.getNextNDays(m).showDate()); 34 } else if (choice == 2) { // test getPreviousNDays method 35 int n = 0; 36 year = Integer.parseInt(input.next()); 37 month = Integer.parseInt(input.next()); 38 day = Integer.parseInt(input.next()); 39 40 DateUtil date = new DateUtil(year, month, day); 41 42 if (!date.checkInputValidity()) { 43 System.out.println("Wrong Format"); 44 System.exit(0); 45 } 46 47 n = input.nextInt(); 48 49 if (n < 0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 54 System.out.print( 55 date.getYear() + "-" + date.getMonth() + "-" + date.getDay() + " previous " + n + " days is:"); 56 System.out.println(date.getPreviousNDays(n).showDate()); 57 } else if (choice == 3) { //test getDaysofDates method 58 year = Integer.parseInt(input.next()); 59 month = Integer.parseInt(input.next()); 60 day = Integer.parseInt(input.next()); 61 62 int anotherYear = Integer.parseInt(input.next()); 63 int anotherMonth = Integer.parseInt(input.next()); 64 int anotherDay = Integer.parseInt(input.next()); 65 66 DateUtil fromDate = new DateUtil(year, month, day); 67 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 68 69 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 70 System.out.println("The days between " + fromDate.showDate() + 71 " and " + toDate.showDate() + " are:" 72 + fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 } 84 85 86 class DateUtil { 87 private int year,month,day; 88 89 public DateUtil() { 90 super(); 91 } 92 93 public DateUtil(int year, int month, int day) { 94 super(); 95 this.year = year; 96 this.month = month; 97 this.day = day; 98 } 99 100 public int getDay() { 101 return day; 102 } 103 104 public int getYear() { 105 return year; 106 } 107 108 public int getMonth() { 109 return month; 110 } 111 112 public boolean isLeapYear(int year){ 113 //判斷year是否為閏年 114 if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){ 115 return true; 116 }else 117 return false; 118 } 119 120 public boolean checkInputValidity(){ 121 //檢測(cè)輸入的年、月、日是否合法 122 if(year >= 1820 && year <= 2020){ 123 if((isLeapYear(year) && month ==2 &&day > 0 && day <= 29)||(!isLeapYear(year) && month ==2 && day > 0 && day <= 28)||((month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)&&day<=31&&day>0)||((month == 4 || month == 6 || month == 9 || month == 11 )&&day<=30&&day>0)){ 124 return true; 125 }else 126 return false; 127 }else 128 return false; 129 } 130 131 public DateUtil getNextNDays(int n){ 132 //取得year-month-day的下n天日期 133 DateUtil date_next = new DateUtil(); 134 date_next.day = day; 135 date_next.month = month; 136 date_next.year = year; 137 int i = n; 138 for(; i >= 365 ; date_next.year += 1) { 139 if((isLeapYear(date_next.year) && date_next.month < 3 )||(!isLeapYear(date_next.year) && isLeapYear(date_next.year+1) && date_next.month > 2)) 140 i-=366; 141 else i -= 365; 142 } 143 for(;i > 0;i--){ 144 if(date_next.month == 12 && date_next.day == 31) { 145 date_next.year ++; 146 date_next.month = 1; 147 date_next.day = 1; 148 }else if((date_next.month == 4 || date_next.month == 6 || date_next.month == 9 || date_next.month == 11 )&& date_next.day == 30){ 149 date_next.day = 1; 150 date_next.month ++; 151 }else if((date_next.month == 1 || date_next.month == 3 || date_next.month == 5 || date_next.month == 7 || date_next.month == 8 || date_next.month == 10 ) && date_next.day == 31) { 152 date_next.day = 1; 153 date_next.month ++; 154 }else if(date_next.month == 2) { 155 if((isLeapYear(date_next.year) && date_next.day == 29) || (!isLeapYear(date_next.year) && date_next.day == 28)){ 156 date_next.day = 1; 157 date_next.month = 3; 158 }else 159 date_next.day++; 160 }else 161 date_next.day++; 162 } 163 return date_next; 164 } 165 public DateUtil getPreviousNDays(int n){ 166 //取得year-month-day的前n天日期 167 DateUtil date = new DateUtil(); 168 date.day = day; 169 date.month = month; 170 date.year = year; 171 int i = n; 172 for(; i >= 365 ; date.year -= 1) { 173 if((isLeapYear(date.year) && date.month > 2)||(!isLeapYear(date.year) && isLeapYear(date.year - 1) && date.month < 3)) 174 i-=366; 175 else i -= 365; 176 } 177 for(;i > 0;i--){ 178 if(date.month == 1 && date.day == 1) { 179 date.year --; 180 date.month = 12; 181 date.day = 31; 182 }else if((date.month == 2 || date.month == 4 || date.month == 6 || date.month == 8 || date.month == 9 || date.month == 11) && date.day == 1){ 183 date.day = 31; 184 date.month --; 185 }else if((date.month == 5 || date.month == 7 || date.month == 10 || date.month == 12 ) && date.day == 1) { 186 date.day = 30; 187 date.month --; 188 }else if(date.month == 3 && date.day == 1) { 189 if(isLeapYear(date.year)){ 190 date.day = 29; 191 date.month = 2; 192 }else{ 193 date.day = 28; 194 date.month = 2; 195 } 196 }else 197 date.day--; 198 } 199 return date; 200 } 201 202 public boolean compareDates(DateUtil date) {//前先于后返回true 203 if(date.year > year) 204 return true; 205 else if(date.year < year) 206 return false; 207 else { 208 if(date.month > month) 209 return true; 210 else if(date.month < month) 211 return false; 212 else { 213 if(date.day > day) 214 return true; 215 else 216 return false; 217 } 218 } 219 } 220 221 public boolean equalTwoDates(DateUtil date){ 222 //判斷兩個(gè)日期是否相等 223 if(date.year == year && date.month == month && date.day == day) 224 return true; 225 else return false; 226 } 227 228 public int getDaysofDates(DateUtil date){ 229 //求當(dāng)前日期與date之間相差的天數(shù) 230 int numOfdays = 0; 231 if(!equalTwoDates(date)){ 232 while(date.year > year + 1) { 233 if(isLeapYear(date.year) && date.month > 2 ) 234 numOfdays += 366; 235 else numOfdays += 365; 236 date.year -= 1; 237 } 238 for(int i = 1;; i++) { 239 if(this.getNextNDays(i).equalTwoDates(date)){ 240 numOfdays++; 241 break; 242 } 243 numOfdays++; 244 } 245 } 246 return numOfdays; 247 } 248 249 public String showDate(){ 250 //以“year-month-day”格式返回日期值 251 String date; 252 date = String.valueOf(year) + "-" + String.valueOf(month) + "-" + String.valueOf(day); 253 return date; 254 } 255 256 }
總結(jié):本體主函數(shù)已經(jīng)寫好,相當(dāng)于需要我們補(bǔ)充一個(gè)類DateUtil,下面是類圖,這種寫法類似于c語言,思維簡(jiǎn)單,但是不是很合理。
03:
1 import java.util.Scanner; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 7 Scanner input = new Scanner(System.in); 8 9 int choice = input.nextInt(); 10 11 int year = 0; 12 int month = 0; 13 int day = 0; 14 15 if (choice == 1) { // test getNextNDays method 16 int m = 0; 17 year = Integer.parseInt(input.next()); 18 month = Integer.parseInt(input.next()); 19 day = Integer.parseInt(input.next()); 20 21 DateUtil date = new DateUtil(year, month, day); 22 23 if (!date.checkInputValidity()) { 24 System.out.println("Wrong Format"); 25 System.exit(0); 26 } 27 28 m = input.nextInt(); 29 30 if (m < 0) { 31 System.out.println("Wrong Format"); 32 System.exit(0); 33 } 34 35 // System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " next " + m + " days is:"); 36 System.out.println(date.getNextNDays(m).showDate()); 37 }else if (choice == 2) { // test getPreviousNDays method 38 int n = 0; 39 year = Integer.parseInt(input.next()); 40 month = Integer.parseInt(input.next()); 41 day = Integer.parseInt(input.next()); 42 43 DateUtil date = new DateUtil(year, month, day); 44 45 if (!date.checkInputValidity()) { 46 System.out.println("Wrong Format"); 47 System.exit(0); 48 } 49 50 n = input.nextInt(); 51 52 if (n < 0) { 53 System.out.println("Wrong Format"); 54 System.exit(0); 55 } 56 // System.out.print(date.getDay().getMonth().getYear().getValue() + "-" + date.getDay().getMonth().getValue() + "-" + date.getDay().getValue() + " previous " + n + " days is:"); 57 System.out.println(date.getPreviousNDays(n).showDate()); 58 }else if (choice == 3) { //test getDaysofDates method 59 year = Integer.parseInt(input.next()); 60 month = Integer.parseInt(input.next()); 61 day = Integer.parseInt(input.next()); 62 63 int anotherYear = Integer.parseInt(input.next()); 64 int anotherMonth = Integer.parseInt(input.next()); 65 int anotherDay = Integer.parseInt(input.next()); 66 67 DateUtil fromDate = new DateUtil(year, month, day); 68 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 69 70 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 71 // System.out.print("The days between " + fromDate.showDate() + 72 // " and " + toDate.showDate() + " are:"); 73 System.out.println(fromDate.getDaysofDates(toDate)); 74 } else { 75 System.out.println("Wrong Format"); 76 System.exit(0); 77 } 78 } 79 else{ 80 System.out.println("Wrong Format"); 81 System.exit(0); 82 } 83 84 input.close(); 85 } 86 } 87 88 class DateUtil { 89 90 private Day day = new Day(); 91 92 public DateUtil(int y, int m, int d) { 93 super(); 94 day.setValue(d); 95 day.getMonth().setValue(m); 96 day.getMonth().getYear().setValue(y); 97 } 98 99 public DateUtil() { 100 super(); 101 } 102 103 public Day getDay() { 104 return day; 105 } 106 107 public void setDay(Day d){ 108 this.day = d; 109 } 110 111 public boolean checkInputValidity(){//校驗(yàn)數(shù)據(jù)合法性 112 if(day.validate() && day.getMonth().validate() && day.getMonth().getYear().validate()) 113 return true; 114 else return false; 115 } 116 117 public boolean compareDates(DateUtil date){//前先于后返回true 118 if(date.getDay().getMonth().getYear().getValue() > this.getDay().getMonth().getYear().getValue()) 119 return true; 120 else if(date.getDay().getMonth().getYear().getValue() < this.getDay().getMonth().getYear().getValue()) 121 return false; 122 else { 123 if(date.getDay().getMonth().getValue() > day.getMonth().getValue()) 124 return true; 125 else if(date.getDay().getMonth().getValue() < day.getMonth().getValue()) 126 return false; 127 else { 128 if(date.day.getValue() > day.getValue()) 129 return true; 130 else 131 return false; 132 } 133 } 134 135 } 136 137 public boolean equalTwoDates(DateUtil date) {//判定兩日期是否相等 138 if(day.getValue() == date.getDay().getValue() 139 && day.getMonth().getValue() == date.getDay().getMonth().getValue() 140 && day.getMonth().getYear().getValue() == date.getDay().getMonth().getYear().getValue()) 141 return true; 142 else return false; 143 } 144 145 public String showDate(){//日期格式化 146 String date; 147 date = String.valueOf(day.getMonth().getYear().getValue()) + "-" + String.valueOf(day.getMonth().getValue()) + "-" + String.valueOf(day.getValue()); 148 return date; 149 } 150 151 public DateUtil getNextNDays(int n){//求下n天 152 DateUtil date_next = new DateUtil(); 153 date_next.day.setValue(this.day.getValue()); 154 date_next.day.getMonth().setValue(this.day.getMonth().getValue()); 155 date_next.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 156 int i = n; 157 for(; i >= 365 ; date_next.day.getMonth().getYear().yearIncrement()) { 158 if((date_next.day.getMonth().getYear().isLeapYear() 159 && date_next.day.getMonth().getValue() < 3 ) 160 ||(!date_next.day.getMonth().getYear().isLeapYear() 161 && date_next.day.getMonth().getYear().isLeapYear(date_next.day.getMonth().getYear().getValue()+1) 162 && date_next.day.getMonth().getValue() > 2)) 163 i-=366; 164 else i -= 365; 165 } 166 for(;i > 0;i--){ 167 if(date_next.day.getMonth().getValue() == 12 && date_next.day.getValue() == 31) { 168 date_next.day.getMonth().getYear().yearIncrement(); 169 date_next.day.getMonth().resetMin(); 170 date_next.day.resetMin(); 171 }else if(date_next.day.getMonth().getValue() == 2) { 172 if((date_next.day.getMonth().getYear().isLeapYear()&& date_next.day.getValue() == 29) 173 || (!date_next.day.getMonth().getYear().isLeapYear() && date_next.day.getValue() == 28)){ 174 date_next.day.resetMin(); 175 date_next.day.getMonth().setValue(3); 176 }else 177 date_next.day.dayIncrement(); 178 }else if(date_next.day.getValue() == date_next.day.mon_maxnum[date_next.day.getMonth().getValue()]){ 179 date_next.day.resetMin(); 180 date_next.day.getMonth().monthIcrement(); 181 }else 182 date_next.day.dayIncrement(); 183 } 184 return date_next; 185 } 186 187 public DateUtil getPreviousNDays(int n){//求前n天 188 DateUtil date_before = new DateUtil(); 189 date_before.day.setValue(this.day.getValue()); 190 date_before.day.getMonth().setValue(this.day.getMonth().getValue()); 191 date_before.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 192 int i = n; 193 for(; i >= 365 ; date_before.day.getMonth().getYear().yearReduction()) { 194 if((date_before.day.getMonth().getYear().isLeapYear() 195 && date_before.day.getMonth().getValue() > 2 ) 196 ||(!date_before.day.getMonth().getYear().isLeapYear() 197 && date_before.day.getMonth().getYear().isLeapYear(date_before.day.getMonth().getYear().getValue()-1) 198 && date_before.day.getMonth().getValue() < 3)) 199 i-=366; 200 else i -= 365; 201 } 202 for(;i > 0;i--){ 203 if(date_before.day.getMonth().getValue() == 1 && date_before.day.getValue() == 1) { 204 date_before.day.getMonth().getYear().yearReduction(); 205 date_before.day.getMonth().resetMax(); 206 date_before.day.resetMax(); 207 }else if(date_before.day.getMonth().getValue() == 3 && date_before.day.getValue() == 1) { 208 date_before.day.getMonth().setValue(2); 209 if(date_before.day.getMonth().getYear().isLeapYear()) 210 date_before.day.setValue(29); 211 else 212 date_before.day.setValue(28); 213 }else if(date_before.day.getValue() == 1){ 214 date_before.day.getMonth().monthReduction(); 215 date_before.day.resetMax(); 216 }else 217 date_before.day.dayReduction(); 218 } 219 return date_before; 220 } 221 222 public int getDaysofDates(DateUtil date) {//求兩日期之間的天數(shù) 223 int numOfdays = 0; 224 DateUtil temp = new DateUtil(); 225 if(!this.compareDates(date)){ 226 temp.day.setValue(date.day.getValue()); 227 temp.day.getMonth().setValue(date.day.getMonth().getValue()); 228 temp.day.getMonth().getYear().setValue(date.day.getMonth().getYear().getValue()); 229 date.day.setValue(this.day.getValue()); 230 date.day.getMonth().setValue(this.day.getMonth().getValue()); 231 date.day.getMonth().getYear().setValue(this.day.getMonth().getYear().getValue()); 232 this.day.setValue(temp.day.getValue()); 233 this.day.getMonth().setValue(temp.day.getMonth().getValue()); 234 this.day.getMonth().getYear().setValue(temp.day.getMonth().getYear().getValue()); 235 236 } 237 for(;date.getDay().getMonth().getYear().getValue() > this.getDay().getMonth().getYear().getValue() + 1;) { 238 if(this.getDay().getMonth().getYear().isLeapYear() && this.getDay().getMonth().getValue() < 3 ) 239 numOfdays += 366; 240 else numOfdays += 365; 241 this.getDay().getMonth().getYear().yearIncrement(); 242 } 243 int i ; 244 for( i = 0;this.compareDates(date); i++){ 245 if(this.day.getMonth().getValue() == 12 && this.day.getValue() == 31) { 246 this.day.getMonth().getYear().yearIncrement(); 247 this.day.resetMin(); 248 this.day.getMonth().resetMin(); 249 }else if(this.day.getMonth().getValue() == 2) { 250 if((day.getMonth().getYear().isLeapYear()&& day.getValue() == 29) 251 || (!day.getMonth().getYear().isLeapYear() && day.getValue() == 28)){ 252 day.resetMin(); 253 day.getMonth().monthIcrement(); 254 }else 255 day.dayIncrement(); 256 }else if(this.day.getValue() == this.day.mon_maxnum[day.getMonth().getValue()]) { 257 this.day.resetMin(); 258 this.day.getMonth().monthIcrement(); 259 }else 260 this.day.dayIncrement(); 261 } 262 numOfdays += i; 263 return numOfdays; 264 } 265 } 266 class Day { 267 private int value; 268 private Month month = new Month(); 269 int[] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 270 271 public Day() { 272 super(); 273 // TODO Auto-generated constructor stub 274 } 275 276 public Day(int yearValue, int dayValue, int monthValue) { 277 super(); 278 this.value = dayValue; 279 month.setValue(monthValue); 280 month.getYear().setValue(yearValue); 281 } 282 283 public int getValue() { 284 return value; 285 } 286 287 public void setValue(int value) { 288 this.value = value; 289 } 290 291 public Month getMonth() { 292 return month; 293 } 294 295 public void setMonth(Month month) { 296 this.month = month; 297 } 298 299 public void resetMin() { 300 this.value = 1; 301 } 302 303 public void resetMax() { 304 this.value = this.mon_maxnum[month.getValue()]; 305 } 306 307 public boolean validate() {//校驗(yàn)數(shù)據(jù)合法性 308 if(month.validate()) 309 if(value > 0 && value <= this.mon_maxnum[this.month.getValue()]) 310 return true; 311 else return false; 312 else return false; 313 } 314 315 public void dayIncrement() {//增一 316 value++; 317 } 318 319 public void dayReduction() {//減一 320 value--; 321 } 322 323 324 } 325 class Month { 326 private int value; 327 private Year year = new Year(); 328 329 public Month() { 330 super(); 331 // TODO Auto-generated constructor stub 332 } 333 334 public Month(int yearValue, int monthValue) { 335 super(); 336 year.setValue(yearValue); 337 this.value = monthValue; 338 } 339 340 public int getValue() { 341 return value; 342 } 343 344 public void setValue(int value) { 345 this.value = value; 346 } 347 348 public Year getYear() { 349 return year; 350 } 351 352 public void setYear(Year year) { 353 this.year = year; 354 } 355 356 public void resetMin() { 357 this.value = 1; 358 } 359 360 public void resetMax() {//月份設(shè)置為12 361 this.value = 12; 362 } 363 364 public boolean validate() {//校驗(yàn)數(shù)據(jù)合法性 365 if(value > 0 && value <= 12) 366 return true; 367 else return false; 368 } 369 370 public void monthIcrement() { 371 this.value++; 372 } 373 374 public void monthReduction() { 375 this.value--; 376 } 377 } 378 class Year { 379 private int value; 380 381 public Year() { 382 super(); 383 // TODO Auto-generated constructor stub 384 } 385 386 public Year(int value) { 387 super(); 388 this.value = value; 389 } 390 391 public int getValue() { 392 return value; 393 } 394 395 public void setValue(int value) { 396 this.value = value; 397 } 398 399 public boolean isLeapYear() {//判斷是否潤年 400 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 401 return true; 402 }else 403 return false; 404 } 405 406 public boolean isLeapYear(int value) {//判斷是否潤年 407 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 408 return true; 409 }else 410 return false; 411 } 412 413 public boolean validate() {//校驗(yàn)數(shù)據(jù)合法性 414 if(value >= 1900 && value <= 2050) { 415 return true; 416 } 417 else return false; 418 } 419 420 public void yearIncrement() {//年份增一 421 value++; 422 } 423 424 public void yearReduction() {//年份減一 425 value--; 426 } 427 }
類圖:

04:
1 import java.util.Scanner; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner input = new Scanner(System.in); 6 int year = 0; 7 int month = 0; 8 int day = 0; 9 10 int choice = input.nextInt(); 11 12 if (choice == 1) { // test getNextNDays method 13 int m = 0; 14 year = Integer.parseInt(input.next()); 15 month = Integer.parseInt(input.next()); 16 day = Integer.parseInt(input.next()); 17 18 DateUtil date = new DateUtil(year, month, day); 19 20 if (!date.checkInputValidity()) { 21 System.out.println("Wrong Format"); 22 System.exit(0); 23 } 24 25 m = input.nextInt(); 26 27 if (m < 0) { 28 System.out.println("Wrong Format"); 29 System.exit(0); 30 } 31 32 System.out.print(date.showDate() + " next " + m + " days is:"); 33 System.out.println(date.getNextNDays(m).showDate()); 34 } else if (choice == 2) { // test getPreviousNDays method 35 int n = 0; 36 year = Integer.parseInt(input.next()); 37 month = Integer.parseInt(input.next()); 38 day = Integer.parseInt(input.next()); 39 40 DateUtil date = new DateUtil(year, month, day); 41 42 if (!date.checkInputValidity()) { 43 System.out.println("Wrong Format"); 44 System.exit(0); 45 } 46 47 n = input.nextInt(); 48 49 if (n < 0) { 50 System.out.println("Wrong Format"); 51 System.exit(0); 52 } 53 54 System.out.print( 55 date.showDate() + " previous " + n + " days is:"); 56 System.out.println(date.getPreviousNDays(n).showDate()); 57 } else if (choice == 3) { //test getDaysofDates method 58 year = Integer.parseInt(input.next()); 59 month = Integer.parseInt(input.next()); 60 day = Integer.parseInt(input.next()); 61 62 int anotherYear = Integer.parseInt(input.next()); 63 int anotherMonth = Integer.parseInt(input.next()); 64 int anotherDay = Integer.parseInt(input.next()); 65 66 DateUtil fromDate = new DateUtil(year, month, day); 67 DateUtil toDate = new DateUtil(anotherYear, anotherMonth, anotherDay); 68 69 if (fromDate.checkInputValidity() && toDate.checkInputValidity()) { 70 System.out.println("The days between " + fromDate.showDate() + 71 " and " + toDate.showDate() + " are:" 72 + fromDate.getDaysofDates(toDate)); 73 } else { 74 System.out.println("Wrong Format"); 75 System.exit(0); 76 } 77 } 78 else{ 79 System.out.println("Wrong Format"); 80 System.exit(0); 81 } 82 } 83 } 84 85 class DateUtil { 86 Year year = new Year(); 87 Month month = new Month(); 88 Day day = new Day(); 89 int [] mon_maxnum = {0,31,28,31,30,31,30,31,31,30,31,30,31}; 90 91 public DateUtil() { 92 super(); 93 // TODO Auto-generated constructor stub 94 } 95 96 public DateUtil(int y, int m, int d) { 97 super(); 98 year.setValue(y); 99 month.setValue(m); 100 day.setValue(d); 101 } 102 103 public Year getYear() { 104 return year; 105 } 106 107 public void setYear(Year year) { 108 this.year = year; 109 } 110 111 public Month getMonth() { 112 return month; 113 } 114 115 public void setMonth(Month month) { 116 this.month = month; 117 } 118 119 public Day getDay() { 120 return day; 121 } 122 123 public void setDay(Day day) { 124 this.day = day; 125 } 126 127 public void resetMin() { 128 this.day.setValue(1);; 129 } 130 131 public void resetMax() { 132 this.day.setValue(this.mon_maxnum[month.getValue()]); 133 } 134 135 public boolean checkInputValidity(){//校驗(yàn)數(shù)據(jù)合法性 136 if(year.validate() && month.validate()) 137 if(day.getValue() > 0 && day.getValue() <= this.mon_maxnum[this.month.getValue()]) 138 return true; 139 else return false; 140 else return false; 141 } 142 143 public boolean compareDates(DateUtil date){//前先于后返回true 144 if(date.year.getValue() > this.year.getValue()) 145 return true; 146 else if(date.year.getValue() < this.year.getValue()) 147 return false; 148 else { 149 if(date.getMonth().getValue() > this.getMonth().getValue()) 150 return true; 151 else if(date.getMonth().getValue() < this.getMonth().getValue()) 152 return false; 153 else { 154 if(date.day.getValue() > this.day.getValue()) 155 return true; 156 else 157 return false; 158 } 159 } 160 } 161 162 public String showDate(){//日期格式化 163 String date; 164 date = String.valueOf(this.getYear().getValue()) + "-" 165 + String.valueOf(this.getMonth().getValue()) + "-" 166 + String.valueOf(this.getDay().getValue()); 167 return date; 168 } 169 170 public boolean equalTwoDates(DateUtil date) {//判定兩日期是否相等 171 if(day.getValue() == date.getDay().getValue() 172 && this.getMonth().getValue() == date.getMonth().getValue() 173 && this.getYear().getValue() == date.getYear().getValue()) 174 return true; 175 else return false; 176 } 177 178 public DateUtil getNextNDays(int n){//求下n天 179 DateUtil date_next = new DateUtil(); 180 date_next.day.setValue(this.day.getValue()); 181 date_next.getMonth().setValue(this.getMonth().getValue()); 182 date_next.getYear().setValue(this.getYear().getValue()); 183 int i = n; 184 for(; i >= 365 ; date_next.getYear().yearIncrement()) { 185 if((date_next.getYear().isLeapYear() 186 && date_next.getMonth().getValue() < 3 ) 187 ||(!date_next.getYear().isLeapYear() 188 && date_next.getYear().isLeapYear(date_next.getYear().getValue()+1) 189 && date_next.getMonth().getValue() > 2)) 190 i-=366; 191 else i -= 365; 192 } 193 for(;i > 0;i--){ 194 if(date_next.getMonth().getValue() == 12 && date_next.day.getValue() == 31) { 195 date_next.getYear().yearIncrement(); 196 date_next.getMonth().resetMin(); 197 date_next.resetMin(); 198 }else if(date_next.getMonth().getValue() == 2) { 199 if((date_next.getYear().isLeapYear()&& date_next.day.getValue() == 29) 200 || (!date_next.getYear().isLeapYear() && date_next.day.getValue() == 28)){ 201 date_next.resetMin(); 202 date_next.getMonth().setValue(3); 203 }else 204 date_next.day.dayIncrement(); 205 }else if(date_next.day.getValue() == date_next.mon_maxnum[date_next.getMonth().getValue()]){ 206 date_next.resetMin(); 207 date_next.getMonth().monthIcrement(); 208 }else 209 date_next.day.dayIncrement(); 210 } 211 return date_next; 212 } 213 214 public DateUtil getPreviousNDays(int n){//求前n天 215 DateUtil date_before = new DateUtil(); 216 date_before.day.setValue(this.day.getValue()); 217 date_before.getMonth().setValue(this.getMonth().getValue()); 218 date_before.getYear().setValue(this.getYear().getValue()); 219 int i = n; 220 for(; i >= 365 ; date_before.getYear().yearReduction()) { 221 if((date_before.getYear().isLeapYear() 222 && date_before.getMonth().getValue() > 2 ) 223 ||(!date_before.getYear().isLeapYear() 224 && date_before.getYear().isLeapYear(date_before.getYear().getValue()-1) 225 && date_before.getMonth().getValue() < 3)) 226 i-=366; 227 else i -= 365; 228 } 229 for(;i > 0;i--){ 230 if(date_before.getMonth().getValue() == 1 && date_before.day.getValue() == 1) { 231 date_before.getYear().yearReduction(); 232 date_before.getMonth().resetMax(); 233 date_before.resetMax(); 234 }else if(date_before.getMonth().getValue() == 3 && date_before.day.getValue() == 1) { 235 date_before.getMonth().setValue(2); 236 if(date_before.getYear().isLeapYear()) 237 date_before.day.setValue(29); 238 else 239 date_before.day.setValue(28); 240 }else if(date_before.day.getValue() == 1){ 241 date_before.getMonth().monthReduction(); 242 date_before.resetMax(); 243 }else 244 date_before.day.dayReduction(); 245 } 246 return date_before; 247 } 248 249 public int getDaysofDates(DateUtil date) {//求兩日期之間的天數(shù) 250 int numOfdays = 0; 251 DateUtil temp = new DateUtil(); 252 if(!this.compareDates(date)){ 253 temp.day.setValue(date.day.getValue()); 254 temp.getMonth().setValue(date.getMonth().getValue()); 255 temp.getYear().setValue(date.getYear().getValue()); 256 date.day.setValue(this.day.getValue()); 257 date.getMonth().setValue(this.getMonth().getValue()); 258 date.getYear().setValue(this.getYear().getValue()); 259 this.day.setValue(temp.day.getValue()); 260 this.getMonth().setValue(temp.getMonth().getValue()); 261 this.getYear().setValue(temp.getYear().getValue()); 262 263 } 264 for(;date.getYear().getValue() > this.getYear().getValue() + 1;) { 265 if(this.getYear().isLeapYear() && this.getMonth().getValue() < 3 ) 266 numOfdays += 366; 267 else numOfdays += 365; 268 this.getYear().yearIncrement(); 269 } 270 int i ; 271 for( i = 0;this.compareDates(date); i++){ 272 if(this.getMonth().getValue() == 12 && this.day.getValue() == 31) { 273 this.getYear().yearIncrement(); 274 this.resetMin(); 275 this.getMonth().resetMin(); 276 }else if(this.getMonth().getValue() == 2) { 277 if((this.getYear().isLeapYear()&& day.getValue() == 29) 278 || (!this.getYear().isLeapYear() && day.getValue() == 28)){ 279 this.resetMin(); 280 this.getMonth().monthIcrement(); 281 }else 282 day.dayIncrement(); 283 }else if(this.day.getValue() == this.mon_maxnum[getMonth().getValue()]) { 284 this.resetMin(); 285 this.getMonth().monthIcrement(); 286 }else 287 this.day.dayIncrement(); 288 } 289 numOfdays += i; 290 return numOfdays; 291 } 292 } 293 class Year { 294 private int value; 295 296 public Year(int value) { 297 super(); 298 this.value = value; 299 } 300 301 public Year() { 302 super(); 303 // TODO Auto-generated constructor stub 304 } 305 306 public int getValue() { 307 return value; 308 } 309 310 public void setValue(int value) { 311 this.value = value; 312 } 313 314 public boolean isLeapYear() {//判斷是否潤年 315 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 316 return true; 317 }else 318 return false; 319 } 320 321 public boolean validate() {//校驗(yàn)數(shù)據(jù)合法性 322 if(value >= 1820 && value <= 2020) { 323 return true; 324 } 325 else return false; 326 } 327 328 public void yearIncrement() {//年份增一 329 value++; 330 } 331 332 public void yearReduction() {//年份減一 333 value--; 334 } 335 336 public boolean isLeapYear(int value) { 337 if((value % 4 == 0 && value % 100 != 0) || value % 400 == 0){ 338 return true; 339 }else 340 return false; 341 } 342 } 343 class Month { 344 private int value; 345 346 public Month(int value) { 347 super(); 348 this.value = value; 349 } 350 351 public Month() { 352 super(); 353 // TODO Auto-generated constructor stub 354 } 355 356 public int getValue() { 357 return value; 358 } 359 360 public void setValue(int value) { 361 this.value = value; 362 } 363 364 public void resetMin() { 365 this.value = 1; 366 } 367 368 public void resetMax() {//月份設(shè)置為12 369 this.value = 12; 370 } 371 372 public boolean validate() {//校驗(yàn)數(shù)據(jù)合法性 373 if(value >= 1 && value <= 12) 374 return true; 375 else return false; 376 } 377 378 public void monthIcrement() { 379 this.value++; 380 } 381 382 public void monthReduction() { 383 this.value--; 384 } 385 } 386 class Day { 387 private int value; 388 389 public Day() { 390 super(); 391 // TODO Auto-generated constructor stub 392 } 393 394 public Day(int value) { 395 super(); 396 this.value = value; 397 } 398 399 public int getValue() { 400 return value; 401 } 402 403 public void setValue(int value) { 404 this.value = value; 405 } 406 407 public void dayIncrement() {//增一 408 value++; 409 } 410 411 public void dayReduction() {//減一 412 value--; 413 } 414 415 }
該題類圖:

3.踩坑心得:
由于這三題解決的問題一樣,故一起總結(jié):這是在老師的框架下,編寫的程序。
02:這題由于主函數(shù)已經(jīng)編寫完成,所以只能寫一個(gè)類予以補(bǔ)充,結(jié)構(gòu)上類似于c語言寫法,寫起來比較容易。
03:這題類之間是一條鏈?zhǔn)剑瑐€(gè)人覺得這樣是不合理的類間關(guān)系,導(dǎo)致寫法上表示一個(gè)數(shù)很復(fù)雜,不過基本上02題寫好,03只是修改一下。
04:這題類間結(jié)構(gòu)比較合理,也是只需在03題上基礎(chǔ)做改動(dòng)即可。
在寫求下n天和上n天時(shí)由于分支結(jié)構(gòu)比較復(fù)雜,在還未設(shè)計(jì)完全就著手寫,導(dǎo)致寫出來的代碼繁雜,于是修修補(bǔ)補(bǔ),在重新理清楚分支關(guān)系后重新完成。由于如果一天一天的推算,當(dāng)輸入數(shù)據(jù)較大比如10000天,會(huì)導(dǎo)致pta計(jì)算超時(shí),于是就需要以年為單位刪除相應(yīng)天數(shù),于是問題又出現(xiàn),平年和閏年的天數(shù)不同。可能是pta老師未設(shè)置該測(cè)試點(diǎn)。判斷扣除的天數(shù)是365還是366不單單是指考慮當(dāng)年是否為閏年這么簡(jiǎn)單,比如2016年是閏年,但2016年3月1日到2017年3月1日只有365天,也就是說是否在2月29(28)日前成為了判斷標(biāo)志(往前算的情況同)。當(dāng)然,如果年份比較多,那么這一天會(huì)被抵消,比如你2016年3月1日往后推一年多算一天,那么本該多算的2015/3/1到2016/3/1日會(huì)少算一天,也就是總的來說,不管如何天數(shù)最多相差1,但由于之前并未考慮該情況,于是在自己測(cè)試的時(shí)候,思考了非常久。該題還有兩個(gè)測(cè)試點(diǎn),便是測(cè)試兩日期之間的天數(shù),最后開始我默認(rèn)是前面日期要先于后面日期,于是該測(cè)試點(diǎn)就導(dǎo)致我的算法推算一直無法結(jié)束,導(dǎo)致運(yùn)行占用內(nèi)存過大(我也不知道為什么不是超時(shí)),起先我認(rèn)為是我算法的問題導(dǎo)致申請(qǐng)內(nèi)存過多,但發(fā)現(xiàn)并沒有多余申請(qǐng)的數(shù)據(jù),甚至數(shù)組都沒有新申請(qǐng)過,換了好幾種寫法依然如此,于是對(duì)照室友寫法,發(fā)現(xiàn)他多考慮到先后日期的差別,但是在前兩題并未出現(xiàn)該測(cè)試點(diǎn)(因?yàn)槲疫^了)所以我便加了一段判斷日期先后的步驟,然后就過了。。。所以還是閱讀題目的問題,不能先入為主。
存在問題:總是忘記先設(shè)置邊界值,測(cè)試的時(shí)候總是出現(xiàn)類似于數(shù)組越界的問題。在區(qū)分閏年和平年2月的時(shí)候,寫出了太多重復(fù)可能性的代碼。
4.收獲:
1.對(duì)eclipse的調(diào)試功能更加熟悉,通過eclipse的快捷創(chuàng)建方法的功能,節(jié)約大量寫重復(fù)代碼的時(shí)間。
eclipse調(diào)試功能中檢查變量也是極其方便,一些沒有的變量但卻想知道數(shù)據(jù),可以在表達(dá)式中輸入也能得出結(jié)果。

一些快捷創(chuàng)建類創(chuàng)建方法的功能也可以幫助程序員節(jié)約編寫重復(fù)必要代碼的時(shí)間。

而且代碼的錯(cuò)誤eclipse也提供了一些快捷解決方法,想要快速找到方法只需要按住ctrl然后單擊方法名便可快速跳轉(zhuǎn)到該方法所在位置。

而且eclipse的功能框都是可以獨(dú)立操控,位置和大小均可隨自己調(diào)動(dòng)。
2.pta上測(cè)試點(diǎn)有大量的邊界值測(cè)試,在我們寫代碼時(shí)需要多考慮到一些有可能超過邊界的情況。在測(cè)試時(shí)避免出現(xiàn)數(shù)組越界等類似問題。然后就是輸出格式,在pta上即使計(jì)算正確,但是正確的輸出輸入格式也是經(jīng)常令我頭疼的地方。特別是判定輸入格式是否正確方面,需要對(duì)數(shù)據(jù)進(jìn)行初步檢測(cè),提取有用數(shù)據(jù),然后對(duì)這些數(shù)據(jù)合法性進(jìn)行檢測(cè),這一步也占有許多測(cè)試點(diǎn)。在進(jìn)行數(shù)據(jù)合法性檢測(cè)時(shí),并未考慮到輸入負(fù)數(shù)的情況出現(xiàn)。老師給的類圖也在我編寫程序的時(shí)候給了我極大啟發(fā),比如數(shù)據(jù)加一減一用方法實(shí)現(xiàn)。跨年,跨月時(shí)數(shù)據(jù)的改動(dòng)也可以使用方法實(shí)現(xiàn)。由于方法名字有其本身意義,也讓的程序的行為變得一眼可見,在檢查程序的是后可以省下大量時(shí)間的重新思考。月份對(duì)應(yīng)最大值也可以不分平年閏年,因?yàn)樵跈z測(cè)年月日的分支結(jié)構(gòu)中,2月是被特殊考慮的,也就不存在爭(zhēng)議。有少數(shù)情況會(huì)出現(xiàn)eclipse上可以運(yùn)行的代碼,轉(zhuǎn)到PTA上卻無法輸出,也許時(shí)編譯器包容性比較強(qiáng),這時(shí)可以在cmd指令窗口運(yùn)行代碼,有時(shí)候也能報(bào)出錯(cuò)誤,雖然不理解原理,但這一招屢試不爽。而cmd上可以運(yùn)行的代碼基本上pta也能運(yùn)行。
3.在對(duì)類間關(guān)系的設(shè)計(jì)上經(jīng)過此次練習(xí)也得到了啟發(fā)。MCV模式是被大多數(shù)程序員認(rèn)可的類間關(guān)系設(shè)計(jì)。也就是以后設(shè)計(jì)一大功能的時(shí)候,輸入數(shù)據(jù)的檢測(cè)判定和初步提取可以寫作一個(gè)類,對(duì)提取好的數(shù)據(jù)進(jìn)行計(jì)算可以寫成一個(gè)類,輸出展示也可寫為一個(gè)類,最后寫一個(gè)類負(fù)責(zé)和這幾種類“交涉”,這樣可以大大降低類間耦合性,代碼的也就可復(fù)用易維護(hù)。而我之所以認(rèn)為三種類間關(guān)系第三種比較合理,是因?yàn)槟暝氯崭髯缘姆椒ǘ际峭ㄟ^DateUtil來管理的,當(dāng)然我還認(rèn)為可以專門寫一個(gè)類Intermediary(中介),讓DateUtil這個(gè)處理數(shù)據(jù)的類與年月日解耦,Displaly(展示)也可以寫成一個(gè)類,降低主函數(shù)的繁雜從程度,最后main函數(shù)只要執(zhí)行中介類的功能。這是我目前能想到更為合理的解決方案。
4.學(xué)習(xí)到了許多實(shí)用的方法,例如:
// 包裝類的Integer中parseInt(String str)(double等同理)可以把字符串轉(zhuǎn)換為對(duì)應(yīng)的基本類型數(shù)字,valueOf也有相似功能,不過返回的是對(duì)應(yīng)基本類型包裝類的對(duì)象。在使用該方法的時(shí)候,經(jīng)常報(bào)錯(cuò),原因是在對(duì)數(shù)據(jù)處理的時(shí)候總是無法保證輸入的字符串是對(duì)應(yīng)的基本類型的樣子。
// String類中(以下的String表示一個(gè)String類的對(duì)象)
(1) String.substring()方法可以截取字符串,返回一個(gè)String對(duì)象。并且有不同用法輸入一個(gè),在保證不越界的情況下,輸入一個(gè)(int index),表示截取該索引值之后的字符串,而(int index1,int index2)則是截取index1 到index2的字符串,左開右閉。
(2) String.trim()方法可以刪除字符串內(nèi)的空格,返回一個(gè)字符串類型對(duì)象。
(3) String.valueOf();該方法可以將基本數(shù)據(jù)類型轉(zhuǎn)化為String型對(duì)象。
(4) String.length().返回字符串長度,返回值為整形
(5) String.charAt(int index).可以返回索引值為index的字符,類型為char,一般用此方法遍歷字符串。
(6) String.indexOf(String str);可以找到字符串str在String中出現(xiàn)的第一個(gè)索引值String.indexOf(String str,int index),表示從index開始搜尋。
最后我強(qiáng)烈建議!!!在pta作業(yè)結(jié)束后可以公開測(cè)試點(diǎn)!因?yàn)橛袝r(shí)候直到最后都沒辦法通過一些測(cè)試點(diǎn),但又不知道是哪里的問題。這樣可以起到類似數(shù)學(xué)作業(yè)中錯(cuò)題的作用,以后便可最大程度規(guī)避該類錯(cuò)誤!
好啦!這大概就是這個(gè)月通過三次作業(yè)的學(xué)習(xí)成果了,寥寥幾千字,確實(shí)n夜抓耳撓腮換來的,禿了呀!而到現(xiàn)在為止也可能只是登堂入室把,后期想要進(jìn)步還需不斷努力呀!

浙公網(wǎng)安備 33010602011771號(hào)