[关闭]
@nextleaf 2018-08-07T19:03:51.000000Z 字数 6200 阅读 706

2018-08-07 工作日志

Java 工作日志


清单:

Keep:今日完成了哪些工作

Problem:遇到了什么问题?

sssss

Try:准备尝试哪些措施

sssss

今日未完

sss

Plan:明日的任务

sssssss

具体内容:

上午

  1. class StudentsTest {
  2. String studentname;//null
  3. char studentgender;//'\u0000'
  4. int studentage;//0
  5. public StudentsTest(String studentname, char studentgender, int studentage) {
  6. this.studentname = studentname;
  7. this.studentgender = studentgender;
  8. this.studentage = studentage;
  9. //System.out.println("学生类带参构造方法");
  10. }
  11. public StudentsTest() {System.out.println("学生类空参构造方法");}
  12. //To introduce myself
  13. public String selfIntroduction(){
  14. return "我叫" +studentname+
  15. ",是一名" +studentage+
  16. "岁的" +studentgender+
  17. "学生";
  18. }
  19. }
  20. public class StudentsArrayTest{
  21. static int averageAge;
  22. public static void main(String[] args){
  23. StudentsTest [] studentsTests={
  24. new StudentsTest("huang",'a',10),
  25. new StudentsTest(),
  26. new StudentsTest("wang",'男',11),
  27. new StudentsTest("jing",'男',12),
  28. new StudentsTest("li",'男',13),
  29. new StudentsTest("sun",'女',14),
  30. new StudentsTest("liu",'女',15),
  31. new StudentsTest("Li",'b',16),
  32. new StudentsTest("gao",'女',17),
  33. new StudentsTest("zhao",'女',18)
  34. };
  35. statistics(studentsTests);
  36. //average(studentsTests);
  37. }
  38. public static void statistics(StudentsTest[] students){
  39. int numberOfMale=0;
  40. int numberOfFemale=0;
  41. int unknown=0;
  42. int age=0;
  43. for (StudentsTest student:students){
  44. age+=student.studentage;
  45. //if (student.studentgender=='\u0000'){}
  46. if ('男'==student.studentgender){
  47. numberOfMale++;
  48. System.out.println(student.selfIntroduction());
  49. }else if ('女'==student.studentgender){
  50. numberOfFemale++;
  51. System.out.println(student.selfIntroduction());
  52. }else {
  53. unknown++;
  54. System.out.println("性别为空或无法判别");
  55. }
  56. }
  57. averageAge=age/students.length;
  58. System.out.println(numberOfMale+"男"+numberOfFemale+"女,还有"+unknown+"个性别未知");
  59. //System.out.println("男女比例:1:"+((double)numberOfFemale/numberOfMale));
  60. System.out.println("平均年龄:"+averageAge);
  61. }
  62. /* public static void average(StudentsTest[] students){
  63. int age=0;
  64. for (StudentsTest student:students){
  65. age+=student.studentage;
  66. }
  67. averageAge=age/students.length;
  68. System.out.println("平均年龄:"+averageAge);
  69. }*/
  70. }

运行结果

学生类空参构造方法
性别为空或无法判别
性别为空或无法判别
我叫wang,是一名11岁的男学生
我叫jing,是一名12岁的男学生
我叫li,是一名13岁的男学生
我叫sun,是一名14岁的女学生
我叫null,是一名15岁的女学生
性别为空或无法判别
我叫gao,是一名17岁的女学生
我叫zhao,是一名18岁的女学生
3男4女,还有3个性别未知
平均年龄:12

  1. package com.nl.sx807;
  2. /**
  3. * Created with IntelliJ IDEA 2018.
  4. * Description:
  5. * User: 14472
  6. * Date: 2018-08-07
  7. * Time: 10:32
  8. */
  9. class Waterglass {
  10. String owner;
  11. String content;
  12. public Waterglass() {}
  13. public Waterglass(String owner, String content) {
  14. this.owner = owner;
  15. this.content = content;
  16. }
  17. public static void exchange(Waterglass g1,Waterglass g2){
  18. show(g1);show(g2);
  19. Waterglass g=new Waterglass();
  20. g.owner=g2.owner;
  21. g2.owner=g1.owner;
  22. g1.owner=g.owner;
  23. show(g1);show(g2);
  24. }
  25. public static void change(Waterglass waterglass){
  26. waterglass=new Waterglass("张无忌","可口可乐");
  27. show(waterglass);
  28. }
  29. public static Waterglass loadWater(Waterglass w,String c){
  30. w.content=c;
  31. return w;
  32. }
  33. public static Waterglass pourWater(Waterglass waterglass){
  34. waterglass.content=null;
  35. return waterglass;
  36. }
  37. public static void show(Waterglass waterglass){
  38. System.out.println(waterglass.owner+"的杯里装着"+waterglass.content);
  39. }
  40. }
  41. public class WaterglassTest{
  42. public static void main(String[] args){
  43. Waterglass waterglass1=new Waterglass("张三丰","碧螺春");
  44. Waterglass waterglass2=new Waterglass("凤九","普洱茶");
  45. Waterglass waterglass3=new Waterglass("我","白开水(烧的不错)");
  46. Waterglass.show(waterglass1);
  47. Waterglass.show(waterglass2);
  48. Waterglass.show(waterglass3);
  49. System.out.println();
  50. System.out.println("改变");
  51. Waterglass.change(waterglass1);
  52. Waterglass.show(waterglass1);
  53. System.out.println();
  54. System.out.println("交换");
  55. Waterglass.exchange(waterglass1,waterglass2);
  56. System.out.println();
  57. System.out.println("装入");
  58. Waterglass.show(Waterglass.loadWater(waterglass3,"金桔柠檬"));
  59. System.out.println("倒掉");
  60. Waterglass.show(Waterglass.pourWater(waterglass1));
  61. }
  62. }

