首页 > 其他 > 详细

Blog

时间:2021-04-04 22:57:37      阅读:22      评论:0      收藏:0      [点我收藏+]

一、前言

  1、第一次题目集比较简单,但题量相对来说还是比较大的,但是并不是很难完成。第一次题目集8道题。从第一次题目集中学到了java中最基本的输入输出,

还有一些基本的语句。

  2、第二次的题目集有5道题,但是难度相对与第一次的题目集难度还是很大的,这次题目集中我从中学到了java中类的创立和类的使用。

  3、第三次的题目集我觉的难度最大,虽然题量不多但是难度是很大的而且第三题还用到了正则表达式进行功能的实现。

二、设计与分析

  

7-8 判断三角形类型 (20 分)

输入三角形三条边,判断该三角形为什么类型的三角形。

输入格式:

在一行中输入三角形的三条边的值(实型数),可以用一个或多个空格或回车分隔,其中三条边的取值范围均为[1,200]。

输出格式:

(1)如果输入数据非法,则输出“Wrong Format”; (2)如果输入数据合法,但三条边不能构成三角形,则输出“Not a triangle”; (3)如果输入数据合法且能够成等边三角形,则输出“Equilateral triangle”; (3)如果输入数据合法且能够成等腰直角三角形,则输出“Isosceles right-angled triangle”; (5)如果输入数据合法且能够成等腰三角形,则输出“Isosceles triangle”; (6)如果输入数据合法且能够成直角三角形,则输出“Right-angled triangle”; (7)如果输入数据合法且能够成一般三角形,则输出“General triangle”。

输入样例1:

在这里给出一组输入。例如:

50 50 50.0

输出样例1:

在这里给出相应的输出。例如:

Equilateral triangle

输入样例2:

在这里给出一组输入。例如:

60.2 60.2 80.56

输出样例2:

在这里给出相应的输出。例如:

Isosceles triangle

输入样例3:

在这里给出一组输入。例如:

0.5 20.5 80

输出样例3:

在这里给出相应的输出。例如:

Wrong Format
 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(),b=in.nextDouble(),c=in.nextDouble();
 6         if(a<1||a>200||b<1||b>200||c<1||c>200)
 7         {
 8             System.out.println("Wrong Format");
 9             System.exit(0);
10         }
11         else
12         {
13             if(a+b<=c||a+c<=b||b+c<=a)
14             {
15                 System.out.println("Not a triangle");
16                 System.exit(0);
17             }
18             else if(a==b&&b==c&&a==c)
19             {
20                 System.out.println("Equilateral triangle");
21                 System.exit(0);
22             }
23             else if(a==b&&a*a+b*b-c*c<0.000001||a==c&&a*a+c*c-b*b<0.00001||c==b&&c*c+b*b-a*a<0.000001)
24             {
25                 System.out.println("Isosceles right-angled triangle");
26                 System.exit(0);
27             }
28             else if(a==b&&a!=c&&b!=c||a==c&&a!=b&&c!=b||b==c&b!=a&&c!=a)
29             {
30                 System.out.println("Isosceles triangle");
31                 System.exit(0);
32             }
33             else if(a*a+b*b-c*c<0.000001||a==c&&a*a+c*c-b*b<0.000001||c==b&&c*c+b*b-a*a<0.000001)
34             {
35                 System.out.println("Right-angled triangle");
36                 System.exit(0);
37             }
38             else
39             {
40                 System.out.println("General triangle");
41                 System.exit(0);
42             }
43         }
44        
45         
46     }
47     
48 }

先输入三角型的三条边,判断三角形的三边是否合法。然后就判断三角形的三边关系,然后输出对应的三角形三边关系,这道题要注意的有两个关键点,第一是每次输出时加上System.exit(0);用来终止掉程序,防止三边符合下边的判断。第二是三边定义的数据类型是double,平方时会有误差所以判断是否为之间三角形时判断要用a*a+b*b-c*c<0.000001。这就是这道题的解题思路和容易采坑的地方。

                                7-4 求下一天 (30 分)
 

输入年月日的值(均为整型数),输出该日期的下一天。 其中:年份的合法取值范围为[1820,2020] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法。

