经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Java » 查看文章
初学JVAVA题目总结
来源:cnblogs  作者:管希雄  时间:2021/4/6 10:20:28  对本文有异议

1,前言:总结三次题目集的知识点、题量、难度等情况

  这次题目集是初学Java的题目,前两次只是涉及Java的一些基本语法和格式,第三次要创建类,有点面向对象的意思了。题目量适中,有足够的时间去写。前两次题目集题目难度还行,有C语言的基础,循环和判断语句用法和C语言差不多,容易上手。第三次题目集创建类是Java的新东西,经过几个小时学习,对类有了大概的了解,完成了第一第二题,第三题要用正则表达式,感觉蛮难的。

2,设计与分析:

(1)题目集1,7-8 输入三角形三条边,判断该三角形为什么类型的三角形。

  判断三角形类型,这里应该是采用了最笨的方法,就一种一种情况判断,最后根据判断情况输出相应内容。首先判断是否能组成一个三角形,再判断等腰、直角、等腰直角、等边、普通。

  我想的是用一个变量的值来记录三角形的判断情况,最后根据变量的值来判断三角形类型。因为等腰直角既是等腰优势直角,如果判断后直接输出会导致输出3个结果。

  1. import java.util.Scanner;
  2. public class Main {
  3. public static void main(String[] args){
  4. Scanner input = new Scanner(System.in);
  5. float a = input.nextFloat();
  6. float b = input.nextFloat();
  7. float c = input.nextFloat();
  8. int x = 5;
  9. if(a >200 || b > 200 || c >200 || a < 1 || b < 1 || c < 1){
  10. x = 6;
  11. System.out.println("Wrong Format");
  12. }
  13. else{
  14. if(a >= b + c || b >= a + c || c >= a + b)   //不构成三角形
  15. x = 0;
  16. 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 )   //直角
  17. x = 1;
  18. if(a == b || b == c || a == c)//等腰
  19. x = 2;
  20. 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) )  //等腰直角
  21. x = 3;
  22. if(a == b && b == c)//等边
  23. x = 4;
  24. }
  25. if(x == 0 )
  26. System.out.println("Not a triangle");
  27. if(x == 1)
  28. System.out.println("Right-angled triangle");
  29. if(x == 2)
  30. System.out.println("Isosceles triangle");
  31. if(x == 3)
  32. System.out.println("Isosceles right-angled triangle");
  33. if(x == 4)
  34. System.out.println("Equilateral triangle");
  35. if(x == 5)
  36. System.out.println("General triangle");
  37. }
  38. }

 

这个代码虽然通过了,但还是有许多待改进的地方。判断方法可以更简洁些,判断等腰直角可以放在判断等腰和直角一起里面。

 

 

(2)题目集2,7-4   根据给定输入日期求下一天

  首先要判断输入日期的年份是否是闰年、月份的一个月天数,闰年二月有29天而平年二月只有28天,有的月份一个月30天有的月份一个月31天。然后根据情况给出相应结果。

  1. import java.util.Scanner;
  2. public class Main{
  3. public static boolean checkInputValidity(int year,int month,int day,boolean r){
  4. boolean p;
  5. p = true;
  6. if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){
  7. p = false;
  8. }
  9. if(!r && day > 28){
  10. p = false;
  11. }
  12. return p;
  13. }
  14. public static void nextDate(int year,int month,int day,boolean r){
  15. boolean p;
  16. int i = month,j = day;
  17. p = true;
  18. if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12){
  19. if(i == 12 && j == 31){
  20. day = 1;
  21. year = year + 1;
  22. month = 1;
  23. }
  24. if(j == 31 && i != 12){
  25. month = month + 1;
  26. day = 1;
  27. }
  28. if(j != 31){
  29. day = day + 1;
  30. }
  31. }
  32. if(i == 4 || i == 6 || i == 9 || i == 11){
  33. if(j == 30){
  34. month = month + 1;
  35. day = 1;
  36. }
  37. if(j != 30){
  38. day = day + 1;
  39. }
  40. }
  41. if(i == 2 && r){
  42. if(j == 29){
  43. month = 3;
  44. day = 1;
  45. }
  46. else{
  47. day = day + 1;
  48. }
  49. }
  50. if(i == 2 && !r){
  51. if(j == 28){
  52. month = 3;
  53. day = 1;
  54. }
  55. else{
  56. day = day + 1;
  57. }
  58. }
  59. System.out.printf("Next date is:%d-%d-%d",year,month,day);
  60. }
  61. public static void main(String[] args){
  62. Scanner input = new Scanner(System.in);
  63. int year = input.nextInt();
  64. int month = input.nextInt();
  65. int day = input.nextInt();
  66. boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  67. if(checkInputValidity(year,month,day,r)){
  68. nextDate(year,month,day,r);
  69. }
  70. else{
  71. System.out.println("Wrong Format");
  72. }
  73. }
  74. }

 

 

 

 

 