==和equals

1)对于==
如果作用于基本数据类型的变量,则直接比较其存储的 “值”是否相等
如果作用于引用类型的变量,则比较的是所指向的对象的地址
2)对于equals方法,注意:equals方法不能作用于基本数据类型的变量
如果没有对equals方法进行重写,则比较的是引用类型的变量所指向的对象的地址;
诸如String、Date等类对equals方法进行了重写的话,比较的是所指向的对象的内容。

下午


手写作业(x2)

统计学生对象数组中男女生人数以及平均年龄

  1. class StudentsTest {
  2. String studentname;//null
  3. char studentgender;//'\u0000'
  4. int studentage;//0
  5. public StudentsTest(String studentname, char studentgender, int studentage) {
  6. this.studentname = studentname;
  7. this.studentgender = studentgender;
  8. this.studentage = studentage;
  9. }
  10. public StudentsTest() {}
  11. }
  12. public class StudentsArrayTest{
  13. static int averageAge;
  14. public static void main(String[] args){
  15. StudentsTest [] studentsTests={
  16. new StudentsTest("huang",'a',10),
  17. new StudentsTest(),
  18. new StudentsTest("wang",'男',11),
  19. new StudentsTest("jing",'男',12),
  20. new StudentsTest("li",'男',13),
  21. new StudentsTest("sun",'女',14),
  22. new StudentsTest(null,'女',15),
  23. new StudentsTest("Li",'b',16),
  24. new StudentsTest("gao",'女',17),
  25. new StudentsTest("zhao",'女',18)
  26. };
  27. statistics(studentsTests);
  28. }
  29. public static void statistics(StudentsTest[] students){
  30. int numberOfMale=0;
  31. int numberOfFemale=0;
  32. int age=0;
  33. for (StudentsTest student:students){
  34. age+=student.studentage;
  35. if ('男'==student.studentgender){
  36. numberOfMale++;
  37. }else if ('女'==student.studentgender){
  38. numberOfFemale++;
  39. }else {;}
  40. }
  41. averageAge=age/students.length;
  42. System.out.println(numberOfMale+"男"+numberOfFemale+"女");
  43. System.out.println("平均年龄:"+averageAge);
  44. }
  45. }

static用法-修饰方法举例

  1. class Waterglass {
  2. String owner;
  3. String content;
  4. public Waterglass() {}
  5. public Waterglass(String owner, String content) {
  6. this.owner = owner;
  7. this.content = content;
  8. }
  9. public static void change(Waterglass waterglass){
  10. waterglass=new Waterglass("张无忌","可口可乐");
  11. show(waterglass);
  12. }
  13. public static void show(Waterglass waterglass){
  14. System.out.println(waterglass.owner+"的杯里装着"+waterglass.content);
  15. }
  16. }
  17. public class WaterglassTest{
  18. public static void main(String[] args){
  19. Waterglass waterglass1=new Waterglass("张三丰","碧螺春");
  20. Waterglass.show(waterglass1);
  21. Waterglass.change(waterglass1);
  22. Waterglass.show(waterglass1);
  23. }
  24. }

Ctrl+Shift+J将两行合并为一行,并删除不必要的空间以匹配代码样式
使用Refactor | Copy创建一个类,该类是所选类的副本。例如,当您需要创建一个与一些现有类有很多共同点的类,并且不可能将共享功能放在公共超类中时,这可能是有用的。
Ctrl+Shift+V选择并插入文本中最近的剪贴板内容。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注