1,前言:总结三次题目集的知识点、题量、难度等情况
这次题目集是初学Java的题目,前两次只是涉及Java的一些基本语法和格式,第三次要创建类,有点面向对象的意思了。题目量适中,有足够的时间去写。前两次题目集题目难度还行,有C语言的基础,循环和判断语句用法和C语言差不多,容易上手。第三次题目集创建类是Java的新东西,经过几个小时学习,对类有了大概的了解,完成了第一第二题,第三题要用正则表达式,感觉蛮难的。
2,设计与分析:
(1)题目集1,7-8 输入三角形三条边,判断该三角形为什么类型的三角形。
判断三角形类型,这里应该是采用了最笨的方法,就一种一种情况判断,最后根据判断情况输出相应内容。首先判断是否能组成一个三角形,再判断等腰、直角、等腰直角、等边、普通。
我想的是用一个变量的值来记录三角形的判断情况,最后根据变量的值来判断三角形类型。因为等腰直角既是等腰优势直角,如果判断后直接输出会导致输出3个结果。
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- float a = input.nextFloat();
- float b = input.nextFloat();
- float c = input.nextFloat();
- int x = 5;
- if(a >200 || b > 200 || c >200 || a < 1 || b < 1 || c < 1){
- x = 6;
- System.out.println("Wrong Format");
- }
-
- else{
- if(a >= b + c || b >= a + c || c >= a + b) //不构成三角形
- x = 0;
- if(-0.01 < a*a + c*c - b*b && a*a + b*b - c*c < 0.01||
-0.01 <a*a + c*c - b*b && b*b + c*c - a*a < 0.01 ||
-0.01 < a*a + c*c - b*b && a*a + c*c - b*b < 0.01 ) //直角 - x = 1;
- if(a == b || b == c || a == c)//等腰
- x = 2;
- if((a == b && -0.01 < a*a + c*c - b*b && a*a + b*b - c*c < 0.01 )||
( b == c && -0.01 <a*a + c*c - b*b && b*b + c*c - a*a < 0.01 )||
( a == c && -0.01 < a*a + c*c - b*b && a*a + c*c - b*b < 0.01) ) //等腰直角 - x = 3;
- if(a == b && b == c)//等边
- x = 4;
- }
- if(x == 0 )
- System.out.println("Not a triangle");
- if(x == 1)
- System.out.println("Right-angled triangle");
- if(x == 2)
- System.out.println("Isosceles triangle");
- if(x == 3)
- System.out.println("Isosceles right-angled triangle");
- if(x == 4)
- System.out.println("Equilateral triangle");
- if(x == 5)
- System.out.println("General triangle");
- }
- }
这个代码虽然通过了,但还是有许多待改进的地方。判断方法可以更简洁些,判断等腰直角可以放在判断等腰和直角一起里面。

(2)题目集2,7-4 根据给定输入日期求下一天
首先要判断输入日期的年份是否是闰年、月份的一个月天数,闰年二月有29天而平年二月只有28天,有的月份一个月30天有的月份一个月31天。然后根据情况给出相应结果。
- import java.util.Scanner;
- public class Main{
- public static boolean checkInputValidity(int year,int month,int day,boolean r){
- boolean p;
- p = true;
- if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){
- p = false;
- }
- if(!r && day > 28){
- p = false;
- }
-
- return p;
- }
- public static void nextDate(int year,int month,int day,boolean r){
- boolean p;
- int i = month,j = day;
- p = true;
- if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
- if(i == 12 && j == 31){
- day = 1;
- year = year + 1;
- month = 1;
- }
- if(j == 31 && i != 12){
- month = month + 1;
- day = 1;
- }
- if(j != 31){
- day = day + 1;
- }
- }
- if(i == 4 || i == 6 || i == 9 || i == 11){
- if(j == 30){
- month = month + 1;
- day = 1;
- }
- if(j != 30){
- day = day + 1;
- }
- }
- if(i == 2 && r){
- if(j == 29){
- month = 3;
- day = 1;
- }
- else{
- day = day + 1;
- }
- }
- if(i == 2 && !r){
- if(j == 28){
- month = 3;
- day = 1;
- }
- else{
- day = day + 1;
- }
- }
-
- System.out.printf("Next date is:%d-%d-%d",year,month,day);
- }
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- int year = input.nextInt();
- int month = input.nextInt();
- int day = input.nextInt();
- boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- if(checkInputValidity(year,month,day,r)){
- nextDate(year,month,day,r);
- }
- else{
- System.out.println("Wrong Format");
- }
- }
- }

