1.前言:第一次到第三次的PTA作业大多数都不是很难,但是第三次的PTA的第3小题和第4小题比较难但也不是难以下手。题目量也不算很少,知识点也不算少(应该是我现在能力不太够)。因为我接触Java不到一个月,所以能力确实不太行,对Java也不是很了解,但之前学过一点c语言,基础的语法还是能懂的,这三周也是痛并快乐着。学习通上的课也是能勉强能听的懂。
2.设计与分析:第三次的7-3的类图(因为是从PTA复制过来的,所以main的类变成了第一题的类)
题目要求:定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] , 日期合法取值范围为[1,31] 。
注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。
代码如下:
import java.util.Scanner;
class Date{
int year;
int month;
int day;
public int getYear(int year){
return year;
}
public void setYear(){
this.year=year;
}
public int getMonth(int month){
return month;
}
public void setMonth(){
this.month=month;
}
public int getDay(int day){
return day;
}
public void setDay(){
this.day=day;
}
public static boolean checkInputValidity(int year,int month,int day) {
boolean checkInputValidity;
int[] Day = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(!isLeapYear(year))
Day[2] = 29;
checkInputValidity = (year>=1900&&year<=2000&&month>0&&month<=12&&day<=Day[month]&&day>=1);
return checkInputValidity;
}
public static boolean isLeapYear(int year){
boolean isLeapYear = false;
if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)
return isLeapYear;
return isLeapYear;
}
public void getNextDate(int year,int month,int day){
Date date = new Date();
int[] Month=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date.isLeapYear(year)){
Month[2]=29;
}
if(date.checkInputValidity(year,month,day)){
if(month == 12){
if(day == 31){
year = year+1;
month = 1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else if(year % 4 == 0&&year % 100 != 0||year % 400 == 0&&month == 2){
if(day == 29){
month = month+1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else if(month == 2){
if(day == 28){
month = month+1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else if(day == 29){
System.out.print("Date Format is Wrong");
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else{
if(day == Month[month]){
month = month+1;
day = 1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
}
else
System.out.print("Date Format is Wrong");
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
Date date = new Date();
date.getNextDate(year, month, day);
}
}
根据题目中所给的类图创建方法,也许有的并不一样,但几乎是一样的,先创造几个方法,判断输入的数据是否有效,然后判断当前年份是否为闰年,如果是闰年,则将二月的天数改为29天,最后一个月的最后一天单独提出来,也用一个方法,剩余的实际的月最后一天分别用一个方法,剩下的都用一个方法;如果不是闰月,除了二月天数不改为29,其余均和闰年一样。
第三次作业的7-4不会写,我就放不了图,也解释不了( q w q )。
其他题目的类图只有一个类,也很简单,什么都没有,所以就不放出来了(q w q)。
3.采坑心得:第一次作业提交了很多次(很多人都是这样,然后上课时就被老师说了),在提交过程中犯了很多错误,很多的语法错误,一直导致编译错误,当时还没看它给的提示,就一直看自己的代码,后来才想起来看它给的提示,但之后又出现了部分错误,发现只有几个测试点过了,又根据代码找出其中的逻辑错误,最后才一步步改正,几乎大部分的题目都是这样的,极少数的代码时一遍过的,还有几题当时觉得很难就没做。
第一次作业的7-11
最后的结果出来不是小数点后4位,然后就在网上搜解决方案才弄出来的。代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
double a = input.nextDouble();
double b = input.nextDouble();
double i = input.nextDouble();
double S = 0;
double q = (b - a) / i;
double n;
for(int k = 0;k < i;k++)
{
n=q*(a + k*q+ q/2)*(a + k*q + q/2);
S = S + n;
}
System.out.printf("%.4f",S);
}
}
第二次作业的7-1
也是最后的精度不对,后来问室友才知道要加float。代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
double kg,mi;
Scanner input = new Scanner(System.in);
kg = input.nextDouble();
mi = input.nextDouble();
double bang;
double yc;
bang = kg / 0.45359237;
yc = mi / 0.0254;
System.out.print((float)bang + " " +(float) yc);
}
}
还有第二次作业的7-4
游戏任务和职业选择,当时忘了用switch语句,导致写的代码非常繁琐,不太美观。代码如下:
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int a,b;
Scanner input = new Scanner(System.in) ;
a = input.nextInt();
b = input.nextInt();
if (a >= 1||a <= 4||b >= 1||b <= 3) {
if(a == 1&&b == 1) {
System.out.println("人类 战士");
}
else if(a == 1&&b == 2) {
System.out.println("人类 法师");
}
else if(a == 1&&b == 3) {
System.out.println("人类 射手");
}
else if(a == 2&&b == 1) {
System.out.println("精灵 战士");
}
else if(a == 2&&b == 2) {
System.out.println("精灵 法师");
}
else if(a == 2&&b == 3) {
System.out.println("精灵 射手");
}
else if(a == 3&&b == 1) {
System.out.println("兽人 战士");
}
else if(a == 3&&b == 2) {
System.out.println("兽人 法师");
}
else if(a == 3&&b == 3) {
System.out.println("兽人 射手");
}
else if(a == 4&&b == 1) {
System.out.println("暗精灵 战士");
}
else if(a == 4&&b == 2) {
System.out.println("暗精灵 法师");
}
else if(a == 4&&b == 3) {
System.out.println("暗精灵 射手");
}
else {
System.out.println("Wrong Format");
}
}
}
}
还有第三次作业的7-3和第二次作业的最后一题题目看上去差不多,所以我第一次直接把第二次作业的最后一题的代码复制上去,结果不行,(也确实不行,如果可以老师也不会出这样的题)。
4.改进建议:我有的时候代码写的过于繁琐,有的时候却又过于简单,对待作业不能草草了事,要认真检查,先把代码做出来再进行优化,要一步一步的写,不能没写完就把之前的代码改了,感觉很难的题目也不能不做,要勇于尝试。对简单的作业也不能觉得随便写写就行了。比如第一次作业的7-1
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
double APR; //基本年利率
APR = 0.077;
Scanner input = new Scanner(System.in);
double year = input.nextDouble();
if (year > 5)
System.out.println("实际利率=8.47%");
else if (year > 3)
System.out.println("实际利率=7.70%");
else if(year > 1)
System.out.println("实际利率=5.39%");
else if(year >= 0)
System.out.println("实际利率=3.85%");
else
System.out.println("error");
}
}
这道题我没用乘法就直接输出结果,当时我是觉得我直接能直接求出这结果就不需要用代码实现,但我现在看来,并不是如此,可惜现在不能更改,不然我就将那题改了。
又比如第一次作业的7-4
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double w = input.nextDouble();
int y;
if (w >= 1.0&&w < 20.0){
y =(int)(12.0 + (w - 1.0)*2+0.5);
System.out.print(y);
}
else if (w >= 20.0&&w < 60.0){
y =(int)(39.0 + (w - 20.0) * 1.9+0.5);
System.out.print(y);
}
else if (w >= 60.0){
y =(int)(115.0 + (w - 60.0)*1.3+0.5);
System.out.print(y);
}
}
}
我当时写的时候有一段的结果一直是错的,后来问室友才发现是逻辑错误导致的,然后我一直看最终发现了那一处的错误,改正之后就对了,那之后我就更懂得了向优秀的人请教问题,不能一个人“闭关锁国”。
又比如第一次作业的7-9
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int m = input.nextInt();
int n = input.nextInt();
int count = 0;
int sum = 0;
if(m >= 1&&n >= m&&n <=10000) {
for (int i = m+1;i <= n;i++) {
int k;
int isPrime = 1;
for(k = 2;k < i;k++) {
if(i%k == 0) {
isPrime = 0;
break;
}
}
if(isPrime == 1) {
count++;
sum = sum + i;
}
}
}
System.out.println(sum);
}
}
当时我并不知道素数应该怎么判断,于是我上网查,查到一点,但也不是很懂,后来就问舍友,就又会了一点,这样下去一天进步一点,以后就会好的。
第一次作业还有3个题目没有写,当时也不会写,但后来学了一点,又问了舍友才懂了一点
又比如第二次作业的7-4
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int a,b;
Scanner input = new Scanner(System.in) ;
a = input.nextInt();
b = input.nextInt();
if (a >= 1||a <= 4||b >= 1||b <= 3) {
if(a == 1&&b == 1) {
System.out.println("人类 战士");
}
else if(a == 1&&b == 2) {
System.out.println("人类 法师");
}
else if(a == 1&&b == 3) {
System.out.println("人类 射手");
}
else if(a == 2&&b == 1) {
System.out.println("精灵 战士");
}
else if(a == 2&&b == 2) {
System.out.println("精灵 法师");
}
else if(a == 2&&b == 3) {
System.out.println("精灵 射手");
}
else if(a == 3&&b == 1) {
System.out.println("兽人 战士");
}
else if(a == 3&&b == 2) {
System.out.println("兽人 法师");
}
else if(a == 3&&b == 3) {
System.out.println("兽人 射手");
}
else if(a == 4&&b == 1) {
System.out.println("暗精灵 战士");
}
else if(a == 4&&b == 2) {
System.out.println("暗精灵 法师");
}
else if(a == 4&&b == 3) {
System.out.println("暗精灵 射手");
}
else {
System.out.println("Wrong Format");
}
}
}
}
这种代码就很繁琐,当时没想起来用switch语句,就只记得一个一个列,不仅浪费时间,还会让自己好烦。当时我就很烦。现在就要记得把代码简化一点,方便后面观看。
又比如第二次作业的7-6
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
float n,lastGuess;
Scanner input = new Scanner(System.in) ;
n = input.nextFloat();
lastGuess= input.nextFloat();
if (n < 0||lastGuess <= 0) {
System.out.println("Wrong Format");
}
else {
float nextGuess;
nextGuess = (lastGuess+n/lastGuess)/2;
while (Math.abs(nextGuess - lastGuess) >= 0.00001) {
lastGuess = (float) nextGuess;
nextGuess = (lastGuess+n/lastGuess)/2;
}
System.out.print((float)lastGuess);
}
}
}
当时看题目有点复杂,就下意识认为很难,但当思路理清之后就觉得十分简单,就很快写出来了。所以不要看题目复杂就认为很难,只要有思路,没有什么有难度的,更何况我们还能上网搜某个功能应该用什么方法。
又比如第二次作业的7-9
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int year,month,day;
Scanner input = new Scanner(System.in) ;
year = input.nextInt();
month = input.nextInt();
day = input.nextInt();
if (year <= 2020&&year >=1820&&month >= 1&&month <= 12&&day <= 31&&day >=1) {
if(year % 4 == 0&&year % 100 != 0||year % 400 == 0) {
if(month == 2) {
if(day == 30||day == 31)
System.out.println("Wrong Format");
else if(day == 29)
System.out.println("Next date is:" + year + "-3-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
else if(month == 12&&day == 31)
System.out.println("Next date is:" + (year+1) + "-1-1");
else if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12) {
if(day == 31)
System.out.println("Next date is:" + year + "-" + (month+1) + "-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
else if(month == 4||month == 6||month == 9||month == 11) {
if(day == 31)
System.out.println("Wrong Format");
else if(day == 30)
System.out.println("Next date is:" + year + "-" + (month+1) + "-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
}
else {
if(month == 2) {
if(day == 30||day == 31||day == 29)
System.out.println("Wrong Format");
else if(day == 28)
System.out.println("Next date is:" + year + "-3-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
else if(month == 12&&day == 31)
System.out.println("Next date is:" + (year+1) + "-1-1");
else if(month == 1||month == 3||month == 5||month == 7||month == 8||month == 10||month == 12) {
if(day == 31)
System.out.println("Next date is:" + year + "-" + (month+1) + "-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
else if(month == 4||month == 6||month == 9||month == 11) {
if(day == 31)
System.out.println("Wrong Format");
if(day == 30)
System.out.println("Next date is:" + year + "-" + (month+1) + "-1");
else
System.out.println("Next date is:" + year + "-" + month + "-" + (day + 1));
}
}
}
else
System.out.println("Wrong Format");
}
}
写的也是比较繁琐,在特殊的天数是还单独列了出来,题目所要求的方法也没写,虽然做出来了,但还是不符合题目要求。以后要好好按照题目的要求来做题,不能认为能做出来就是对的,不然以后很容易犯错。
还有第三次作业的7-2
import java.util.Scanner;
import java.time.LocalDate;
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
Account a=new Account();
a.id=in.nextInt();
a.balance=in.nextDouble();
a.annuallnterestRate=in.nextDouble();
double withdraw=in.nextDouble();
double deposit=in.nextDouble();
double newbalance;
if(a.withDraw(withdraw)==false||a.deposit(deposit)==false)
{
newbalance=a.balance;
if(a.withDraw(withdraw)==true)
newbalance=newbalance-withdraw;
else
System.out.println("WithDraw Amount Wrong");
if(a.deposit(deposit)==true)
newbalance=newbalance+deposit;
else
System.out.println("Deposit Amount Wrong");
System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlylnteresRate(newbalance,a.annuallnterestRate)));
a.getDateCreated();
return ;
}
if(a.withDraw(withdraw)==true&&a.deposit(deposit)==true)
{
newbalance=a.balance-withdraw+deposit;
System.out.printf("The Account'balance:"+"%.2f\n",newbalance);
System.out.printf("The Monthly interest:"+"%.2f\n",(a.getMonthlylnteresRate(newbalance,a.annuallnterestRate)));
a.getDateCreated();
}
}
}
class Account{
int id;
double balance;
double annuallnterestRate;
LocalDate dateCreated;
public int getId(int id){
return id;
}
public void setId(){
this.id=id;
}
public double getBalance(double balance){
return balance;
}
public void setBalance(){
this.balance=balance;
}
public double getAnnuallnterestRate(double annuallnterestRate){
return annuallnterestRate;
}
public void setAnnuallnterestRate(){
this.annuallnterestRate=annuallnterestRate;
}
public void getDateCreated(){
System.out.println("The Account'dateCreated:2020-07-31");
}
public double getMonthlylnteresRate(double balance,double annuallnterestRate){
double monthlyInterestRate;
return monthlyInterestRate=balance*(annuallnterestRate/1200.0);
}
public boolean withDraw(double withdraw){
boolean result=true;
if(withdraw>balance||withdraw<0)
result=false;
return result;
}
public boolean deposit(double deposit){
boolean result=true;
if(deposit>20000||deposit<0)
result=false;
return result;
}
}
当时我也不知道应该怎么去写,但根据题目要求一步一步写了一点,就知道了一点,有不会的方法也在网上查,不能说非常明白,但也是比较清楚。
第三次作业的7-3
import java.util.Scanner;
class Date{
int year;
int month;
int day;
public int getYear(int year){
return year;
}
public void setYear(){
this.year=year;
}
public int getMonth(int month){
return month;
}
public void setMonth(){
this.month=month;
}
public int getDay(int day){
return day;
}
public void setDay(){
this.day=day;
}
public static boolean checkInputValidity(int year,int month,int day) {
boolean checkInputValidity;
int[] Day = new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(!isLeapYear(year))
Day[2] = 29;
checkInputValidity = (year>=1900&&year<=2000&&month>0&&month<=12&&day<=Day[month]&&day>=1);
return checkInputValidity;
}
public static boolean isLeapYear(int year){
boolean isLeapYear = false;
if(year % 4 == 0&&year % 100 != 0||year % 400 == 0)
return isLeapYear;
return isLeapYear;
}
public void getNextDate(int year,int month,int day){
Date date = new Date();
int[] Month=new int[]{0,31,28,31,30,31,30,31,31,30,31,30,31};
if(date.isLeapYear(year)){
Month[2]=29;
}
if(date.checkInputValidity(year,month,day)){
if(month == 12){
if(day == 31){
year = year+1;
month = 1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else if(year % 4 == 0&&year % 100 != 0||year % 400 == 0&&month == 2){
if(day == 29){
month = month+1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else if(month == 2){
if(day == 28){
month = month+1;
day = 1 ;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else if(day == 29){
System.out.print("Date Format is Wrong");
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
else{
if(day == Month[month]){
month = month+1;
day = 1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
else{
day = day+1;
System.out.print("Next day is:"+year+"-"+month+"-"+day);
}
}
}
else
System.out.print("Date Format is Wrong");
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int year = input.nextInt();
int month = input.nextInt();
int day = input.nextInt();
Date date = new Date();
date.getNextDate(year, month, day);
}
}
当时以为跟第二次作业的7-9差不多,但后来发现还还是不太一样,有些方法不清楚应该怎么用也上网查,也问了室友,最后勉强写了出来。
至于第三次作业的7-4当时没写出来也不好说。
5.总结:我学到了如何简单处理日期之间的关系,但更复杂的关系却很难下手,不知道改如何去做;学会了如何用各种方法的做出结果,比如用多个类做出一个简单的存储程序等等。但我还要学习怎么更好的以后的PTA作业,不能简简单单的“60分及格”。至于对老师、课程、实验、的意见我还是没有的。课上和课下的建议我也没有。但关于这门课,我还是没有学好,有时间要多刷学习通上的网课,反复学习,再与室友沟通交流以促进自己的学习,让自己进步的更快。也挺感谢自己的室友,能帮助我一个能力不太行的人,不放弃我,一直鼓励我。希望我以后能做的更好。加油!