要求:Main类中必须含有如下方法,签名如下:

public static void main(String[] args);//主方法 
public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型 
public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
public static void nextDate(int year,int month,int day) ; //求输入日期的下一天

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  1. 当输入数据非法及输入日期不存在时,输出“Wrong Format”;
  2. 当输入日期合法,输出下一天,格式如下:Next date is:年-月-日

输入样例1:

在这里给出一组输入。例如:

2020 3 10

输出样例1:

在这里给出相应的输出。例如:

Next date is:2020-3-11

输入样例2:

在这里给出一组输入。例如:

2025 2 10

输出样例2:

在这里给出相应的输出。例如:

Wrong Format

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// public static void main(String[] args);//主方法
// public static boolean isLeapYear(int year) ;//判断year是否为闰年,返回boolean类型
// public static boolean checkInputValidity(int year,int month,int day);//判断输入日期是否合法,返回布尔值
// public static void nextDate(int year,int month,int day) ; //求输入日期的下一天
int a,b,c;
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
if(checkInputValidity(a,b,c))
{
if(b==12&&c==31)
{
System.out.println("Next date is:"+(a+1)+"-"+1+"-"+1);
}
else if((b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&c==31)
{
System.out.println("Next date is:"+a+"-"+(b+1)+"-"+1);
}
else if((b==4||b==6||b==9||b==11)&&c==30)
{
System.out.println("Next date is:"+a+"-"+(b+1)+"-"+1);
}
else if(isLeapYear(a)&&b==2&&c==29)
{
System.out.println("Next date is:"+a+"-"+(b+1)+"-"+1);
}
else if(!isLeapYear(a)&&b==2&&c==28)
{
System.out.println("Next date is:"+a+"-"+(b+1)+"-"+1);
}
else
{
System.out.println("Next date is:"+a+"-"+b+"-"+(c+1));
}
}
else
{
System.out.println("Wrong Format");
}
}
public static boolean isLeapYear(int year) {
if(year%4==0&&year%100!=0||(year%400==0))
{
return true;
}
else
{
return false;
}
}
public static boolean checkInputValidity(int a,int b,int c){
int t=0;
if(isLeapYear(a))
{
if(b==2&&(c<=0||c>29))
{
t=1;
}
}
if(!isLeapYear(a))
{
if(b==2&&(c<=0||c>28))
{
t=1;
}
}
if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
{
if(c<=0||c>31)
{
t=1;
}
}
if(b==4||b==6||b==9||b==11)
{

if(c<=0||c>30)
{
t=1;
}
}
if(a<1820||a>2020||b<1||b>12||c<1||c>31)
{
t=1;
}
if(t==1)
{
return false;
}
else
{
return true;
}
}
}

 

这道题首先判断输入的日期是否合法,这里关于合法是指这个日期是否为有效日期,所以首先要判断这个年份是否为闰年,然后判断月份的是否为1-12,根据月份判断该月份一共有多少天,这样才可以判断输入的日期是否合法,在计算下一天时判断的东西几乎和上述相同,需要注意的是如果该日期是该年的最后一天,那么下一天就是下一年的1月1日。最重要的就是每个月份的天数判断要清晰。

 

                                                                                                              7-5 求前N天 (30 分)

输入年月日的值(均为整型数),同时输入一个取值范围在[-10,10] 之间的整型数n,输出该日期的前n天(当n > 0时)、该日期的后n天(当n<0时)。
其中年份取值范围为 [1820,2020] ,月份取值范围为[1,12] ,日期取值范围为[1,31] 。
注意:不允许使用Java中任何与日期有关的类或方法。

输入格式:

在一行中输入年月日的值以及n的值,可以用一个或多个空格或回车分隔。

输出格式:

  1. 当输入的年、月、日以及n的值非法时,输出“Wrong Format”;
  2. 当输入数据合法时,输出“n days ago is:年-月-日”

输入样例1:

在这里给出一组输入。例如:

2018  6 19 8 

输出样例1:

在这里给出相应的输出。例如:

8 days ago is:2018-6-11

