前三次题目及集总结

发布时间 2023-03-26 18:39:02作者: zjwby

 前言:前三次pta实验让我了解到了基本的java入门知识和基本语法,也让我能够有一定的时间去适应从C语言到java的转变,也让我够感觉到面向过程和面向对象的不同之处,总体来说前三次pta实验让我对java有了初步的了解。      

                    

题目集一:

主要知识点就是熟悉Scanner类 import java.util.Scanner就是Java的一些基本语法,由于老师不讲语法,通过这些题目巩固了我的Java语法;  第一次PTA总共题量较多,总共有十二阿道题目,有些题比较简单,有些题目有一定的难度,需要有很强的数学知识和逻辑能力,第一次实验知道了java有基础的,也有很难得,难的需要自己去探索,动手取编写代码

计算年利率到身体质量指数(BM)),测算再到九九乘法表(双重循环),快递运费,去掉重复的字符,统计一个子串在整串中出现的次数,有重复的数据,从一个字符串中移除包含在另一个字符串中的字符,Prime Numbers, GPS数据处理,求定积分,列出最简真分数序列

题目集二:
从简单的输入输出逐步接触真正的面向对象,学习到对象、字符串,一些西方公示的实质等知识。第二次pta实验考察内容主要是java中的字符串以及数组并旦考察了在一个类中设计多种方法(可返回的,不可返回的,第一次接触的布尔型的)。此次作业正式引入了类与方法的概念,也表明我们正式进入到ava中的学习当中来。本次作业布置了九道题目,题目总体题量多,难度中等。7-1 长度质量计量单位换算7-2-奇数求和
7-3房产税费计算20227-4 游戏角色选择7-5 学号识别7-6 巴比伦法求平方根近似值7-7 二进制数值提取7-8 判断三角形类型7-9 求下一天。这些题目复杂程度较上一次题目有所提高,需要一定的逻辑思维,通过第二次作业能够对第三次实验使用类打下一定的基础,让我了解了类的基本使用方式。

题目集三:

这次的作业主要涉及到类、对象等知识,从第三次作业就开始从面向过程到面向对象,主要针对面向对象,知识点有如何设计对象并使用,封装,this关键词,构造方法等还要学学习类的调用,以及类之间的关系,用不同的类关系来解决同一个问题。本次作业布置了四道题,题目总体题量不多,难度较高。7-1 创建圆形类7-2 创建账户类Account7-3定义日期类7-4 日期类设计。其中7-3定义日期类是第二次作业求7-9下一天的迭代,7-4日期类设计又是7-3定义日期类的迭代,随着类与方法的增加,设计日期的功能性逐渐完善。此次pta实验题量少,但是难度大需要仔细全面的思考各种情况

                          第一次题目集

1、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int year = in.nextInt();
double rate=0.077;
if (year<1) rate=rate*0.5*100;
else if(1<year&&year<3) rate=rate*0.7*100;
else if(year<=5 && year>3) rate=rate*100;
else if(year>5) rate=rate*1.1*100;
else if(year<0) System.out.println("error\n");
System.out.println("实际利率="+rate+"%");

}
}

心得:简单的数字运算,控制输出位数即可

2、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double weight,high,BMI;
weight=in.nextDouble();
high=in.nextDouble();
BMI=weight/(high*high);
if(weight>727||high>2.72||weight<0) System.out.println("input out of range");
else if (BMI<18.5)System.out.println("thin");
else if (BMI>=18.5&&BMI<24)System.out.println("fit");
else if (BMI>=24&&BMI<28) System.out.println("overweight");
else if(BMI>=28)System.out.println("fat");

}
}

心得:计算出bmi的值,通过if-else判断bmi各种值所对应的情况,然后再输出对应的文字

3、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int i,j;
int n;
Scanner in=new Scanner(System.in);
n=in.nextInt();
if(n<1||n>9) System.out.println("INPUT ERROR.");
else if (true) {for (i=1;i<=n;i++)
{for (j=1;j<=i;j++)
{if(i==j)
System.out.print(i+"X"+j+"="+j*i);
else if(j<i)
{System.out.print(i+"X"+j+"="+j*i+"\t");}

} System.out.println();

}

}


}
}

