JAVA题目集4~6的总结
前言:
本次博客主要是总结一下有关JAVA课程学习和作业的相关问题与心得。已经上课学习JAVA两个月左右啦,又做了三次大作业,总体来说,这三次的大作业题目还是很有难度的,题量也蛮大,还是能在规定时间内写完的,但不保证准确率呀。下面举例具体分析我学会的和遇到的问题吧。
7-2 日期问题面向对象设计(聚合一) (35 分)
参考题目7-2的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1900,2050] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
- 当输入有误时,输出格式如下:
Wrong Format- 当第一个数字为1且输入均有效,输出格式如下:
year-month-day- 当第一个数字为2且输入均有效,输出格式如下:
year-month-day- 当第一个数字为3且输入均有效,输出格式如下:
天数值输入样例1:
在这里给出一组输入。例如:
3 2014 2 14 2020 6 14结尾无空行输出样例1:
在这里给出相应的输出。例如:
2312结尾无空行输入样例2:
在这里给出一组输入。例如:
2 1935 2 17 125340结尾无空行输出样例2:
在这里给出相应的输出。例如:
1591-12-17结尾无空行输入样例3:
在这里给出一组输入。例如:
1 1999 3 28 6543结尾无空行输出样例3:
在这里给出相应的输出。例如:
2017-2-24结尾无空行输入样例4:
在这里给出一组输入。例如:
0 2000 5 12 30结尾无空行输出样例4:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
踩坑心得:
一开始想走捷径,想将之前写过的一次下N天的算法直接放进本次作业之中,发现无法得到满分,最致命的是我一直看不出错误。后来我索性重新写了一遍算法。

后来,我得到了这样一个答案:
class DateUtil{
Day day;
//默认构造方法
public DateUtil(){
}
//带参构造方法
public DateUtil(int d,int m,int y){
this.day=new Day(d,m,y);
}
//getter
public Day getDay(){
return day;
}
//setter
public void setDay(Day d){
this.day=d;
}
//效验数据合法性
public boolean checkInputValidity(){
if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())
return true;
else
return false;
}
//比较两个日期大小
public boolean compareDates(DateUtil date) {
if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
return false;
else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
return false;
else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
return false;
else
return true;
}
//判定两个日期是否相等
public boolean equalTwoDates(DateUtil date){
if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
return true;
else
return false;
}
//日期值格式化
public String showDate(){
return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
}
//计算该年的剩余天数
public int syts(DateUtil d){
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b=0,i;
for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
b=b+a[i];
}
b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年
b++;
return b;
}
public void DaysLater(int year,int month,int day) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
int d=0,m=0;
if(this.getDay().getMonth().getYear().isLeapYear())
arr[2] = 29;
if(month==12) {
if(day==arr[month]) {
year = year+1;
m = 1;
d=1;
}
else{
m=month;
d =day +1;
}
}
else {
if(day==arr[month]) {
m = month + 1;
d = 1;
}
else{
m=month;
d = day+1;
}
}
}
public void DaysAgo(int year,int month,int day,int days) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.getDay().getMonth().getYear().isLeapYear())
arr[2] = 29;
int a = 0,b = 0,c = 0;
if(days > 0) {
if(days < day) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 1) {
a = year -1;
b = 12;
c = arr[b] - days + day;
}
else {
a = year;
b = month - 1;
c = arr[b] - days + day;
}
}
}
else if(days == 0) {
a = year;
b = month;
c = day;
}
else if (days < 0){
if(day - days <= arr[month]) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 12) {
a = year + 1;
b = 1;
c = day - days - arr[month];
}
else {
a = year;
b = month + 1;
c = day - days - arr[month];
}
}
}
}
//求下n天
public DateUtil getNextNDays(int n){
int a[]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int y=0,m=0,d=0;
int i,j;
int b=syts(this);//该年剩余天数
if(b>n){//该年剩余天数大于n
y=this.getDay().getMonth().getYear().getValue();
if(!this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
a[2]=28;
}
int e=a[this.getDay().getMonth().getValue()];//该月的天数
e=e-this.getDay().getValue();//本月剩余的天数
if(e>=n){//如果n天后在本月
m=this.getDay().getMonth().getValue();
d=n+this.getDay().getValue();
}
else{//如果n天后不在本月
n=n-e;
m=this.getDay().getMonth().getValue()+1;
i=m;
while(n-a[i]>0 && i<=12){//找到月
n=n-a[i];
m++;
i++;
}
d=n;//找到天
}
}
else{//该年剩余天数小于n
n=n-b;
y=this.getDay().getMonth().getYear().getValue()+1;
int c=365;//平年天数
if(new Year(y).isLeapYear()){//闰年天数
c += 1;
}
while(n-c>0){//找到年
n=n-c;
y++;
c=365;
if(new Year(y).isLeapYear())
c += 1;
}
for(i = 1;n-a[i]>0&&i<=12;i++){//找到月
n=n-a[i];
}
m=i;
d=n;//找到天
}
return new DateUtil(y, m, d);
}
//求前n天
public DateUtil getPreviousNDays(int n){
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y=0;
int m=0;
int d=0;
int i,b;
b=365-syts(this);//该日期所在年份已经过的天数
if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
b++;
}
if (b>n){//如果前n天在该年
y=this.getDay().getMonth().getYear().getValue();
int e=this.getDay().getValue();//本月已经过的天数
if(e>n){//如果前n天在本月
m=this.getDay().getMonth().getValue();
d=e-n;
}
else{//如果前n天不在本月
n=n-e;
m=this.getDay().getMonth().getValue()-1;
i=m;
while(n-a[i]>0&&i>=0){//找到月
n=n-a[i];
m--;
i--;
}
d=a[i]-n;//找到天
if(new Year(y).isLeapYear()&&m==2){
d++;
}
}
}
else{//如果前n天不在该年
n=n-b;
y=this.getDay().getMonth().getYear().getValue()-1;
int f=365;
if(new Year(y).isLeapYear()){
f++;
}
while(n-f>0){//找到年
n=n-f;
y--;
f=365;
if(new Year(y).isLeapYear())
f++;
}
for(i=12;n-a[i]>0&&i>=0;i--){//找到月
n=n-a[i];
}
m=i;
d=a[i]-n;//找到天
if(new Year(f).isLeapYear()&&m==2){
d++;
}
}
return new DateUtil(y, m, d);
}
//求两个日期之间的天数
public int getDaysofDates(DateUtil date){
DateUtil b1=this;
DateUtil b2=date;
if(this.equalTwoDates(date)){//如果两天的日期相等
return 0;
}
else if(!this.compareDates(date)){//如果日期大小不对
b1=date;
b2=this;
}
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,ts=0;
i=b1.getDay().getMonth().getYear().getValue()+1;
while(i<b2.getDay().getMonth().getYear().getValue()){//两个日期的年数之和
ts=ts+365;
if(new Year(i).isLeapYear())
ts++;
i++;
}
if(b1.getDay().getMonth().getYear().getValue() == b2.getDay().getMonth().getYear().getValue()){
if(b1.getDay().getMonth().getValue() == b2.getDay().getMonth().getValue()) {//年份相同,月份相同,日不同
ts = b2.getDay().getValue() - b1.getDay().getValue();
}
}
else if(b1.getDay().getMonth().getYear().getValue() == b2.getDay().getMonth().getYear().getValue()) {
if (b1.getDay().getMonth().getValue() != b2.getDay().getMonth().getValue()) {//年份相同,月份不同
if (b1.getDay().getMonth().getYear().isLeapYear())//是闰年
a[2] = 29;
ts += a[b1.getDay().getMonth().getValue()] - b1.getDay().getValue();//小日期该月剩余的天数
ts += b2.getDay().getValue();//大日期的天数
j = b1.getDay().getMonth().getValue() + 1;
while (j <= b2.getDay().getMonth().getValue() - 1) {//月份天数和
ts += a[j];
j++;
}
}
}
else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){//年份不同
ts += a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数
ts += b2.getDay().getValue();//大日期在该月已经过的天数
for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)//小日期在该年剩余的天数
ts=ts+a[j];
for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)//大日期在该年已经过的天数
ts=ts+a[j];
if(b1.getDay().getMonth().getYear().isLeapYear()) {
if (b1.getDay().getMonth().getValue() <= 2) {//如果小日期该年为闰年且该天在1月或2月
ts++;
}
}
else if(b2.getDay().getMonth().getYear().isLeapYear()) {//如果大日期该年为闰年且该天在1月或2月后
if (b2.getDay().getMonth().getValue() > 2)
ts++;
}
}
return ts;
}
}