输入样例2:

在这里给出一组输入。例如:

2018  6 19 -8 

输出样例2:

在这里给出相应的输出。例如:

-8 days ago is:2018-6-27
  1 import java.util.Scanner;
  2 public class Main {
  3     public static void main(String[] args) {
  4         Scanner in = new Scanner(System.in);
  5         int a,b,c,n;
  6         a=in.nextInt();
  7         b=in.nextInt();
  8         c=in.nextInt();
  9         n=in.nextInt();
 10         if(checkInputValidity(a,b,c,n))
 11         {
 12             last_n_Date(a,b,c,n);
 13         }
 14         else
 15         {
 16             System.out.println("Wrong Format");
 17         }
 18     }
 19     public static boolean isLeapYear(int year) {
 20         if(year%4==0&&year%100!=0||(year%400==0))
 21         {
 22             return true;
 23         }
 24         else 
 25         {
 26             return false;
 27         }
 28     }
 29     public static boolean checkInputValidity(int a,int b,int c,int n){
 30         int t=0;
 31         if(n>10||n<(-10))
 32         {
 33             t=1;
 34         }
 35         if(isLeapYear(a)) 
 36         {
 37             if(b==2&&(c<=0||c>29))
 38             {
 39                 t=1;
 40             }
 41         }
 42         if(!isLeapYear(a))
 43         {
 44             if(b==2&&(c<=0||c>28))
 45             {
 46                 t=1;
 47             }
 48         }
 49         if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
 50         {
 51             if(c<=0||c>31)
 52             {
 53                 t=1;
 54             }
 55         }
 56         if(b==4||b==6||b==9||b==11)
 57         {
 58             
 59             if(c<=0||c>30)
 60             {
 61                 t=1;
 62             }
 63         }
 64         if(a<1820||a>2020||b<1||b>12||c<1||c>31)
 65         {
 66             t=1;
 67         }
 68         if(t==1)
 69         {
 70             return false;
 71         }
 72         else
 73         {
 74             return true;
 75         }
 76     }
 77     public static void last_n_Date(int a,int b,int c,int n) {
 78         int date;
 79         if(n>0&&c<=n)
 80         {
 81             if(b==5||b==7||b==10||b==12)
 82             {
 83                 date=30-n+c;
 84                 System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+date);
 85             }
 86             if(b==4||b==2||b==6||b==8||b==9||b==11)
 87             {
 88                 date=31-n+c;
 89                 System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+date);
 90             }
 91             if(b==3&&isLeapYear(a))
 92             {
 93                 date=29-n+c;
 94                 System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+date);
 95             }
 96             if(b==3&&!isLeapYear(a))
 97             {
 98                 date=28-n+c;
 99                 System.out.println(n+" days ago is:"+a+"-"+(b-1)+"-"+date);
100             }
101             if(b==1)
102             {
103                 date=31-n+c;
104                 System.out.println(n+" days ago is:"+(a-1)+"-"+12+"-"+date);
105             }
106         }
107          if(n>0&&c>n)
108         {
109             date=c-n;
110             System.out.println(n+" days ago is:"+a+"-"+b+"-"+date);
111         }
112          if(n<0)
113         {
114             if((b==1||b==3||b==5||b==7||b==8||b==10)&&(31-c)<=Math.abs(n))
115             {
116                 date=Math.abs(n)-(31-c);
117                 System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+date);
118             }
119             else if((b==4||b==6||b==9||b==11)&&(30-c)<=Math.abs(n))
120             {
121                 date=Math.abs(n)-(30-c);
122                 System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+date);
123             }
124             else if(b==2&&isLeapYear(a))
125             {
126                 date=Math.abs(n)-(29-c);
127                 System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+date);
128             }
129             else if(b==2&&!isLeapYear(a))
130             {
131                 date=Math.abs(n)-(28-c);
132                 System.out.println(n+" days ago is:"+a+"-"+(b+1)+"-"+date);
133             }
134             else if(b==12&&(31-c)<=Math.abs(n))
135             {
136                 date=Math.abs(n)-(31-c);
137                 System.out.println(n+" days ago is:"+(a+1)+"-"+1+"-"+date);
138             }
139             else 
140             {
141                 date=c+Math.abs(n);
142                 System.out.println(n+" days ago is:"+a+"-"+b+"-"+date);
143             }
144         }
145         if(n==0)
146         {
147             System.out.println(n+" days ago is:"+a+"-"+b+"-"+c);
148         }
149     }
150 }