心得:简单的二重for循环,非法输入时特定输出,注意口诀表的格式(空格,乘法的格式)

4、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
double weight;
double m=0;
Scanner in=new Scanner(System.in);
weight= in.nextDouble();
if(weight>=1.0&&weight<20.0)
{ m=12+2*(weight-1);}
else if(weight>=20&&weight<60)
{
m=39+1.9*(weight-20);}
else m=115+1.3*(weight-60);


System.out.println ((int)(m+0.5));


}
}

心得:还是简单的对输入数据进行基本的数学运算,然后根据对应的情况输出对应的结果即可

5、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
String []a = new String[s.length()];
for (int i =0;i<s.length();i++){
a[i] = s.substring(i,i+1);
}
for (int i =0;i<s.length();i++) {
for (int j =i+1;j<s.length();j++){
if(a[j].equals(a[i])){
a[j] = "";
}
}
}
for (int i =0;i<s.length();i++) {
System.out.print(a[i]);
}
}
}

 

心得:使用substring函数去截取字符,实现任意数量的字符的输出,由于substring函数之前未使用用过,查询了用法之后,然后实现了需求

6、

源码:

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.nextLine();
String s2=sc.next();
int index=0;
int count=0;
while(true)
{
index=s1.indexOf(s2);
if(index!=-1)
{
count++;
s1=s1.substring(index+s2.length());
}
else
{
break;
}
}
System.out.println(count);
}

}

心得:输出重复的字符,先判断什么字符重复,然后在设置一个计数器,每次重复的时候及记录数据最后输出即可

7、

源码:

import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []a=new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Arrays.sort(a);
if(n==1)System.out.print("NO");
else{
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1]){
System.out.print("YES");
break;
}
if(i==n-2)System.out.print("NO");
}
}
}
}

心得:开始一直显示运行超时,开始采用的是暴力查询,也有使用二分查询,因为暴力查询时间慢,最开始我没排序,还以为自己做的还挺对,其实逻辑就有问题,后来排序之后,直接判断。

8、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String f=in.nextLine();
String []a = new String[f.length()];
String s=in.nextLine();
String []b = new String[s.length()];
for (int i =0;i<f.length();i++){
a[i] = f.substring(i,i+1);
}
for (int j =0;j<s.length();j++){
b[j] = s.substring(j,j+1);
}
for (int j =0;j<s.length();j++)
for (int i =0;i<f.length();i++)
if (a[i].equals(b[j])) a[i] = "";

for (int i =0;i<f.length();i++)
{ System.out.print(a[i]);}


}
}

心得:先是要找出第一个字符中含有与第二个字符串相同的字符,也是使用substring函数去截取字符,然后输出对应的字符

9、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int m, n, sum = 0;
Scanner in = new Scanner(System.in);
m = in.nextInt();
n = in.nextInt();
while (m != n) {
if (is_prime(m))
sum = sum + m;
m++;
}
System.out.print(sum);


}


public static boolean is_prime ( int x)
{
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;


}
}

心得:

10、

源码:

import java.util.Scanner;

public class Main{

private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
String str=null,time = null;
while (in.hasNext()) {
str=in.nextLine();
if (str.equals("END")) {
break;
}
int sum=0;//$*之间字符的异或值
String []split=str.split(",");
if(split[0].equals("$GPRMC")) {
int end=str.indexOf("*");
for(int i=1;i<end;i++) {
sum^=(int)str.charAt(i);
}
sum%=65536;
Boolean flag=split[2].equals("A");
Boolean S=sum==Integer.parseInt(str.substring(end+1),16) ;
if(S==true&&flag==true) {
time=split[1];
}
}
}
if(time!=null) {
int h=(Integer.parseInt(time.substring(0, 2))+8)%24;
String m=time.substring(2, 4);
String s=time.substring(4, 6);
if(h<10) System.out.print("0");
System.out.print(h+":"+m+":"+s);
}
}
}