(3)题目集2,7-5   根据给定输入日期求前N天日期

  跟上一题差不多,首先也是要判断日期,根据N来判断断是否要跨月,再根据前一个月或后一个月的天数来输出相应日期。

 

  1. import java.util.Scanner;
  2. public class Main{
  3. public static boolean checkInputValidity(int year,int month,int day,boolean r){
  4. boolean p;
  5. p = true;
  6. if(year < 1820 || year > 2020 || month < 1 || month > 12 || day < 1 || day > 31 ){
  7. p = false;
  8. }
  9. if(!r && day > 28){
  10. p = false;
  11. }
  12. return p;
  13. }
  14. public static void nextDate(int year,int month,int day,boolean r,int n){
  15. int i = month,j = day;
  16. day = j + n;
  17. if(r&&i==2){
  18. if(j + n>29){
  19. day = j + n-29;
  20. month++;
  21. }
  22. }
  23. if(!r&&i==2){
  24. if(j + n>28){
  25. day = j + n-28;
  26. month++;
  27. }
  28. }
  29. if(r&&i==3){
  30. if(j + n<=0){
  31. day =29+j+n;
  32. month--;
  33. }
  34. }
  35. if(!r&&i==3){
  36. if(j + n<=0){
  37. day = j + n + 28;
  38. month--;
  39. }
  40. }
  41. if(n + j <=0){
  42. if(i == 1 ){
  43. year--;
  44. day = 31 + (n + j);
  45. month = 12;
  46. }
  47. if(i == 2 || i == 4 || i == 6 || i == 9 || i == 8 || i == 11){
  48. day = 31 + (n + j);
  49. month--;
  50. }
  51. if(i == 5 || i == 7 || i == 10 || i == 12 ){
  52. year--;
  53. day = 30 + (n + j);
  54. month = 12;
  55. }
  56. }
  57. if(n + j >30){
  58. if(i == 12){
  59. if(n+j > 31){
  60. day =(n+j)-31;
  61. year++;
  62. month=1;
  63. }
  64. else
  65. day = 31;
  66. }
  67. if(i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10){
  68. if(n+j > 31){
  69. day =(n+j)-31;
  70. month++;
  71. }
  72. else
  73. day = 31;
  74. }
  75. if(i == 4 || i==6 || i==9 || i==11){
  76. day =(n+j)-30;
  77. month++;
  78. }
  79. }
  80. System.out.printf("%d days ago is:%d-%d-%d",-n,year,month,day);
  81. }
  82. public static void main(String[] args){
  83. Scanner input = new Scanner(System.in);
  84. int year = input.nextInt();
  85. int month = input.nextInt();
  86. int day = input.nextInt();
  87. int n = input.nextInt();
  88. boolean r = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  89. if(checkInputValidity(year,month,day,r)){
  90. nextDate(year,month,day,r,-n);
  91. }
  92. else{
  93. System.out.println("Wrong Format");
  94. }
  95. }
  96. }

 

 

 

 