import java.util.Scanner;
//Year类
class Year{
int value;
//默认构造方法
public Year(){
}
//带参构造方法
public Year(int value){
this.value=value;
}
//getter
public int getValue(){
return value;
}
//setter
public void setValue(int value){
this.value=value;
}
//判断year是否为闰年
public boolean isLeapYear(){
boolean isLeapYear;
isLeapYear = ((value%4==0&&value%100!=0) || value%400==0);
return isLeapYear;
}
//效验数据合法性
public boolean validate(){
boolean validate;
validate = (value<=2050&&value>=1900);
return validate;
}
//年份加一
public void yearIncrement(){
value=value+1;
}
//年份减一
public void yearReduction(){
value=value-1;
}
}
//Month类
class Month{
int value;
Year year;
//默认构造方法
public Month(){
}
//带参构造方法
public Month(int yearValue,int monthValue){
this.year=new Year(yearValue);
this.value=monthValue;
}
//getter
public int getValue(){
return value;
}
public Year getYear(){
return year;
}
//setter
public void setValue(int value){
this.value=value;
}
public void setYear(Year year){
this.year=year;
}
//日期复位(1)
public void resetMin(){
value=1;
}
//月份设置为12
public void resetMax(){
value=12;
}
//效验数据合法性
public boolean validate(){
boolean validate;
validate = (value>=1&&value<=12);
return validate;
}
//月份加一
public void dayIncrement(){
value=value+1;
}
//月份减一
public void dayReduction(){
value=value-1;
}
}
//Day类
class Day{
int value;
Month month;
int a[]={31,28,31,30,31,30,31,31,30,31,30,31};
//默认构造方法
public Day(){
}
//带参构造方法
public Day(int yearValue,int monthValue,int dayValue){
this.month=new Month(yearValue,monthValue);
this.value=dayValue;
}
//getter
public int getValue(){
return value;
}
public Month getMonth(){
return month;
}
//setter
public void setValue(int value){
this.value=value;
}
public void setMonth(Month value){
this.month=value;
}
//日期复位(1)
public void resetMin(){
value=1;
}
//日期设为该月最大值
public void resetMax(){
value=a[month.getValue()-1];
}
//效验数据合法性
public boolean validate(){
if(this.getMonth().getYear().isLeapYear())
a[1]=29;
if(value>=1&&value<=a[month.getValue()-1])
return true;
else
return false;
}
//日期加一
public void dayIncrement() {
value=value+1;
}
//日期减一
public void dayReduction() {
value=value-1;
}
}
//DateUtil类
class DateUtil{
Day day;
//默认构造方法
public DateUtil(){
}
//带参构造方法
public DateUtil(int d,int m,int y){
this.day=new Day(d,m,y);
}
//getter
public Day getDay(){
return day;
}
//setter
public void setDay(Day d){
this.day=d;
}
//效验数据合法性
public boolean checkInputValidity(){
if(this.getDay().getMonth().getYear().validate()&&this.getDay().getMonth().validate()&&day.validate())
return true;
else
return false;
}
//比较两个日期大小
public boolean compareDates(DateUtil date) {
if(date.getDay().getMonth().getYear().getValue()<this.getDay().getMonth().getYear().getValue())
return false;
else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()<this.getDay().getMonth().getValue())
return false;
else if(date.getDay().getMonth().getYear().getValue()==this.getDay().getMonth().getYear().getValue()&&date.getDay().getMonth().getValue()==this.getDay().getMonth().getValue()&&date.getDay().getValue()<this.getDay().getValue())
return false;
else
return true;
}
//判定两个日期是否相等
public boolean equalTwoDates(DateUtil date){
if(this.getDay().getValue()==date.getDay().getValue()&&this.getDay().getMonth().getValue()==date.getDay().getMonth().getValue()&& this.getDay().getMonth().getYear().getValue()==date.getDay().getMonth().getYear().getValue())
return true;
else
return false;
}
//日期值格式化
public String showDate(){
return this.getDay().getMonth().getYear().getValue()+"-"+this.getDay().getMonth().getValue()+"-"+this.getDay().getValue();
}
//计算该年的剩余天数
public int syts(DateUtil d){
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int b=0,i;
for(i=d.getDay().getMonth().getValue()+1;i<=12;i++){
b=b+a[i];
}
b=b+a[d.getDay().getMonth().getValue()]-d.getDay().getValue();
if(d.getDay().getMonth().getYear().isLeapYear()&&d.getDay().getMonth().getValue()<=2)//闰年
b++;
return b;
}
public void DaysLater(int year,int month,int day) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
int d=0,m=0;
if(this.getDay().getMonth().getYear().isLeapYear())
arr[2] = 29;
if(month==12) {
if(day==arr[month]) {
year = year+1;
m = 1;
d=1;
}
else{
m=month;
d =day +1;
}
}
else {
if(day==arr[month]) {
m = month + 1;
d = 1;
}
else{
m=month;
d = day+1;
}
}
}
public void DaysAgo(int year,int month,int day,int days) {
int[] arr=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(this.getDay().getMonth().getYear().isLeapYear())
arr[2] = 29;
int a = 0,b = 0,c = 0;
if(days > 0) {
if(days < day) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 1) {
a = year -1;
b = 12;
c = arr[b] - days + day;
}
else {
a = year;
b = month - 1;
c = arr[b] - days + day;
}
}
}
else if(days == 0) {
a = year;
b = month;
c = day;
}
else if (days < 0){
if(day - days <= arr[month]) {
a = year;
b = month;
c = day - days;
}
else {
if(month == 12) {
a = year + 1;
b = 1;
c = day - days - arr[month];
}
else {
a = year;
b = month + 1;
c = day - days - arr[month];
}
}
}
}
//求下n天
public DateUtil getNextNDays(int n){
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y=0,m=0,d=0;
int i,j;
int b=syts(this);//该年剩余天数
if(b>n){//该年剩余天数大于n
y=this.getDay().getMonth().getYear().getValue();
if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
a[2]=29;
}
int e=a[this.getDay().getMonth().getValue()];//该月的天数
e=e-this.getDay().getValue();//本月剩余的天数
if(e>=n){//如果n天后在本月
m=this.getDay().getMonth().getValue();
d=n+this.getDay().getValue();
}
else{//如果n天后不在本月
n=n-e;
m=this.getDay().getMonth().getValue()+1;
i=m;
while(n-a[i]>0 && i<=12){//找到月
n=n-a[i];
m++;
i++;
}
d=n;//找到天
}
}
else{//该年剩余天数小于n
n -= b;
y=this.getDay().getMonth().getYear().getValue()+1;
int c=365;//平年天数
if(new Year(y).isLeapYear()){//闰年天数
c += 1;
}
while(n-c>0){//找到年
n=n-c;
y++;
c=365;
if(new Year(y).isLeapYear())
c ++;
}
for(i = 1;n-a[i]>0&&i<=12;i++){//找到月
n=n-a[i];
}
m=i;
d=n;//找到天
}
return new DateUtil(y, m, d);
}
//求前n天
public DateUtil getPreviousNDays(int n){
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int y=0;
int m=0;
int d=0;
int i,b;
b=365-syts(this);//该日期所在年份已经过的天数
if(this.getDay().getMonth().getYear().isLeapYear()){//如果是闰年
b++;
}
if (b>n){//如果前n天在该年
y=this.getDay().getMonth().getYear().getValue();
int e=this.getDay().getValue();//本月已经过的天数
if(e>n){//如果前n天在本月
m=this.getDay().getMonth().getValue();
d=e-n;
}
else{//如果前n天不在本月
n=n-e;
m=this.getDay().getMonth().getValue()-1;
i=m;
while(n-a[i]>0&&i>=0){//找到月
n=n-a[i];
m--;
i--;
}
d=a[i]-n;//找到天
if(new Year(y).isLeapYear()&&m==2){
d++;
}
}
}
else{//如果前n天不在该年
n=n-b;
y=this.getDay().getMonth().getYear().getValue()-1;
int f=365;
if(new Year(y).isLeapYear()){
f++;
}
while(n-f>0){//找到年
n=n-f;
y--;
f=365;
if(new Year(y).isLeapYear())
f++;
}
for(i=12;n-a[i]>0&&i>=0;i--){//找到月
n=n-a[i];
}
m=i;
d=a[i]-n;//找到天
if(new Year(f).isLeapYear()&&m==2){
d++;
}
}
return new DateUtil(y, m, d);
}
//求两个日期之间的天数
public int getDaysofDates(DateUtil date){
DateUtil b1=this;
DateUtil b2=date;
if(this.equalTwoDates(date)){//如果两天的日期相等
return 0;
}
else if(!this.compareDates(date)){//如果日期大小不对
b1=date;
b2=this;
}
int a[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,j,ts=0;
i=b1.getDay().getMonth().getYear().getValue()+1;
while(i<b2.getDay().getMonth().getYear().getValue()){//两个日期的年数之和
ts=ts+365;
if(new Year(i).isLeapYear())
ts++;
i++;
}
if(b1.getDay().getMonth().getYear().getValue() == b2.getDay().getMonth().getYear().getValue()){
if(b1.getDay().getMonth().getValue() == b2.getDay().getMonth().getValue()) {//年份相同,月份相同,日不同
ts = b2.getDay().getValue() - b1.getDay().getValue();
}
}
else if(b1.getDay().getMonth().getYear().getValue() == b2.getDay().getMonth().getYear().getValue()) {
if (b1.getDay().getMonth().getValue() != b2.getDay().getMonth().getValue()) {//年份相同,月份不同
if (b1.getDay().getMonth().getYear().isLeapYear())//是闰年
a[2] = 29;
ts += a[b1.getDay().getMonth().getValue()] - b1.getDay().getValue();//小日期该月剩余的天数
ts += b2.getDay().getValue();//大日期的天数
j = b1.getDay().getMonth().getValue() + 1;
while (j <= b2.getDay().getMonth().getValue() - 1) {//月份天数和
ts += a[j];
j++;
}
}
}
else if(b1.getDay().getMonth().getYear().getValue()!=b2.getDay().getMonth().getYear().getValue()){//年份不同
ts += a[b1.getDay().getMonth().getValue()]-b1.getDay().getValue();//小日期在该月剩余的天数
ts += b2.getDay().getValue();//大日期在该月已经过的天数
for(j=b1.getDay().getMonth().getValue()+1;j<=12;j++)//小日期在该年剩余的天数
ts=ts+a[j];
for(j=b2.getDay().getMonth().getValue()-1;j>0;j--)//大日期在该年已经过的天数
ts=ts+a[j];
if(b1.getDay().getMonth().getYear().isLeapYear()) {
if (b1.getDay().getMonth().getValue() <= 2) {//如果小日期该年为闰年且该天在1月或2月
ts++;
}
}
else if(b2.getDay().getMonth().getYear().isLeapYear()) {//如果大日期该年为闰年且该天在1月或2月后
if (b2.getDay().getMonth().getValue() > 2)
ts++;
}
}
return ts;
}
}
//主类
public class Main {
public static void main(String[] args) {
Scanner x=new Scanner(System.in);
int year=0;
int month=0;int day=0;
int a,b;
a=x.nextInt();//输入判断类型
year=x.nextInt();month= x.nextInt();day=x.nextInt();//输入年月日
DateUtil c=new DateUtil(year,month,day);
if(a > 0 && a < 4){
switch(a){
case 1:
b=x.nextInt();//输入n
if(!c.checkInputValidity()||b<0){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getNextNDays(b).showDate());
System.exit(0);
case 2:
b=x.nextInt();//输入n
if(!c.checkInputValidity()||b<0){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getPreviousNDays(b).showDate());
System.exit(0);
case 3:
int y1,m1,d1;
y1=x.nextInt();m1= x.nextInt();d1=x.nextInt();//输入第二个年月日
DateUtil d=new DateUtil(y1,m1,d1);
if(!c.checkInputValidity()||!d.checkInputValidity()){//如果数据不合法
System.out.println("Wrong Format");
System.exit(0);
}
else
System.out.println(c.getDaysofDates(d));
System.exit(0);
default:
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
下面是第二个日期聚合,我想老师说的题目集5的7-4应该是7-5才对。
7-5 日期问题面向对象设计(聚合二) (40 分)参考题目7-3的要求,设计如下几个类:DateUtil、Year、Month、Day,其中年、月、日的取值范围依然为:year∈[1820,2020] ,month∈[1,12] ,day∈[1,31] , 设计类图如下:
应用程序共测试三个功能:
- 求下n天
- 求前n天
- 求两个日期相差的天数
注意:严禁使用Java中提供的任何与日期相关的类与方法,并提交完整源码,包括主类及方法(已提供,不需修改)
输入格式:
有三种输入方式(以输入的第一个数字划分[1,3]):
- 1 year month day n //测试输入日期的下n天
- 2 year month day n //测试输入日期的前n天
- 3 year1 month1 day1 year2 month2 day2 //测试两个日期之间相差的天数
输出格式:
- 当输入有误时,输出格式如下:
Wrong Format- 当第一个数字为1且输入均有效,输出格式如下:
year1-month1-day1 next n days is:year2-month2-day2- 当第一个数字为2且输入均有效,输出格式如下:
year1-month1-day1 previous n days is:year2-month2-day2- 当第一个数字为3且输入均有效,输出格式如下:
The days between year1-month1-day1 and year2-month2-day2 are:值输入样例1:
在这里给出一组输入。例如:
3 2014 2 14 2020 6 14结尾无空行输出样例1:
在这里给出相应的输出。例如:
The days between 2014-2-14 and 2020-6-14 are:2312结尾无空行输入样例2:
在这里给出一组输入。例如:
2 1834 2 17 7821结尾无空行输出样例2:
在这里给出相应的输出。例如:
1834-2-17 previous 7821 days is:1812-9-19结尾无空行输入样例3:
在这里给出一组输入。例如:
1 1999 3 28 6543结尾无空行输出样例3:
在这里给出相应的输出。例如:
1999-3-28 next 6543 days is:2017-2-24结尾无空行输入样例4:
在这里给出一组输入。例如:
0 2000 5 12 30结尾无空行输出样例4:
在这里给出相应的输出。例如:
Wrong Format结尾无空行
踩坑心得:
其实导致这两个错误的不过是一个很小很小的变量而已,所以大家千万千万要仔细。

import java.util.Scanner;
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 DateUtil(){
}
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;
}
private final int[] arr = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
private int getDayOfMonth(int year, int month) {
int days;
days = arr[month - 1];
if (month == 2) {
if (!isLeapYear(year)) {
days = 28;
}
}
return days;
}
public boolean isLeapYear(int year)//判断year是否为闰年
{
boolean isLeapYear;
isLeapYear = ((year % 4 == 0 && year % 100 !=0 )||year % 400 == 0);
return isLeapYear;
}
public boolean checkInputValidity()//检测输入的年、月、日是否合法
{
if (year < 1820)
return false;
else if (year > 2020)
return false;
if (month < 1)
return false;
else if (month > 12)
return false;
if(day > getDayOfMonth(year, month))
return false;
else if(day < 1)
return false;
boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=arr[month-1]&&day>0);
return checkInputValidity;
}
public DateUtil getNextNDays(int n)//取得year-month-day的下n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
if(n > 0){
for (int i = 0; i < n; i++) {
day++;
if (day > getDayOfMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}}
else if(n == 0){
year = year;
month =month;
day = day;
}
else if(n < 0){
int p = -n;
int i = 0;
while(i < p) {
day--;
while (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day += getDayOfMonth(year, month);
}
i++;
}
}
return new DateUtil(year, month, day);
}
public DateUtil getPreviousNDays(int n)//取得year-month-day的前n天日期
{
int year = this.year;
int month = this.month;
int day = this.day;
if(n > 0){
int i = 0;
while(i < n) {
day--;
while (day < 1) {
month--;
if (month < 1) {
month = 12;
year--;
}
day += getDayOfMonth(year, month);
}
i++;
}}
else if(n == 0){
year = year;
month =month;
day = day;
}
else if(n < 0){
int p = -n;
for (int i = 0; i < p; i++) {
day++;
if (day > getDayOfMonth(year, month)) {
day = 1;
month++;
if (month > 12) {
month = 1;
year++;
}
}
}
}
return new DateUtil(year, month, day);
}
public boolean compareDates(DateUtil date)//比较当前日期与date的大小(先后)
{
if (this.year > date.year)
return true;
else if (this.year == date.year&&this.month > date.month) {
if (this.month > date.month)
return true;
if (this.month == date.month&&this.day >= date.day) {
if (this.day >= date.day)
return true;
}
}
return false;
}
public boolean equalTwoDates(DateUtil date)//判断两个日期是否相等
{
if (date != null) {
if (year == date.year) {
if (month == date.month) {
if (day == date.day) {
return true;
}
}
}
}
return false;
}
private static final int[] mon = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
public int getDaysofDates(DateUtil date)//求当前日期与date之间相差的天数
{
DateUtil dateUtil1 = this; // 小
DateUtil dateUtil2 = date; // 大
if (this.compareDates(date)) {
dateUtil1 = date;
dateUtil2 = this;
}
int days;
int leapYearNum = 0;
for (int i = dateUtil1.getYear(); i < dateUtil2.getYear(); i++) {
if (isLeapYear(i)) {
leapYearNum++;
}
}
days = 365 * (dateUtil2.getYear() - dateUtil1.getYear()) + leapYearNum;
int d1;
d1 = mon[dateUtil1.getMonth() - 1] + dateUtil1.getDay() + (dateUtil1.getMonth() > 2 && isLeapYear(dateUtil1.getYear()) ? 1 : 0);
int d2;
d2 = mon[dateUtil2.getMonth() - 1] + dateUtil2.getDay() + (dateUtil2.getMonth() > 2 && isLeapYear(dateUtil2.getYear()) ? 1 : 0);
return days - d1 + d2;
}
public String showDate()//以“year-month-day”格式返回日期值
{
return year + "-" + month + "-" + day;
}
}
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 > 0 && choice < 4) {
switch (choice) {
case 1:
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.showDate() + " next " + m + " days is:");
System.out.println(date.getNextNDays(m).showDate());
break;
case 2:
int n = 0;
year = Integer.parseInt(input.next());
month = Integer.parseInt(input.next());
day = Integer.parseInt(input.next());
DateUtil date1 = new DateUtil(year, month, day);
if (!date1.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(date1.showDate() + " previous " + n + " days is:");
System.out.println(date1.getPreviousNDays(n).showDate());
break;
case 3:
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;
fromDate = new DateUtil(year, month, day);
DateUtil toDate;
toDate = new DateUtil(anotherYear, anotherMonth, anotherDay);
if (fromDate.checkInputValidity() && toDate.checkInputValidity()) {
System.out.print("The days between " + fromDate.showDate() + " and " + toDate.showDate() + " are:");
System.out.println(fromDate.getDaysofDates(toDate));
}
else {
System.out.println("Wrong Format");
System.exit(0);
}
break;
default:
System.out.println("Wrong Format");
System.exit(0);
}
}
else{
System.out.println("Wrong Format");
System.exit(0);
}
}
}
7-3 图形继承 (15 分)
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
- 类Shape,无属性,有一个返回0.0的求图形面积的公有方法
public double getArea();//求图形面积- 类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
- 类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
- 类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法
public double getVolume();//求球体积- 类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法
public double getVolume();//求立方体体积- 注意:
- 每个类均有构造方法,且构造方法内必须输出如下内容:
Constructing 类名- 每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
- 输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出
Wrong Format输入格式:
共四种合法输入
- 1 圆半径
- 2 矩形宽、长
- 3 球半径
- 4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
输入样例1:
在这里给出一组输入。例如:
1 1.0结尾无空行输出样例1:
在这里给出相应的输出。例如:
Constructing Shape Constructing Circle Circle's area:3.14结尾无空行输入样例2:
在这里给出一组输入。例如:
4 3.6 2.1 0.01211结尾无空行输出样例2:
在这里给出相应的输出。例如:
Constructing Shape Constructing Rectangle Constructing Box Box's surface area:15.26 Box's volume:0.09结尾无空行输入样例3:
在这里给出一组输入。例如:
2 -2.3 5.110结尾无空行输出样例2:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
设计与分析:
主要就是使用类的继承。
复杂度:

源代码:
import java.util.Scanner;
class Shape //定义一个无自身属性,有一个返回值为0.0的求面积方法
{
public Shape()
{
System.out.println("Constructing Shape");
}
public double getArea()
{
return 0.0;
}
}
class Circle extends Shape//继承自Shape
{
public Circle()
{
System.out.println("Constructing Circle");//圆形
}
private double radius;//新定义一个半径
public void setRadius(double radius) {// 设置半径
this.radius = radius;
}
public double getRadius() {// 获取半径
return radius;
}
public double getArea() {//重写父类的方法
// TODO Auto-generated method stub
double area;
area = Math.PI*radius*radius;
return area;
}
}
class Rectangle extends Shape
{
public Rectangle()
{
System.out.println("Constructing Rectangle");//长方形
}
private double width;
private double length;
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getArea() {
// TODO Auto-generated method stub
double area;
area = width*length;
return area;
}
}
class Ball extends Circle
{
public Ball()
{
System.out.println("Constructing Ball");
}
public double getArea() {
// TODO Auto-generated method stub
double area;
area = 4.0*super.getArea();//方法的重载,super关键字
return area;
}
public double getVolume()
{
double r2=getRadius();
double volume = 4.0/3.0*r2*r2*r2*Math.PI;
return volume;
}
}
class Box extends Rectangle
{
public Box()
{
System.out.println("Constructing Box");
}
private double height;
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getVolume()
{
double volume = height*super.getArea();
return volume;
}
public double getArea() {
// TODO Auto-generated method stub
double w2=getWidth();
double l2=getLength();
double area;
area = 2*(w2*l2+w2*height+l2*height);
return area;
}
}
public class Main {
public static void main(String[] args) {
int Type;
Scanner scanner=new Scanner(System.in);
Type=scanner.nextInt();
if(Type < 1 || Type > 4){
System.out.println("Wrong Format");
}
else {
switch (Type) {
case 1:
double r = scanner.nextDouble();
if (r > 0.0) {
Circle circle = new Circle();
circle.setRadius(r);
System.out.print("Circle's area:");
System.out.println(String.format("%.2f", circle.getArea()));
}
else
System.out.println("Wrong Format");
break;
case 2:
double width = scanner.nextDouble();
double length = scanner.nextDouble();
if (width <= 0.0) {
System.out.println("Wrong Format");
}
else if (length <= 0.0) {
System.out.println("Wrong Format");
}
else {
Rectangle rectangle = new Rectangle();
rectangle.setLength(length);
rectangle.setWidth(width);
System.out.print("Rectangle's area:");
System.out.println(String.format("%.2f", rectangle.getArea()));
}
break;
case 3:
double r2 = scanner.nextDouble();
if (r2 <= 0.0) {
System.out.println("Wrong Format");
}
else {
Ball ball = new Ball();
ball.setRadius(r2);
System.out.print("Ball's surface area:");
System.out.println(String.format("%.2f", ball.getArea()));
System.out.print("Ball's volume:");
System.out.println(String.format("%.2f", ball.getVolume()));
}
break;
case 4:
double width2 = scanner.nextDouble();
double length2 = scanner.nextDouble();
double height = scanner.nextDouble();
if (width2 > 0.0) {
if(length2 > 0.0){
if(height > 0.0){
Box box = new Box();
box.setHeight(height);
box.setWidth(width2);
box.setLength(length2);
System.out.print("Box's surface area:");
System.out.println(String.format("%.2f", box.getArea()));
System.out.print("Box's volume:");
System.out.println(String.format("%.2f", box.getVolume()));
}
else
System.out.println("Wrong Format");
}
else
System.out.println("Wrong Format");
}
else
System.out.println("Wrong Format");
break;
}
}
}
}
7-5 图形继承与多态 (50 分)
掌握类的继承、多态性及其使用方法。具体需求参见作业指导书。
输入格式:
从键盘首先输入三个整型值(例如a b c),分别代表想要创建的Circle、Rectangle及Triangle对象的数量,然后根据图形数量继续输入各对象的属性值(均为实型数),数与数之间可以用一个或多个空格或回车分隔。
输出格式:
- 如果图形数量非法(小于0)或图形属性值非法(数值小于0以及三角形三边关系),则输出
Wrong Format。- 如果输入合法,则正常输出,输出内容如下(输出格式见输入输出示例):
- 各个图形的面积;
- 所有图形的面积总和;
- 排序后的各个图形面积;
- 再次所有图形的面积总和。
输入样例1:
在这里给出一组输入。例如:
1 1 1 2.3 3.2 3.2 6.5 3.2 4.2结尾无空行输出样例1:
在这里给出相应的输出。例如:
Original area: 16.62 10.24 5.68 Sum of area:32.54 Sorted area: 5.68 10.24 16.62 Sum of area:32.54结尾无空行输入样例2:
在这里给出一组输入。例如:
0 2 2 2.3 2.5 56.4 86.5 64.3 85.6 74.6544 3.2 6.1 4.5结尾无空行输出样例2:
在这里给出相应的输出。例如:
Original area: 5.75 4878.60 2325.19 7.00 Sum of area:7216.54 Sorted area: 5.75 7.00 2325.19 4878.60 Sum of area:7216.54结尾无空行输入样例3:
在这里给出一组输入。例如:
0 0 1 3 3 6结尾无空行输出样例3:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
踩坑心得:
边界值永远都要注意!