心得:找出$GPRMC语句,计算校验和,找出其中校验正确,并且字段2表示已定位的语句,从中计算出时间,换算成北京时间。一次数据中会包含多条$GPRMC语句,以最后一条语句得到的北京时间作为结果输出。

11、

源码:暂无

心得:对微积分了解不足,导致程序难以实现

12、

源码:

import java.util.Scanner;

public class Main {
public static int num(int x,int y){
int z,a;
if(x<y) { z=x;x=y;y=z;}
while (x%y!=0)
{
a=x%y;
x=y;
y=a;
}
return y;

}
public static void main(String[] args) {
int n, i = 0;
Scanner in = new Scanner(System.in);
n = in.nextInt();
for (i = 1; i < n; i++) {
if (num(i, n) == 1) System.out.printf("%d/%d,", i, n);


}

}
}

 

心得:

对分子采用穷举发,利用最大公约数的方法,判断分子与40是否构成真分数。

首先穷举n以内的全部分子,由于分子为1时此分式必为最简分式,所以要加if判断,若分子不是1,采用辗转相除发求出最大公约数至此程序结束。

 

                            第二次题目集

1、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
double weight= input.nextDouble();
double length= input.nextDouble();
double yweight=weight/0.45359237;
double ylength=length/0.0254;
System.out.print((float)yweight +" "+(float)ylength);

 

 

}
}

心得:简单的对输入数据进行基本的数学运算,然后根据对应的情况输出对应的结果即可

2、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int []a=new int [10];
int sum=0;
Scanner in=new Scanner(System.in);
for(int i=0;i<a.length;i++) {
a[i] = in.nextInt();
if (a[i]%2==1||a[i]%2==-1) {
sum = sum + a[i];
}
}
System.out.print(sum);
}
}

心得:首先是数组存入数据,然后遍历数组找出输入的奇数,然后计算和即可

3、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int n=0,price=0,guess_price=0;
double square=0;
Scanner in=new Scanner(System.in);
n=in.nextInt();
price =in.nextInt();
guess_price=in.nextInt();
square=in.nextDouble();
double qs=0,yhs=0,jyf=0,chf=0;
if(n==1&&square<=90) qs=100*price;
else if(n==1&&square>90&&square<=144) qs=150*price;
else if(n>1||square>144)qs=300*price;
yhs=5*price;
jyf=3*square;
chf=1.36*square;
System.out.println((float)qs+" "+(float) yhs
+" "+(float) jyf+" "+(float) chf);

 

}
}

心得:根据各种税之间的对应关系,计算结果即可,都是正常的数学计算,不过要理清楚各种税和房子面积的关系。

4、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
int m,n;
Scanner in=new Scanner(System.in);
m=in.nextInt();
n=in.nextInt();
if(m<0||m>4||n<0||n>3)
{System.out.println("Wrong Format");
return;}
switch(m)
{ case 1:
System.out.print("人类"+" ");break;
case 2:
System.out.print("精灵"+" ");break;
case 3:
System.out.print("兽人"+" ");break;
case 4:
System.out.print("暗精灵"+" ");break;

}

switch (n){case 1:
System.out.print("战士");break;
case 2:
System.out.print("法师");break;
case 3:
System.out.print("射手");break;}


}
}

心得:使用switch case 实现条件判断,对应设置好输出即可

5、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {

Scanner in=new Scanner(System.in);
String str=in.next();
if(str.length()!=8|| !str.startsWith("01", 2)&&
!str.startsWith("02", 2)&&
!str.startsWith("03", 2)&&
!str.startsWith("20", 2))
{ System.out.println("Wrong Format");
return;}
System.out.println("入学年份:20"+str.substring(0,2) +"年");
if(str.startsWith("01", 2)) System.out.println("学院:材料学院");
else if(str.startsWith("02", 2)) System.out.println("学院:机械学院");
else if(str.startsWith("03", 2)) System.out.println("学院:外语学院");
else System.out.println("学院:软件学院");
System.out.println("班级:"+str.substring(4,6));
System.out.print("学号:"+str.substring(6,8));

}
}

 