这道题思路和上一道题思路极为相似,唯一不同的是上一道题是求下一天,换成这道题就是n=1,只不过这次n为输入的一个数字,如果输入n为负数,就是求后n天,如果n为正数,就是求前n天,当n=0时就将你输入的年份再输出。

 

                                         7-2 定义日期类 (28 分)
 

定义一个类Date,包含三个私有属性年(year)、月(month)、日(day),均为整型数,其中:年份的合法取值范围为[1900,2000] ,月份合法取值范围为[1,12] ,日期合法取值范围为[1,31] 。 注意:不允许使用Java中和日期相关的类和方法,否则按0分处理。

要求:Date类结构如下图所示:

技术分享图片

输入格式:

在一行内输入年月日的值,均为整型数,可以用一到多个空格或回车分隔。

输出格式:

  • 当输入数据非法及输入日期不存在时,输出“Date Format is Wrong”;
  • 当输入日期合法,输出下一天,格式如下:Next day is:年-月-日

输入样例1:

在这里给出一组输入。例如:

1912 12 25

输出样例1:

在这里给出相应的输出。例如:

Next day is:1912-12-26

输入样例2:

在这里给出一组输入。例如:

2001 2 30

输出样例2:

在这里给出相应的输出。例如:

Date Format is Wrong

  1 import java.util.Scanner;
  2 class deal {
  3         private int a=0,b=0,c=0;
  4         
  5         
  6        // public deal() {
  7        //     
  8       //  }
  9         public deal(int a1,int b2,int c3) {
 10             a=a1;
 11             b=b2;
 12             c=c3;
 13             
 14         }
 15         public static boolean isLeapYear(int year) {
 16             if(year%4==0&&year%100!=0||(year%400==0))
 17             {
 18                 return true;
 19             }
 20             else 
 21             {
 22                 return false;
 23             }
 24         }
 25         public static boolean checkInputValidity(int a,int b,int c){
 26             int t=0;
 27             if(isLeapYear(a)) 
 28             {
 29                 if(b==2&&(c<=0||c>29))
 30                 {
 31                     t=1;
 32                 }
 33             }
 34             if(!isLeapYear(a))
 35             {
 36                 if(b==2&&(c<=0||c>28))
 37                 {
 38                     t=1;
 39                 }
 40             }
 41             if(b==1||b==3||b==5||b==7||b==8||b==10||b==12)
 42             {
 43                 if(c<=0||c>31)
 44                 {
 45                     t=1;
 46                 }
 47             }
 48             if(b==4||b==6||b==9||b==11)
 49             {
 50                 
 51                 if(c<=0||c>30)
 52                 {
 53                     t=1;
 54                 }
 55             }
 56             if(a<1900||a>2000||b<1||b>12||c<1||c>31)
 57             {
 58                 t=1;
 59             }
 60             if(t==1)
 61             {
 62                 return false;
 63             }
 64             else
 65             {
 66                 return true;
 67             }
 68         }
 69         
 70         public void tt() {
 71             if(checkInputValidity(a,b,c))
 72         {
 73             if(b==12&&c==31)
 74             {
 75                 System.out.println("Next day is:"+(a+1)+"-"+1+"-"+1);
 76             }
 77             else if((b==1||b==3||b==5||b==7||b==8||b==10||b==12)&&c==31) 
 78             {
 79                 System.out.println("Next day is:"+a+"-"+(b+1)+"-"+1);
 80             }
 81             else if((b==4||b==6||b==9||b==11)&&c==30)
 82             {
 83                 System.out.println("Next day is:"+a+"-"+(b+1)+"-"+1);
 84             }
 85             else if(isLeapYear(a)&&b==2&&c==29)
 86             {
 87                 System.out.println("Next day is:"+a+"-"+(b+1)+"-"+1);
 88             }
 89             else if(!isLeapYear(a)&&b==2&&c==28)
 90             {
 91                 System.out.println("Next day is:"+a+"-"+(b+1)+"-"+1);
 92             }
 93             else
 94             {
 95                 System.out.println("Next day is:"+a+"-"+b+"-"+(c+1));
 96             }
 97         }
 98         else
 99         {
100             System.out.println("Date Format is Wrong");
101         }
102     }
103         
104     
105 }
106 
107 public class Main {
108     public static void main(String[] args) {
109         Scanner in = new Scanner(System.in);
110 
111         int a,b,c;
112         a=in.nextInt();
113         b=in.nextInt();
114         c=in.nextInt();
115         deal n=new deal(a,b,c);
116         n.tt();
117     }
118     
119 }