import java.util.Scanner;
import java.util.Arrays;
//Shape类
abstract class Shape1{
public abstract double Area();
public abstract boolean validate();
public abstract String toString();
}
//Circle类
class Circle1 extends Shape1{
private double r;
public Circle1(double r){
super();
this.r=r;
}
//getter
public double getR(){
return r;
}
//setter
public void setR(double r){
this.r = r;
}
public double Area(){//计算圆面积
double s;
s = Math.PI*r*r;
return s;
}
public boolean validate(){
return true;
}
public String toString(){
return null;
}
}
//Rectangle类
class Rectangle1 extends Shape1{
private double a, b;
public Rectangle1(double a, double b){
super();
this.a=a;
this.b=b;
}
//getter
public double A(){
return a;
}
public double B(){
return b;
}
//setter
public void setA(double a){
this.a=a;
}
public void setB(double b){
this.b=b;
}
public double Area(){//计算矩形面积
double c;
c = a*b;
return c;
}
public boolean validate(){
return true;
}
public String toString(){
return null;
}
}
//Triangle类
class Triangle1 extends Shape1{
private double a, b, c;
public Triangle1(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
//getter
public double A(){
return a;
}
public double B(){
return b;
}
public double C(){
return c;
}
//setter
public void setA(double a){
this.a=a;
}
public void setB(double b){
this.b=b;
}
public void setC(double c){
this.c=c;
}
public double Area(){//计算三角形面积
double s1;
s1 = Math.sqrt((a+b+c)/2*((a+b+c)/2-a)*((a+b+c)/2-b)*((a+b+c)/2-c));
return s1;
}
public boolean validate(){
return true;
}
public String toString(){
return null;
}
}
//主类
public class Main{
public static void main(String[] args){
Scanner x = new Scanner(System.in);
int m, n, o, gs=0,h;//个数
m = x.nextInt();
n = x.nextInt();
o = x.nextInt();
if(m < 0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
if(n < 0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
if(o < 0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
h = m + n + o;
double[] e = new double [m+n+o];
double z = 0;
int i ;
int flag = 0;
Shape1 []c = new Circle1[m];
Shape1 []r = new Rectangle1[n];
Shape1 []t = new Triangle1[o];
for(i = 0;i < m; i++){//三角形
double bj;
bj = x.nextDouble();//输入半径
if(bj <= 0){//判断是否合法
flag = 1;
}
c[i] = new Circle1(bj);//把半径传入Circle类
z = z +c [i].Area();//计算总面积
e[gs] = c[i].Area();//把面积传入数组he
gs++;
}
for(i = 0;i < n;i++){//矩形
double db;
double cb;
db = x.nextDouble();
cb = x.nextDouble();//输入边长
if(db < 0){//判断是否合法
flag = 1;
}
if(cb < 0){//判断是否合法
flag = 1;
}
r[i] = new Rectangle1(db,cb);//把边长传入Rectangle类
z = z + r[i].Area();//计算总面积
e[gs] = r[i].Area();//把面积传入数组he
gs++;
}
for(i = 0; i < o; i++){//三角形
double a, b, d;
double[] three=new double[3];
three[0] = x.nextDouble();
three[1] = x.nextDouble();
three[2] = x.nextDouble();//输入边长
Arrays.sort(three);//三边从小到大排序
a = three[0];
b = three[1];
d = three[2];
if(a < 0){//判断是否合法
flag = 1;
}
if(b < 0){//判断是否合法
flag = 1;
}
if(d < 0){//判断是否合法
flag = 1;
}
if(a + b <= d){//判断是否合法
flag = 1;
}
t[i] = new Triangle1(a,b,d);//把边长传入Triangle类
z = z + t[i].Area();//计算总面积
e[gs] = t[i].Area();//把面积传入数组he
gs++;
}
if(flag==1){//如果输入的不合法
System.out.println("Wrong Format");
System.exit(0);
}
else{
System.out.println("Original area:");
i=0;
while(i<h){
System.out.printf("%.2f ",e[i]);
i++;
}
System.out.printf("\nSum of area:%.2f\n",z);
System.out.println("Sorted area:");
Arrays.sort(e);
i=0;
while(i<h){
System.out.printf("%.2f ",e[i]);
i++;
}
System.out.printf("\nSum of area:%.2f",z);
}
}
}
7-6 实现图形接口及多态性 (30 分)
编写程序,使用接口及类实现多态性,类图结构如下所示:
其中:
- GetArea为一个接口,无属性,只有一个GetArea(求面积)的抽象方法;
- Circle及Rectangle分别为圆类及矩形类,分别实现GetArea接口
- 要求:在Main类的主方法中分别定义一个圆类对象及矩形类对象(其属性值由键盘输入),使用接口的引用分别调用圆类对象及矩形类对象的求面积的方法,直接输出两个图形的面积值。(要求只保留两位小数)
输入格式:
从键盘分别输入圆的半径值及矩形的宽、长的值,用空格分开。
输出格式:
- 如果输入的圆的半径值及矩形的宽、长的值非法(≤0),则输出
Wrong Format- 如果输入合法,则分别输出圆的面积和矩形的面积值(各占一行),保留两位小数。
输入样例1:
在这里给出一组输入。例如:
2 3.6 2.45结尾无空行输出样例1:
在这里给出相应的输出。例如:
12.57 8.82结尾无空行输入样例2:
在这里给出一组输入。例如:
9 0.5 -7.03结尾无空行输出样例2:
在这里给出相应的输出。例如:
Wrong Format
结尾无空行
这道题是在7-5的基础上使用类接口并调用即可完成题目要求。
import java.util.Scanner;
import java.util.Arrays;
//GetArea类
interface GetArea1{
public double getArea();
}
//Circle类
class Circle1 implements GetArea1{
private double r;
//创建无参构造方法
public Circle1(){
}
//创建带参构造方法
public Circle1(double r){
this.r=r;
}
//getter
public double getR(){
return r;
}
//setter
public void setR(double r){
this.r = r;
}
//计算圆面积
public double getArea(){
double area;
area = Math.PI*r*r;
return area;
}
}
//Rectangle类
class Rectangle1 implements GetArea1{
private double a,b;
//创建无参构造方法
public Rectangle1(){
}
//创建带参构造方法
public Rectangle1(double a, double b){
this.a=a;
this.b=b;
}
//getter
public double A(){
return a;
}
public double B(){
return b;
}
//setter
public void setA(double a){
this.a=a;
}
public void setB(double b){
this.b=b;
}
//计算矩形面积
public double getArea(){
double area;
area = a*b;
return area;
}
}
class Triangle1 implements GetArea1{
private double a, b, c;
public Triangle1(double a, double b, double c){
this.a = a;
this.b = b;
this.c = c;
}
//getter
public double A(){
return a;
}
public double B(){
return b;
}
public double C(){
return c;
}
//setter
public void setA(double a){
this.a=a;
}
public void setB(double b){
this.b=b;
}
public void setC(double c){
this.c=c;
}
public double getArea(){//计算三角形面积
double s1;
s1 = Math.sqrt((a+b+c)/2*((a+b+c)/2-a)*((a+b+c)/2-b)*((a+b+c)/2-c));
return s1;
}
}
//主类
public class Main{
public static void main(String[] args){
Scanner x = new Scanner(System.in);
double j,c,k;
double l,p;
j=x.nextDouble();
c=x.nextDouble();
k=x.nextDouble();
if(j<=0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
else if(c<=0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
else if(k<=0){//如果不合法
System.out.println("Wrong Format");
System.exit(0);
}
GetArea1 circle1;
circle1=new Circle1(j);
GetArea1 rectangle1;
rectangle1=new Rectangle1(c,k);
l=circle1.getArea();
p=rectangle1.getArea();
System.out.printf("%.2f\n",l);
System.out.printf("%.2f\n",p);
}
}
三次题目集中的正则表达式:
正则表达式定义了字符串的模式。
正则表达式可以用来搜索、编辑或处理文本。
正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
在 Java 中,\\ 表示:我要插入一个正则表达式的反斜线,所以其后的字符具有特殊的意义。
在 Java 中正则表达式中则需要有两个反斜杠才能被解析为其他语言中的转义作用。也可以简单的理解在 Java 的正则表达式中,两个 \\ 代表其他语言中的一个 \,这也就是为什么表示一位数字的正则表达式是 \\d,而表示一个普通的反斜杠是 \\。
反正正则表达式要学通还是有一定难度的。
改进建议:
我写的代码大多数都是参照题目中所给出的类结构和方法,有一些写得十分复杂,绕绕弯弯,还请大佬们给提出意见。对课堂模式的看法与建议:我们的课堂采取的是线上线下相结合的模式。线上需通过mooc自学相关Java内容,老师根据题目的完成情况讲产生的学各种知识, 这种课堂模式非常具有创新性,大大增强了课堂效率。烙上在课堂上也会让我们带着电脑,当场出些小题目,让我们编写,过程十分刺激。当然我也了解到代码的编写过程一定要严谨,数据的处理也是十分重要的 ,一定要去测试代码,测试代码输入的合理性,这也是题目涉及到的非法输入的测试点。 但很多东西需要自学,这样也养成了自学的习惯和学会翻阅资料等。做题只靠老师讲的知识 远远不够,需要我们自己去网上查找资料。
总结;
这几次主要是学了类的封装,继承和多态。怎么说呢,JAVA还是不简单的,甚至有点很难哦。所以,加油吧!朋友们!



浙公网安备 33010602011771号