心得:通过if else 判断学生第五第六位学号是否是01 02 03 20 ,如果是其中之一那就输出对应的信息,如果不是,则输出Wrong Format

6、

源码:

import java.util.*;
public class Main{
public static void main(String args[]){
Scanner in=new Scanner(System.in);
float n=in.nextFloat();
float lastGuess=in.nextFloat();
if(n<0||lastGuess<=0)
{System.out.println("Wrong Format");
return;}
float nextGuess=(lastGuess+n/lastGuess)/2;
while(Math.abs(nextGuess-lastGuess)>=0.00001){
lastGuess=nextGuess;
nextGuess=(lastGuess+n/lastGuess)/2;
}
System.out.print((float)lastGuess);
}
}

心得:

7、

源码:


import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
String a=in.nextLine();

for(int i=0;i<a.length();i++) {
if (a.charAt(i)=='0' || a.charAt(i)=='1')
System.out.print(a.charAt(i));
else{ if(a.charAt(i)=='-')
{if (a.charAt(i+1)=='1')
System.out.print("");
return;}
}
}
System.out.print("Wrong Format");
}
}

 

心得:

8、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
double a= in.nextDouble();
double b= in.nextDouble();
double c= in.nextDouble();
if(a>200||a<1||b>200||b<1||c>200||c<1)
{System.out.println("Wrong Format");
return;}
else if(a+b<c||b+c<a||c+a<b){
System.out.println("Not a triangle");
return;
}
else if(a==b&&a==c) {
System.out.println("Equilateral triangle");
} else if (a==b||b==c||c==a) {
System.out.println("Isosceles triangle");

} else if (a*a+b*b==c*c||b*b+c*c==a*a||a*a+c*c==a*a) {
System.out.println("Right-angled triangle");

} else if (a+b>c||b+c>a||a+c>b) {
System.out.println("General triangle");

}

}
}

心得:

重点在于利用if语句进行判断,但是在等腰直角三角形的判断中,会与直角三角形以及等腰三角形的判断重叠,所以需要多重判断来隔开来,在判断直角三角形,使用勾股定理时存在精度问题,计算机只能通过a*a+b*b-c*c<0.1的近似值来进行判断。

首先进行三次输入对应三角形的三条边长,如果三条边满足判断能够组成三角形,即两边之和长度应大于第三边或者两边只差长度应小于第三边,若不满足,即输出:Not a triangle;接下来则判断是否为等边三角形,即三条边长长度相等,若相等,即输出:Equilateral triangle;若不相等,则继续判断。若两边长度相等,且三边满足勾股定理,则输出:Isosceles right-angled triangle;若仅是长度相等,则输出:Isosceles triangle;若只是满足勾股定理,则输出:Right-angled triangle;在判断数据合法的前提下,其他情况输出;General triangle;若任意一条边不满足题目要求即输出:Wrong Format;至此程序结束。

9、

 

源码:

import java.util.Scanner;

public class Main {

public static boolean isLeapYear(int year) {
boolean isLeapYear = (year % 4 == 0 && year % 100 !=0 )||year % 400 == 0;
return isLeapYear;
}

public static boolean checkInputValidity(int year,int month,int day) {
int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
boolean checkInputValidity = (year>=1820&&year<=2020&&month>0&&month<=12&&day<=aa[month]&&day>0);
return checkInputValidity;
}

public static void nextDate(int year,int month,int day) {

int[] aa=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(isLeapYear(year))
aa[2] = 29;
int a = 0,b = 0,c = 0;
if(checkInputValidity(year,month,day)) {
if(month==12) {
if(day==aa[month]) {
a = year+1;
b = 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c =day +1;
}
}
if(month<12) {
if(day==aa[month]) {
a = year;
b = month + 1;
c = 1;}
if(day>0&&day<aa[month])
{a = year;
b = month;
c = day+1;
}
}
System.out.println("Next date is:"+a+"-"+b+"-"+c);
}
else System.out.println("Wrong Format");
}

public static void main(String[] args) {
Scanner LJY = new Scanner(System.in);
Main ljy = new Main();
int year = LJY.nextInt();
int month = LJY.nextInt();
int day = LJY.nextInt();
ljy.nextDate(year,month,day);

}

}

 

