其他分享
首页 > 其他分享> > 题目集1~3的总结性Blog

题目集1~3的总结性Blog

作者:互联网

(1)前言

这三次pta作业题量还行,个别题目较难。

第一次作业

知识点:关于Java的基本的程序设计包括从控制台读取输入,变量的的赋值语句和表达式,以及if语句的使用。

第二次作业

知识点:主要是关于方法的使用,Java数组的使用,数组的声明和创建。

第三次作业

知识点:定义类和创建对象,无参构造方法,有参构造方法。

(2)设计和分析

第一次作业7-8

在本题中我等腰直角三角形一直过不了我的代码如下

通过学习网上他人代码应将等腰直角三角形的判断条件改为a*a+c*c-b*b<0.1。

代码如下:

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 c=input.nextDouble();
if(a>=1&&a<=200&&b>=1&&b<=200&&c>=1&&c<=200)
{

if(a==b)
{
if(a*a+b*b==c*c)
System.out.print("Isosceles right-angled triangle");
else if(a==b&&b==c)
System.out.print("Equilateral triangle");
else if((a+b)>c&&(a+c)>b&&(b+c)>a&&(a-b)<c&&(a-c)<b&&(b-c)<a&&(b-a)<c&&(c-a)<b&&(c-b)<a)
System.out.print("Isosceles triangle");
else
System.out.print("Not a triangle");
}
else if(b==c)
{
if(b*b+c*c==a*a)
System.out.print("Isosceles right-angled triangle");
else if(a==b&&b==c)
System.out.print("Equilateral triangle");
else if((a+b)>c&&(a+c)>b&&(b+c)>a&&(a-b)<c&&(a-c)<b&&(b-c)<a&&(b-a)<c&&(c-a)<b&&(c-b)<a)
System.out.print("Isosceles triangle");
else
System.out.print("Not a triangle");
}
else if(a==c)
{
if(a*a+c*c==b*b)
System.out.print("Isosceles right-angled triangle");
else if(a==b&&b==c)
System.out.print("Equilateral triangle");
else if((a+b)>c&&(a+c)>b&&(b+c)>a&&(a-b)<c&&(a-c)<b&&(b-c)<a&&(b-a)<c&&(c-a)<b&&(c-b)<a)
System.out.print("Isosceles triangle");
else
System.out.print("Not a triangle");
}
else if(a*a+c*c==b*b)
System.out.print("Right-angled triangle");
else if(b*b+c*c==a*a)
System.out.print("Right-angled triangle");
else if(a*a+b*b==c*c)
System.out.print("Right-angled triangle");
else if((a+b)>c&&(a+c)>b&&(b+c)>a&&(a-b)<c&&(a-c)<b&&(b-c)<a&&(b-a)<c&&(c-a)<b&&(c-b)<a)
System.out.print("General triangle");
else
System.out.print("Not a triangle");
}
else
System.out.print("Wrong Format");
}
}

第二次作业的7-4

这次作业pta的测试点太少,我后面才反应过来我在日期的合法值这个函数写的不够到位少了一些细节

在我代码中少了对30天的月份的判断比如9月31我这个函数直接认为没有错误。应该加入对天数为31天,天数为30天以及2月分别判断。

这次作业难度一般,把12个月进行分类31,30以及2月就可以很好写下一天。

代码如下:

import javax.lang.model.type.NullType;
import javax.swing.*;
import java.util.Scanner;

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();
if(checkInputValidity(year,month,day)==true)
{
nextDate(year,month,day);

}
else
System.out.print("Wrong Format");
}//主方法;
public static boolean isLeapYear(int year) {
if(year%4==0&&year%100!=0)
return true;
else if(year%400==0)
return true;
else
return false;
}//判断year是否为闰年,返回boolean类型;
public static boolean checkInputValidity(int year,int month,int day)
{
if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day<=31)
{
if(isLeapYear(year)==false&&month==2&&day>28)
return false;
else
return true;
}
else
return false;

}//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day)
{
if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)
{
if(day<31)
System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
else if(day==31&&month==12)
System.out.print("Next date is:"+(year+1)+"-"+1+"-"+1);
else
System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
}
else if(isLeapYear(year)==true&&month==2)
{
if(day<29)
System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
else
System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
}
else if(isLeapYear(year)==false&&month==2)
{
if(day<28)
System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
else
System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
}
else
{
if(day<30)
System.out.print("Next date is:"+year+"-"+month+"-"+(day+1));
else
System.out.print("Next date is:"+year+"-"+(month+1)+"-"+1);
}
}//求输入日期的下一天


}

