第十六章 课程总复习

一、课程回顾

    整本书分为二部分,第一部是程序的基础,第二部Java类基础

1 第一章~第十章

  • Java语法
  • 选择结构(重点)
  • 循环结构(重点)(难点)
  • 数组

2 第十一章~第十五章

  • 类和对象(难点)
  • 类的方法(重点)
  • 字符串类

二、知识点梳理

1 程序逻辑



2 数组


3 初识Java


三、总结

  • 多重if和switch选择结构都可以用于多分支的情况,但使用场合不同
  • while循环是先判断再执行,do-while循环反之
  • for循环适用于循环次数确定的情况
  • break和continue都可以改变程序执行的流程,但含义不同,使用场合也不同
  • 类是对象的类型,对象是类的实例
  • 方法分为无参方法和带参方法
  • String类提供了很多方法实现对字符串的操作

四、作业

1 实现用户登录功能,在控制台输入用户名和密码,然后判断输入是否正确并输出结果,如下图所示:

参考代码
  1. public class Login {
  2. /**
  3. * 判断学生登录是否成功
  4. * @param name
  5. * @param password
  6. * @return
  7. */
  8. public boolean login(String name,String password){
  9. if(name.equals("jbit")&&password.equals("bdqn")){
  10. return true;
  11. }else
  12. return false;
  13. }
  14. public static void main(String[] args) {
  15. Scanner input = new Scanner(System.in);
  16. System.out.println("请输入用户名:");
  17. String name = input.next();
  18. System.out.println("请输入密码:");
  19. String password = input.next();
  20. Login lg=new Login ();
  21. boolean flag=lg.login(name, password);
  22. if(flag){
  23. System.out.println("登录成功!");
  24. }else{
  25. System.out.println("用户名或密码错误!");
  26. }
  27. }
  28. }
2 在控制台输入学生姓名、年龄、性别和学校,然后模拟将学生信息存储到数据库中,输出结果如图所示

参考代码
学生类
  1. public class Student {
  2. private String name; //姓名
  3. private int age; //年龄
  4. private String sex; //性别
  5. private String school; //学校
  6. public String getName() {
  7. return name;
  8. }
  9. public void setName(String name) {
  10. this.name = name;
  11. }
  12. public int getAge() {
  13. return age;
  14. }
  15. public void setAge(int age) {
  16. this.age = age;
  17. }
  18. public String getSex() {
  19. return sex;
  20. }
  21. public void setSex(String sex) {
  22. this.sex = sex;
  23. }
  24. public String getSchool() {
  25. return school;
  26. }
  27. public void setSchool(String school) {
  28. this.school = school;
  29. }
  30. }
Dao类
  1. public class StudentDao {
  2. /**
  3. * 模拟插入学生数据到数据库
  4. * @param stu
  5. */
  6. public void insertStudent(Student stu){
  7. String name=stu.getName();
  8. int age=stu.getAge();
  9. String sex=stu.getSex();
  10. String school=stu.getSchool();
  11. System.out.println(" 将该学生信息成功写入到数据库");
  12. System.out.println(name+" "+age+" "+ sex +" "+school);
  13. }
  14. public static void main(String[] args) {
  15. Scanner input = new Scanner(System.in);
  16. System.out.println("请输入学生姓名:");
  17. String name = input.next();
  18. System.out.println("请输入学生年龄:");
  19. int age = input.nextInt();
  20. System.out.println("请输入学生性别:");
  21. String sex = input.next();
  22. System.out.println("请输入学生学校:");
  23. String school = input.next();
  24. Student stu = new Student();
  25. stu.setName(name);
  26. stu.setAge(age);
  27. stu.setSchool(school);
  28. stu.setSex(sex);
  29. StudentDao stuDao =new StudentDao();
  30. stuDao.insertStudent(stu);
  31. }
  32. }
3 某公司对固定资产进行编号,规划如下:购买年分+产品类型(1为台式机,2为笔记本 3为其他,统一采用两位数字,数字前加0)+3位随机数。如下图所示

参考代码
  1. public class GetProNo {
  2. public static void main(String[] args) {
  3. Scanner input = new Scanner(System.in);
  4. System.out.print("请输入年份: ");
  5. int year = input.nextInt();
  6. System.out.print("请选择产品类型(1. 台式机 2. 笔记本 3. 其他)");
  7. int type = input.nextInt();
  8. int random = (int)(Math.random() * 1000); //产生3位随机数
  9. String productNo = year + "0" + type + random; //产生产品编号
  10. System.out.println(" 该固定资产编号是: " + productNo);
  11. }
  12. }
4 按照月/日/年的方法输入一个日期(如8/8/2008),然后对字符串进行拆分,输出某天是哪年哪月哪日(如2008年8月8日),如下图所示

参考代码
  1. public class DeDate {
  2. public static void main(String[] args) {
  3. System.out.print("请输入一个日期(月/日/年): ");
  4. Scanner input = new Scanner(System.in);
  5. String date = input.next();
  6. int pos1 = date.indexOf("/");
  7. int pos2 = date.lastIndexOf("/");
  8. int length = date.length();
  9. String month = date.substring(0, pos1);
  10. String day = date.substring((pos1 + 1), pos2);
  11. String year = date.substring((pos2 + 1), length);
  12. System.out.println(" " + year + "年" + month + "月" + day + "日");
  13. }
  14. }
捐赠我们
    良师益友工作室一直在致力于帮助编程爱好更加快速方便地学习编程,如果您对我们的成果表示认同并且觉得对你有所帮助,欢迎您对我们捐赠^_^。
    





原文地址:https://www.cnblogs.com/imentors/p/4803691.html