心得:

首先考虑类的创建,思路主要就是以题目所给的类图作为参照来写,过程分为

1.进行三次输入分别对应为年、月、日。2.创建判断输入日期是否合法的方法:年份的合法取值范围为[1900,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] ,若不符合则输出:Wrong Format;至此程序结束。3.创建求下一天的方法。4.若输入数据合法,则调用下一天的方法,至此程序结束。

求下一天的方法的创建首先要考虑是否输入的数据是符合正确年份,月份,日,若输入合法,及判断是否下一天跨了月份或者跨了年份,若未跨月和跨年,则直接day ++;输出年月日即可。若跨月,分为平年与闰年跨月,每种年分为2月跨月,1,3,5,7,8,10,12月跨月与4,6,9,11月跨月,month++;day = 1;再输出年月日即可。若跨年,则year ++;month = 1;day = 1;再输出年月日即可。

                        第三次题目集

1、

源码:

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Circle s = new Circle();
Scanner in = new Scanner(System.in);
double m = in.nextDouble();
s.setRadius(m);
s.show();
}
}

class Circle {
private double radius;
private double area;

public Circle() {
}

public double getRadius() {
return radius;
}

public void setRadius(double radius) {
this.radius = radius;

}

public void show() {
if (radius < 0) System.out.print("Wrong Format");
else {
System.out.print("The circle's radius is:");
System.out.printf("%.2f\n", radius);
System.out.print("The circle's area is:");
System.out.printf("%.2f", (Math.PI * radius * radius));
}
}

}

 

心得:创建圆类,创建成员变量,半径面积,然后通过get set方法获取私人信息,写好面积的方法在main中调用即可

2、

源码:

import java.time.LocalDate;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Account a=new Account();
Scanner in=new Scanner(System.in);

a.setId(in.nextInt());
a.setBalance(in.nextDouble());
a.setAnnualInterestRate(in.nextDouble());
double b=in.nextDouble();
double c=in.nextDouble();
a.withDraw(b);
a.deposit(c);
double newbalance;
if(!a.withDraw(b) || !a.deposit(c))
{
newbalance=a.balance;
if(a.withDraw(b))
newbalance=newbalance-b;
else
System.out.println("WithDraw Amount Wrong");

if(a.deposit(c))
newbalance=newbalance+c;
else
System.out.println("Deposit Amount Wrong");

System.out.print("The Account'balance:");
System.out.printf("%.2f\n",newbalance);
System.out.print("The Monthly interest:");
System.out.printf("%.2f\n",a.getMonthlyInterestRate(newbalance,a.annualInterestRate));
a.getDateCreated();
}

if(a.withDraw(b) && a.deposit(c))
{

newbalance=a.balance-b+c;
System.out.print("The Account'balance:");
System.out.printf("%.2f\n",newbalance);
System.out.print("The Monthly interest:");
System.out.printf("%.2f\n",a.getMonthlyInterestRate(newbalance,a.annualInterestRate));
a.getDateCreated();
}
}

static class Account {
private int id;
private double balance;
private double annualInterestRate;
public Account(){}
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}

public void setBalance(double balance) {
this.balance = balance;
}

public double getAnnualInterestRate() {
return annualInterestRate;
}

public void setAnnualInterestRate(double annualInterestRate) {
this.annualInterestRate = annualInterestRate;
}

public void getDateCreated() {
System.out.println("The Account'dateCreated:2020-07-31");
}

public double getMonthlyInterestRate(){
return balance*(annualInterestRate/1200);
}

public boolean withDraw(double balance){
return !(balance > balance) && !(balance < 0);
}
public boolean deposit(double deposit){
return !(deposit > 20000) && !(deposit < 0);
};

public double getMonthlyInterestRate(double balance, double annualInterestRate) {
double monthlyInterestRate;
return monthlyInterestRate=balance*(annualInterestRate/1200.0);
}
}
}

心得:当取钱和存钱同时超出范围时,没能实现

3、

源码:

import java.util.Random;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Date d = new Date();
d.setYear(in.nextInt());
d.setMonth(in.nextInt());
d.setDay(in.nextInt());
d.getNextDate(d.getYear(), d.getMonth(), d.getDay());

}
}

class Date {
int year;
int month;
int day;
int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Date() {

}

Date(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 isLeapYear(int year) {

return (0 == year % 4 && year % 100 != 0) || (0 == year % 400);
}

public boolean checkInputValidity(int year, int month, int day) {

int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
a[2] = 29;
return (year >= 1900 && year <= 2000 && month > 0 && month <= 12 && day <= a[month] && day > 0);

}

public void getNextDate(int year, int month, int day) {
int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (checkInputValidity(year, month, day)) {
if (isLeapYear(year)) {
if (month == 2 & day == 29) {
month = month + 1;
day = 1;
} else {
if (month == 12 && day == a[month]) {
year = year + 1;
day = 1;
month = 1;
} else if (day == a[month]) {
month = month + 1;
day = 1;
}
else day = day + 1;
}


} else {
if (month == 2 && day == 28) {
month = month + 1;
day = 1;
} else {
if (month == 12 && day == a[month]) {
year = year + 1;
day = 1;
month = 1;
} else {
if (day == a[month]) {
month = month + 1;
day = 1;
} else day = day + 1;
}

}
}
System.out.println("Next day is:" + year + "-" + month + "-" + day);


} else System.out.println("Date Format is Wrong");

}
}

心得:主要就是写下一天这个方法,首先从最大的条件年出发,分闰年和平年,然后下一天分正常下一天,和月底这一天,还有就是12月的最后一天,特别的是2月,

需要单独扔出来去写思路会比较好

4、

源码:

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( year,month,day)) {
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( year, month, day)) {
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( year, month, day) && toDate.checkInputValidity( year, month, day)) {
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 {
int year;
int month;
int day;
int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

DateUtil() {

}

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 isLeapYear(int year) {

return (0 == year % 4 && year % 100 != 0) || (0 == year % 400);
}

public boolean checkInputValidity(int year, int month, int day) {

int[] a = new int[]{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (isLeapYear(year))
a[2] = 29;
return (year >= 1820 && year <= 2020 && month > 0 && month <= 12 && day <= a[month] && day > 0);

}

 

public DateUtil getNextNDays(int n) {
while (n > 365) {
if (this.isLeapYear(year) && month <= 2) {
if (month == 2 && day == 29) {
day = 1;
month = 3;
}
year++;
n = n - 366;
} else if (this.isLeapYear(year + 1) && month > 2) {
year++;
n = n - 366;
} else {
year++;
n = n - 365;
}
}
for (int i = 0; i < n; i++) {
day++;
if (isLeapYear(year) && month == 2) {
if (day > 29) {
month = 3;
day = 1;
}
} else if (day > a[month]) {
month++;
day = 1;
if (month > 12) {
month = 1;
year++;
}

}

}


return this;

}

public DateUtil getPreviousNDays(int n) {

while (n > 365) {
if (isLeapYear(getYear()) && getMonth() > 2) {
n = n - 366;
setYear(getYear() - 1);
} else if (isLeapYear(getYear()) && getMonth() <= 2) {
n = n - 366;
setYear(getYear() - 1);
} else {
n = n - 365;
setYear(getYear() - 1);
}
}
for (int i = 0; i < n; i++) {
setDay(getDay() - 1);
if (getDay() <= 0) {
setMonth(getMonth() - 1);
if (getMonth() <= 0) {
setMonth(12);
setYear(getYear() - 1);
}
if (isLeapYear(getYear()) && month == 2)
setDay(29);
else setDay(a[getMonth()]);
}

}

return this;
}


public boolean compareDates(DateUtil date) {
if (this.year > date.getYear())
return true;
else if (this.year == date.getYear())
if (this.month > date.getMonth())
return true;
else if (this.year == date.getYear() && this.month == date.getMonth() && this.month == date.getMonth())
return this.day == date.getDay();
return false;

}

public boolean equalTwoDates(DateUtil date) {
if (this.year != date.getYear())
return false;
else if (this.month != date.getMonth())
return false;
else return this.day == date.getDay();

}

public int getDaysofDates(DateUtil date) {
int flag=0;
if(this.compareDates(date)){
while (getYear()-date.getYear()>=2){
if(isLeapYear(getYear())&&getMonth()>2)
flag=flag+366;
else flag=flag+365;
setYear(getYear()-1);
}
while (!equalTwoDates(date)) {
flag++;
setDay(getDay() - 1);
if (getDay() <= 0) {
setMonth(getMonth() - 1);
if (getMonth() <= 0) {
setMonth(12);
setYear(getYear() - 1);
}
if (isLeapYear(getYear()) && getMonth() == 2)
setDay(29);
else setDay(a[getMonth()]);
}
}
}
else {
while (date.getYear()-getYear()>=2){
if(isLeapYear(getYear())&&getMonth()<=2)
flag=flag+366;
else if(isLeapYear(getYear()+1)&&getMonth()>2)
flag=flag+366;
else flag=flag+365;
setYear(getYear()+1);
}
while (!equalTwoDates(date)) {
flag++;
setDay(getDay() + 1);
if (isLeapYear(getYear()) && getMonth() == 2) {
if (getDay() > 29) {
setMonth(getMonth() + 1);
setDay(1);
}
} else if (getDay() > a[getMonth()]) {
setMonth(getMonth() + 1);
setDay(1);
if (getMonth() > 12) {
setMonth(1);
setYear(getYear() + 1);
}
}

}

}
return flag;
}

 

public String showDate() {
return year + "-" + month + "-" + day;
}


}

类图如下:

 

 

 

心得:

此题与上一题思路相同,首先构建方法:分别构建判断年,月,日是否合法的方法

构建求下n天的方法,为了实现此功能,1.判断年,月,日是否合法,若输入合法,我们可以用for循环一天一天的增加,每一天包含一个判断的if语句,判断是否下一天跨了月份或者跨了年份,若未跨月和跨年,则直接 day++;输出年月日即可。若跨月,则month++;day =1;再输出年月日即可。若跨年,则year ++;month =1;day =1;再输出年月日即可。此时跨月与跨年的过程中分为是否闰年是否2月与是否大月。

构造求上n天方法与求下n天类似;

构造比较两个日期大小的方法,首先比年,若年相同再比月,若年与月都相同,就比日;

构造两个日期相差天数,首先构造一个计算从0年0月0日至输入日期总天数的方法,在利用此方法算两个日期相差的天数;

踩坑心得

第一次作业中的踩坑点

7-2身体质量指数(BMI)测算中,变量边界值  没写大于等于。

7-5 去掉重复的字符中,需要解决问题:当for循环遇到终止条件时将字符加入新字符串的同时继续进行新的循环

应该用到路径与continue;

7-11 求定积分  没看懂题目

 

第二次作业中的踩坑点

7-1 长度质量计量单位换算,输入的数是double型,而输出需要变为float型;

7-5 学号识别,开始写程序时我是用字符数组储存学号,但无法输出结果,所以又将号码转换成ASCLL,但由于无法调用字符数组中相邻整数型字符,最后还是用字符串与equals函数解决问题;

7-8 判断三角形类型:在实现判断条件的包含关系没有想全面

7-9求下一天,闰年重置2月天数的时候与之后的循环发生

 

第三次作业的踩坑点

7-4日期类设计:求下n天与上n天时并不是用for循环一天天递增的,而是看n的范围,首先判断是否超过此月,再判断是否超过此年,再判断超过此年多少天。但发现诸多问题,尤其是n很大时无法判断平年闰年,更无法判断二月多少天,所以逻辑行不通;

 

总结

通过这三次pta实验,让我从C语言过渡到Java,对面向对象有了一定的认知,开始步入面向对象的进程。面向对象不像面向程序,面向对象需要结合实际问题去设计各种类和方法,比较贴合实际,也是以后编程的主要语言,但是也体会到了面向对象的严格性和私密性,要比c语言更难更要求逻辑思维能力,希望自己能多做题总结。