第三次作业7-2

7-2这个作业和第二次作业差不多在日期的合法性我做了修改使其更完整。

在这次作业中使用创建类的方法来写,其实与第二次作业差不多,主要是运用到了创建对象的的方法,让代码看起来更简单明了。

代码如下:

import java.util.Scanner;

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(year,month,day);
if(date.checklnputValidity()==true)
date.getNextDate();
else
System.out.print("Date Format is Wrong");


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

public Date() {

}

public 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) {
if (year % 4 == 0 && year % 100 != 0)
return true;
else if (year % 400 == 0)
return true;
else
return false;
}

public boolean checklnputValidity() {
if (year >= 1900 && year <= 2000 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
if (isLeapYear(year) == true) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return true;
else if (month == 2) {
if (day > 29)
return false;
else
return true;
} else {
if (day > 30)
return false;
else
return true;
}
} else {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return true;
else if (month == 2) {
if (day > 28)
return false;
else
return true;
} else {
if (day > 30)
return false;
else
return true;
}
}
} else
return false;
}

public void getNextDate() {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
if (day < 31)
System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1));
else if (day == 31 && month == 12)
System.out.print("Next day is:" + (year + 1) + "-" + 1 + "-" + 1);
else
System.out.print("Next day is:" + year + "-" + (month + 1) + "-" + 1);
} else if (isLeapYear(year) == true && month == 2) {
if (day < 29)
System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1));
else
System.out.print("Next day is:" + year + "-" + (month + 1) + "-" + 1);
} else if (isLeapYear(year) == false && month == 2) {
if (day < 28)
System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1));
else
System.out.print("Next day is:" + year + "-" + (month + 1) + "-" + 1);
} else {
if (day < 30)
System.out.print("Next day is:" + year + "-" + month + "-" + (day + 1));
else
System.out.print("Next day is:" + year + "-" + (month + 1) + "-" + 1);
}
}
}}

第三次作业7-3

这次作业难度较大,我只能写出一些特殊情况,这次作业用到一些Java自带的库函数我还要学习,难度较大。

代码如下:

import java.util.Scanner;

public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
String s=input.next();
int i;
int flag=0;
int flag1=0;
for(i=0;i<s.length();i++)
{
if(s.charAt(i)=='x')
{
flag1++;
if(s.charAt(i-2)=='0'||s.charAt(i+2)=='0')
flag++;
}
}
if(flag1==0)
System.out.print("0");
if(flag!=0)
System.out.print("Wrong Format");

}
}

(3)踩坑心得

这三次作业个别题目较难,写代码的过程中也是不断的修改测试,由于pta的测试点较少有时候一些错误不好找到比如

public static boolean checkInputValidity(int year,int month,int day)
{
if(year>=1820&&year<=2020&&month>=1&&month<=12&&day>=1&&day<=31)
{
if(isLeapYear(year)==false&&month==2&&day>28)
return false;
else
return true;
}
else
return false;

}//判断输入日期是否合法,返回布尔值

在这段代码中我单纯看了题目给的边界范围和测试点没有注意一些细节虽然在pta上过了测试点后来经过同学的指点才找到了自己的

错误比如9月31。修改后应该为

public boolean checklnputValidity() {
if (year >= 1900 && year <= 2000 && month >= 1 && month <= 12 && day >= 1 && day <= 31) {
if (isLeapYear(year) == true) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return true;
else if (month == 2) {
if (day > 29)
return false;
else
return true;
} else {
if (day > 30)
return false;
else
return true;
}
} else {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return true;
else if (month == 2) {
if (day > 28)
return false;
else
return true;
} else {
if (day > 30)
return false;
else
return true;
}
}
} else
return false;
}

上述代码是我修改以后的代码。经过几个星期的Java学习让我对Java有了初步的了解。虽然刚刚开始接触Java时,有点懵,刚刚上课的时候一点不会,但是经过

几个星期的学习,在csdn上看他人文章有了初步了解。

(4)改进建议

我在一些题目上if 和else使用太多使代码看起来复杂比如