这道题同7-5思路是相同的,但是这道题创建了一个Date类,并且在主类中的主方法中引用了Date类的对象n。这里要注意在一个包中只能有一个public类,但是可以存在多个类。

 

                                             7-3 一元多项式求导(类设计) (50 分)
 

编写程序性,实现对简单多项式的导函数进行求解。详见作业指导书。 OO作业3-3题目说明.pdf

输入格式:

在一行内输入一个待计算导函数的表达式,以回车符结束。

输出格式:

  1. 如果输入表达式不符合上述表达式基本规则,则输出“Wrong Format”。
  2. 如果输入合法,则在一行内正常输出该表达式的导函数,注意以下几点: 结果不需要排序,也不需要化简;
  • 当某一项为“0”时,则该项不需要显示,但如果整个导函数结果为“0”时,则显示为“0”;
  • 当输出结果第一项系数符号为“+”时,不输出“+”;
  • 当指数符号为“+”时,不输出“+”;
  • 当指数值为“0”时,则不需要输出“x^0”,只需要输出其系数即可。

输出格式见输入输出示例。

输入样例1:

在这里给出一组输入。例如:

-2*     x^-2+  5*x^12-4*x+       12

输出样例1:

在这里给出相应的输出。例如:

4*x^-3+60*x^11-4

输入样例2:

在这里给出一组输入。例如:

2*x^6-0*x^7+5

输出样例2:

在这里给出相应的输出。例如:

Wrong Format
 1 import java.util.Scanner;
 2 import java.util.regex.Matcher;
 3 import java.util.regex.Pattern;
 4 public class Main{
 5     
 6     public static void main(String[] args)  {
 7         Scanner in = new Scanner(System.in);
 8         String s,s1;
 9         s1=in.nextLine();
10         s=s1.replace(" ","");
11         boolean isNum=s.matches("[0-9]+");
12         if(s.equals("x"))
13         {
14             System.out.println("1");
15             System.exit(0);
16         }
17         if(s.equals("-x"))
18         {
19             System.out.println("-1");
20             System.exit(0);
21         }
22         if(isNum==true)
23         {
24             System.out.println("0");
25             System.exit(0);
26         }
27         else
28         {
29                 System.out.println("Wrong Format");
30         }
31         
32     }
33 }
这道题好难,写不出来

三、采坑心得

  在书写代码时,一定要有良好的代码书写规范,比如缩进格式的处理。这样能让你的代码看着更加清晰,其次定义方以及定义类时,方法名和类名一定要用英文单词命名,尽量避免使用汉语拼音来代替。书写代码时不要将所有代码都放在一个方法中,这样会让你的代码看起来比较杂乱无章难易读懂,因此要定义多种方法,让自己程序的结构更清晰

四、改进建议

  我还是没能更好的理解面向对象编程,总是写着写着就写成了面向过程编程,因此书写代码中要更好的理解和使用java中的类。

而且书写代码时一定要有注释,否则时间久了自己可能难以第一时间读懂自己写的程序

五、总结

  在这三次的题中。我学到了很多,java的基本语法已近掌握,更是能多类进行应用,对于正则表达式我也学到了很多,以后我会更加注重java中类的使用。

Blog

原文:https://www.cnblogs.com/1974687929zz/p/14616829.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!