(3)题目集2,7-5 根据给定输入日期求前N天日期
跟上一题差不多,首先也是要判断日期,根据N来判断断是否要跨月,再根据前一个月或后一个月的天数来输出相应日期。
- import java.util.Scanner;
- public class Main{
- public static boolean checkInputValidity(int year,int month,int day,boolean r){
- boolean p;
- p = true;
- if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){
- p = false;
- }
- if(!r && day > 28){
- p = false;
- }
- return p;
- }
- public static void nextDate(int year,int month,int day,boolean r,int n){
- int i = month,j = day;
- day = j + n;
- if(r&&i==2){
- if(j + n>29){
- day = j + n-29;
- month++;
- }
- }
- if(!r&&i==2){
- if(j + n>28){
- day = j + n-28;
- month++;
- }
- }
- if(r&&i==3){
- if(j + n<=0){
- day =29+j+n;
- month--;
- }
- }
- if(!r&&i==3){
- if(j + n<=0){
- day = j + n + 28;
- month--;
- }
- }
- if(n + j <=0){
- if(i == 1 ){
- year--;
- day = 31 + (n + j);
- month = 12;
- }
- if(i == 2 || i == 4 || i == 6 || i == 9 || i == 8 || i == 11){
- day = 31 + (n + j);
- month--;
- }
- if(i == 5 || i == 7 || i == 10 || i == 12 ){
- year--;
- day = 30 + (n + j);
- month = 12;
- }
- }
- if(n + j >30){
- if(i == 12){
- if(n+j > 31){
- day =(n+j)-31;
- year++;
- month=1;
- }
- else
- day = 31;
- }
- if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10){
- if(n+j > 31){
- day =(n+j)-31;
- month++;
- }
- else
- day = 31;
- }
- if(i == 4 || i==6 || i==9 || i==11){
- day =(n+j)-30;
- month++;
- }
- }
- System.out.printf("%d days ago is:%d-%d-%d",-n,year,month,day);
- }
- public static void main(String[] args){
- Scanner input = new Scanner(System.in);
- int year = input.nextInt();
- int month = input.nextInt();
- int day = input.nextInt();
- int n = input.nextInt();
- boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
- if(checkInputValidity(year,month,day,r)){
- nextDate(year,month,day,r,-n);
- }
- else{
- System.out.println("Wrong Format");
- }
- }
- }

(4)题目集3,7-2 定义日期类求下一天
这里就有点Java面相对象的意思了,创建一个Data类,包含私有属性年月日,以及各种方法。只用在主类中创建一个该类对象就可以通过调用该对象自己的方法让它自己“说”说出下一天的日期。

(5)题目集3,7-3 一元多项式求导
一元多项式求导首先要使用正则表达式进行匹配,查看输入是否正确,然后进行求导。此代码只能判断输入是否正确及对某种特定情况求导,50满分只得了20分,还是靠投机取巧得的,感觉此题太复杂了,求导过程中不同的情况太对,不知道如何下手。
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- class Equation{
- String string;
- String totalRegex = "([-+]?([1-9]+[0-9]*(\\s)*(\\*)?)?x?(\\^[+-]?[1-9]+[0-9]*)?(\\s)*)+";
- String sbuRegex = "[-+]?[1-9]+[0-9]*";
- Equation(String input){
- string = input;
- }
- boolean judge(){
- if(string.matches("[-+]?[1-9]?[\\d]*")){
- return string.matches("[-+]?[1-9]+[\\d]*");
- }
- else
- return string.matches(totalRegex);
- }
- void derivation(String input){
- Pattern pattern = Pattern.compile(sbuRegex);
- Matcher matcher=pattern.matcher(input);
- int s = 0,j = 0;
- if(input.matches("[-+]?[1-9]?[\\d]*")) {
- System.out.println("0");
- }
- else {
- while(matcher.find())
- {
- String tmp=matcher.group();
- s++;
- int i = Integer.parseInt(tmp);
- if(s%2 == 0){
- if(i>0)
- System.out.print("+"+i*j+"*x^"+(i-1));
- else
- System.out.print(i*j+"*x^"+(i-1));
- }
- j = i;
- int end=matcher.end();
- if(end==input.length())
- break;
- }
- // System.out.println("1");
- }
- }
- }
- public class Main{
- public static void main(String[] args){
- Scanner p = new Scanner(System.in);
- String input = p.nextLine();
- Equation a = new Equation(input);
- if(a.judge())
- {
- a.derivation(input);
- }
- else
- System.out.println("Wrong Format");
- }
- }

3,采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
在判断三角形类型的那题中一开始直接 a*a == b*b+c*c ,然后发现一直过不了。然后偶然想起判断两个浮点数是否相等尽量不要用 == 来判断,要用这两个数的差值来判断,于是就改成了 Math.abs(a*a - b*b - c*c) < 0.01 这样就对了。因为人算时根号数平方可以得到一个人整数但这题只能用小数来代替带根号的数,平方后会产生误差,所以只能用两个数的插值来判断。
例如
- public class Main{
- public static void main(String[] args){
- System.out.println(Math.abs(Math.pow(Math.sqrt(2.0), 2)-2) < 0.001); //true
- }
- }
- public class Main{
- public static void main(String[] args){
- System.out.println(Math.pow(Math.sqrt(2.0), 2) == 2); //flase
- }
- }
在求前N天的题目中,一开始忽略了N为负数的情况。两个算日期的题也没什么坑,就是要做好判断理清关系。
题集三那题只写出了能判断多项式格式是否正确,和某种特殊情况(指数不为1,没有常数项)下的求导。一开始写的正则表达式忽略了项之间可以存在空格的情况,解决方法很简单,在可以已存在空格的地方加上(\\s)*就行。
4,改进建议
没有对代码太多的改,基本就是写完了交了对了就不管了,以后要多加注意代码的改进问题。
5,总结:
初步学习感受到了Java的面向对象含义,了解了正则表达式。作业涉及内容超过课上教学,为了写作业自学完了,上课就不知道干什么。