public class Main {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int a= input.nextInt();
double b= input.nextInt();
double c=0;
int s=0;
if(a==0)
{
if(b<0)
{
System.out.println("Wrong Format");
s++;
}
else if(b<=8350)
c=b*0.1;
else if(b>8350&&b<=33950)
c=8350*0.1+(b-8350)*0.15;
else if(b>33950&&b<=82250)
c=8350*0.1+(33950-8350)*0.15+(b-33950)*0.25;
else if(b>82250&&b<=171550)
c=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(b-82250)*0.28;
else if(b>171550&&b<=372950)
c=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(b-171550)*0.33;
else if(b>372950)

c=8350*0.1+(33950-8350)*0.15+(82250-33950)*0.25+(171550-82250)*0.28+(372950-171550)*0.33+(b-372950)*0.35;
if(s==0)
System.out.println(c+.0);


}
else if(a==1)
{
if(b<0) {
System.out.println("Wrong Format");
s++;
}
if(b<=16700)
c=b*0.1;
else if(b>16700&&b<=67900)
c=16700*0.1+(b-16700)*0.15;
else if(b>67900&&b<=137050)
c=16700*0.1+(67900-16700)*0.15+(b-67900)*0.25;
else if(b>137050&&b<=208850)
c=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(b-137050)*0.28;
else if(b>208850&&b<=372950)
c=16700*0.1+(67900-16700)*0.15+(137050-67900)*0.25+(208850-137050)*0.28+(b-208850)*0.33;
else if(b>372950)
c = 16700 * 0.1 + (67900 - 16700) * 0.15 + (137050 - 67900) * 0.25 + (208850 - 137050) * 0.28 + (372950 - 208850) * 0.33 + (b - 372950) * 0.35;
if(s==0)
System.out.println(c + .0);

}
else if(a==2) {
if (b < 0) {
System.out.println("Wrong Format");
s++;
}
if (b <= 8350)
c = b * 0.1;
else if (b > 8350 && b <= 33950)
c = 8350 * 0.1 + (b - 8350) * 0.15;
else if (b > 33950 && b <= 68525)
c = 8350 * 0.1 + (33950 - 8350) * 0.15 + (b - 33950) * 0.25;
else if (b > 68525 && b <= 104425)
c = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (b - 68525) * 0.28;
else if (b > 104425 && b <= 186475)
c = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (b - 104425) * 0.33;
else if (b > 186475)


c = 8350 * 0.1 + (33950 - 8350) * 0.15 + (68525 - 33950) * 0.25 + (104425 - 68525) * 0.28 + (186475 - 104425) * 0.33 + (b - 186475) * 0.35;
if(s==0)
System.out.println(c + .0);

}
else if(a==3)
{
if(b<0) {
System.out.println("Wrong Format");
s++;
}
if(b<=11950)
c=b*0.1;
else if(b>11950&&b<=45500)
c=11950*0.1+(b-11950)*0.15;
else if(b>45500&&b<=117450)
c=11950*0.1+(45500-11950)*0.15+(b-45500)*0.25;
else if(b>117450&&b<=190200)
c=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(b-117450)*0.28;
else if(b>190200&&b<=372950)
c=11950*0.1+(45500-11950)*0.15+(117450-45500)*0.25+(190200-117450)*0.28+(b-190200)*0.33;
else if(b>372950)
c = 11950 * 0.1 + (45500 - 11950) * 0.15 + (117450 - 45500) * 0.25 + (190200 - 117450) * 0.28 + (372950 - 190200) * 0.33 + (b - 372950) * 0.35;
if(s==0)
System.out.println(c + .0);

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

在税率这道题目中完全可以使用switch语句来写让代码看起来更加简单,让别人看起来更容易明白。平时if-else语句使用太多,一写代码就是if-else。

(5)总结

经过几个星期的学习,让我对Java有了初步了解,Java说与C语言像,又与C语言不一样。Java有许多的库函数,使用更加的方便。关于作业有一些点我实在是做不出来,一些

题目实在是太难了,有点头秃。关于学习Java,要多看慕课学习。希望老师上课可以讲慢点。同时通过三次作业让我有了编写java基本的程序的能力,这三次作业题目有点多,

有一些题目的数据有点复杂,我觉得没有必要,毕竟太复杂的数据容易输错希望老师数据方面能改的的简单点,主要考察我们的思维能力。

 

 

 

 

 

 

 

 

 

标签:总结性,题目,System,month,Blog,&&,year,else,day
来源: https://www.cnblogs.com/zz234/p/15412210.html