(4)题目集3,7-2   定义日期类求下一天

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

 

 

 

 

(5)题目集3,7-3   一元多项式求导

  一元多项式求导首先要使用正则表达式进行匹配,查看输入是否正确,然后进行求导。此代码只能判断输入是否正确及对某种特定情况求导,50满分只得了20分,还是靠投机取巧得的,感觉此题太复杂了,求导过程中不同的情况太对,不知道如何下手。

  1. import java.util.Scanner;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4. class Equation{
  5. String string;
  6. String totalRegex = "([-+]?([1-9]+[0-9]*(\\s)*(\\*)?)?x?(\\^[+-]?[1-9]+[0-9]*)?(\\s)*)+";
  7. String sbuRegex = "[-+]?[1-9]+[0-9]*";
  8. Equation(String input){
  9. string = input;
  10. }
  11. boolean judge(){
  12. if(string.matches("[-+]?[1-9]?[\\d]*")){
  13. return string.matches("[-+]?[1-9]+[\\d]*");
  14. }
  15. else
  16. return string.matches(totalRegex);
  17. }
  18. void derivation(String input){
  19. Pattern pattern = Pattern.compile(sbuRegex);
  20. Matcher matcher=pattern.matcher(input);
  21. int s = 0,j = 0;
  22. if(input.matches("[-+]?[1-9]?[\\d]*")) {
  23. System.out.println("0");
  24. }
  25. else {
  26. while(matcher.find())
  27. {
  28. String tmp=matcher.group();
  29. s++;
  30. int i = Integer.parseInt(tmp);
  31. if(s%2 == 0){
  32. if(i>0)
  33. System.out.print("+"+i*j+"*x^"+(i-1));
  34. else
  35. System.out.print(i*j+"*x^"+(i-1));
  36. }
  37. j = i;
  38. int end=matcher.end();
  39. if(end==input.length())
  40. break;
  41. }
  42. // System.out.println("1");
  43. }
  44. }
  45. }
  46. public class Main{
  47. public static void main(String[] args){
  48. Scanner p = new Scanner(System.in);
  49. String input = p.nextLine();
  50. Equation a = new Equation(input);
  51. if(a.judge())
  52. {
  53. a.derivation(input);
  54. }
  55. else
  56. System.out.println("Wrong Format");
  57. }
  58. }

 

 

 

 

3,采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空

   在判断三角形类型的那题中一开始直接 a*a == b*b+c*c ,然后发现一直过不了。然后偶然想起判断两个浮点数是否相等尽量不要用 == 来判断,要用这两个数的差值来判断,于是就改成了 Math.abs(a*a - b*b - c*c) < 0.01 这样就对了。因为人算时根号数平方可以得到一个人整数但这题只能用小数来代替带根号的数,平方后会产生误差,所以只能用两个数的插值来判断。

例如

  1. public class Main{
  2. public static void main(String[] args){
  3. System.out.println(Math.abs(Math.pow(Math.sqrt(2.0), 2)-2) < 0.001); //true
  4. }
  5. }
  6. public class Main{
  7. public static void main(String[] args){
  8. System.out.println(Math.pow(Math.sqrt(2.0), 2) == 2); //flase
  9. }
  10. }

 

 

  在求前N天的题目中,一开始忽略了N为负数的情况。两个算日期的题也没什么坑,就是要做好判断理清关系。

 

  题集三那题只写出了能判断多项式格式是否正确,和某种特殊情况(指数不为1,没有常数项)下的求导。一开始写的正则表达式忽略了项之间可以存在空格的情况,解决方法很简单,在可以已存在空格的地方加上(\\s)*就行。

4,改进建议

  没有对代码太多的改,基本就是写完了交了对了就不管了,以后要多加注意代码的改进问题。

5,总结

  初步学习感受到了Java的面向对象含义,了解了正则表达式。作业涉及内容超过课上教学,为了写作业自学完了,上课就不知道干什么。

 

原文链接:http://www.cnblogs.com/Gxx-x/p/14615695.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号