今日收获:

一、所有引用类型变量的初始化一定要使用new 关键字定义声明,空指针异常的错误原因可能是变量没有初始化导致的。

每一个类体的数据成员一定要在实例化的同时赋值,用一个实例化的类实现问题中最小的一部分,比如四则运算题目,一个类就是一个算式,问题要极度分解简化。

  函数之间的返回值和参数

二、定义int 或者 char 类型的数组   :        int [ ]a=new int [10];    int char [ ]c=new char [10];

三、Random的使用  Random r1=new Random(seed);       这里的seed可以用 System.nanotime获取时间随机种子

Random 函数    r1.nextInt( ) ; 指定随机数的范围;      r1.nextInt( max) %(max-min+1)+min;      这表示从 min 到 max 的随机数

r1.nextInt( num) ; 表示  从 0   ---  num-1  的范围随机数。

  随机数的产生有三种方式

       第一种:new Random()

   第二种:Math.random()

   第三种:currentTimeMillis()

    第二种方法返回的数值是[0.0,1.0)的double型数值

 1 public class RandomTest {
2 public static void main(String[] args) {
3 int max=20;
4 int min=10;
5 Random random = new Random();
6
7 /*
8 random.nextInt(max)表示生成[0,max]之间的随机数,然后对(max-min+1)取模。
9 以生成[10,20]随机数为例,首先生成0-20的随机数,然后对(20-10+1)取模得到[0-10]之间的随机数,然后加上min=10,最后生成的是10-20的随机数
10 */
11 int s = random.nextInt(max)%(max-min+1) + min;
12 System.out.println(s);
13 }
14 }

四、ArrayList 数组存储数据要一个一个存,那么取数据也一定是一个一个取出来,不然容易出错!

**************************************************************************************************************

理解空指针异常错误产生的原因(图片来自 https://blog.csdn.net/qq_44543508/article/details/94589868?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.channel_param)

总之,NullPointerException由RuntimeException派生出来,是一个运行时异常。其意指可能会在运行的时候才会被抛出,一个变量是null,及只有其名,没有实值内容,也没分配内存,当你要去取他的长度,对他进行操作就会出现NullPointException,所以声明变量时最好给它分配好内存空间,给予赋值,例如拿该变量与一个值比较时,要么先做好该异常的处理要么给它进行判断先: if (str !=null && str “”){ …}
判断一个String的实例s是否等于“a”时,不要写成s.equals(“a”),这样容易抛NullPointerException,而写成"a".equals(s)就可以避免这个问题,不过对变量先进行判空后再进行操作更好,尽量避免返回null,方法的返回值不要定义成为一般的类型,用数组。这样如果想要返回null的时候,就返回一个没有元素的数组。就能避免许多不必要的NullPointerException

拿到一个题之后,先考虑一个类可以做的最基本的事情是什么,哪些数据是在类里面的,哪些数据是要在主方法的,定义方法的时候,一定每次都要考虑方法的参数和返回值,调用关系是否可以满足。尽量不要采用“散装”的数据变量来存储数据,要用高级的数据类型,数组等等,方便数据之后的存取。定义变量必须初始化赋值,实例化对象要给每一个成员变量赋初值!!!

类的实例化对象调用自己的类是不需要再传递一个本类参数的,直接在方法中使用this. 调用就可以使用成员变量!!!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

测试二阶段一:

FourOperations.java

  1 package fouroperations;
2 import java.util.*;
3 public class FourOperations {
4 /*
5 * 总体思路,一个实例化的类就是一个算式,里面有单独的几个函数
6 * 判断重复函数,需要一个参数引用所有的实例化类。
7 * 在主类定义一个ArrayList类存储每一个实例化的 FourOperations类
8 *
9 *
10 *
11 *
12 * 1,定义一个数组存储生成的指定数量的操作数(2---5)个,那么就只需要一个随机数生成的变量,
13 *
14 * 2,定义一个char类型的数组 来存储4个符号 + - * /;
15 *
16 *
17 */
18 private int operationnum; //随机数个数
19 private int max; //最大值
20 private int min; //最小值
21 private String formulas; //最终产生的完整的算式
22 String symbol; //运算符号
23 private int []allnumber=new int[5]; //操作数数组
24 private int tempnum_1;
25 private Random r1=new Random(10); //随机数变量
26
27 public int getOperationnum() {
28 return operationnum;
29 }
30 public void setOperationnum(int operationnum) {
31 this.operationnum = operationnum;
32 }
33 public int getMax() {
34 return max;
35 }
36 public void setMax(int max) {
37 this.max = max;
38 }
39 public int getMin() {
40 return min;
41 }
42 public void setMin(int min) {
43 this.min = min;
44 }
45 public String getFormulas() {
46 return formulas;
47 }
48 public void setFormulas(String formulas) {
49 this.formulas = formulas;
50 }
51 public String getSymbol() {
52 return symbol;
53 }
54 public void setSymbol(String symbol) {
55 this.symbol = symbol;
56 }
57 FourOperations(){}; //无参构造
58 /*
59 * 定义初始化变量函数
60 * 参数
61 * int operationnum,int max,int min
62 */
63 public void init(int operationnum,int max,int min) {
64 this.setOperationnum(operationnum);
65 this.setMax(max);
66 this.setMin(min);
67 }
68
69 /*
70 * 定义产生随机数的函数,生成算式String对象
71 * 参数:
72 * 无
73 */
74
75 public void creatNum( )
76 {
77 int tempnum_1=0;
78 for(int j=0;j<this.operationnum;j++) {
79 this.r1=new Random(System.nanoTime());
80 tempnum_1=this.r1.nextInt(this.max)%(this.max-this.min+1) +this.min;
81 this.allnumber[j]=tempnum_1;
82 }
83 switch(this.operationnum) {
84 case 2:
85 this.formulas=""+this.allnumber[0]+this.creatSymbol()+this.allnumber[1];
86 break;
87 case 3:
88 this.formulas=""+this.allnumber[0]+this.creatSymbol()+this.allnumber[1]+
89 this.creatSymbol()+this.allnumber[2];
90 break;
91 case 4:
92 this.formulas=""+this.allnumber[0]+this.creatSymbol()+this.allnumber[1]+
93 this.creatSymbol()+this.allnumber[2]+this.creatSymbol()+this.allnumber[3];
94 break;
95 case 5:
96 this.formulas=""+this.allnumber[0]+this.creatSymbol()+this.allnumber[1]+
97 this.creatSymbol()+this.allnumber[2]+this.creatSymbol()+this.allnumber[3]+
98 this.creatSymbol()+this.allnumber[4];
99 break;
100 default : break;
101 }
102 }
103 /*
104 * 定义查重函数;
105 * 参数 ArrayList<FourOperations> arrays
106 */
107 public boolean Repeatition(ArrayList<FourOperations> arrays) {
108 boolean flag=false;
109 for(int i=0;i<arrays.size();i++) {
110 if(this.formulas.equals(arrays.get(i).formulas)) {
111 flag=true;
112 }
113 }
114 while(flag) {
115 for(int i=0;i<arrays.size();i++) {
116 if(!(this.formulas.equals(arrays.get(i).formulas)) ){
117 flag=false;
118 }
119 this.creatNum();
120 }
121 }
122 return true;
123 }
124 /*
125 * 定义产生随机符号的函数
126 * 参数 无
127 * 返回值 String 类型的一个变量
128 */
129
130 public String creatSymbol() {
131 Random r2=new Random();
132 int flag= r2.nextInt(4);
133 switch(flag) {
134 case 0:
135 this.symbol="+";
136 break;
137 case 1:
138 this.symbol="-";
139 break;
140 case 2:
141 this.symbol="*";
142 break;
143 case 3:
144 this.symbol="/";
145 break;
146 }
147 return symbol;
148 }
149
150 /*
151 * 定义输出函数 display
152 *
153 */
154
155 public static void display(int num,ArrayList<FourOperations> arrays) {
156 for(int j=0;j<num;j++) {
157 //实例化类 tempclass 接收arrays的返回值
158 FourOperations tempclass=new FourOperations();
159 tempclass=arrays.get(j);
160 System.out.println(""+(j+1)+"、"+tempclass.formulas);
161 }
162 }
163 public int getTempnum_1() {
164 return tempnum_1;
165 }
166 public void setTempnum_1(int tempnum_1) {
167 this.tempnum_1 = tempnum_1;
168 }
169
170
177 }

FourOperationsDome.java

 1 package fouroperations;
2
3 import java.lang.reflect.Array;
4 import java.util.ArrayList;
5 import java.util.Scanner;
6 public class FourOperationsDemo {
7 public static void main(String[] args) {
8 // TODO 自动生成的方法存根
9
10 int num;
11 //定义arrays数组 存储每次产生的类
12 ArrayList<FourOperations> arrays = new ArrayList<FourOperations>();
13
14 Scanner sc= new Scanner(System.in);
15 System.out.println("请输入算式的个数:");
16 num=sc.nextInt();
17 int operationnum; //随机数个数
18 int max; //最大值
19 int min; //最小值
20 System.out.println("请输入操作数的个数(2---5)");
21 operationnum=sc.nextInt();
22 //输入max min
23 while(true)
24 {
25 System.out.println("请输入操作数的最大值");
26 max=sc.nextInt();
27 System.out.println("请输入操作数的最小值");
28 min=sc.nextInt();
29 if(max>min)
30 break;
31 else {
32 System.out.println("输入的最大值要大于最小值,请检查后重新输入!");
33 }
34 }
35
36
37 boolean f=true; //查重循环条件
38 for(int i=0;i<num;i++) {
39 FourOperations test1=new FourOperations();
40 test1.init(operationnum,max,min); //调用初始化函数
41 test1.creatSymbol(); //调用生成随机运算符函数
42 test1.creatNum(); //调用生成随机式子函数
43 test1.Repeatition(arrays); //调用查重函数
44 arrays.add(test1); //把新生成的类存到ArrayList数组中
45 }
46 //调用输出函数
47 FourOperations.display(num, arrays);
48 sc.close();
49 }
50 }

@Override

重写的一定是Object类有的方法,而且一定有一个参数是Object类的一个引用。

Object类的对象obj 要进行向下转型      (MyTestClass)obj

最新文章

  1. 日志log
  2. 微软四十周年 Microsoft’s 40th anniversary
  3. JavaScript的陷阱
  4. [设计模式] javascript 之 模板方法模式
  5. Linux shell get random number
  6. Linux下修改计算机名
  7. Particle Editor 无法启动此程序,因为计算机中丢失MSCP110.dll。尝试重新安装该程序以解决此问题。
  8. (转)关于linux中内核编程中结构体的赋值操作(结构体指定初始化)
  9. Codevs_1403_新三国争霸_(Kruskal+动态规划)
  10. driver_register()函数解析
  11. hdu5145 NPY and girls
  12. MySQL索引 - 索引的类型
  13. Java并发编程-各种锁
  14. omit 配合antd from使用 hoistStatics
  15. web.xml的学习
  16. python数据结构与算法之问题求解
  17. 第一次发博,发个简单的Java程序发送手机短信验证
  18. 【mysql】---php链接数据库---【巷子】
  19. Unity shader学习之屏幕后期处理效果之边缘检测
  20. Where关键词的用法

热门文章

  1. 学习abp vnext框架到精简到我的Vop框架
  2. 原创题目 白银之春 Problem and Solution
  3. python核心高级学习总结6------面向对象进阶之元类
  4. moviepy音视频开发:音频文件存取类AudioFileClip属性和方法介绍
  5. PyQt+moviepy音视频剪辑实战1:多个音视频合成顺序播放或同屏播放的视频文件实现详解
  6. 第8.5节 Python类中的__new__方法和构造方法__init__关系深入剖析:执行顺序及参数关系案例详解
  7. KafKa简介和利用docker配置kafka集群及开发环境
  8. ASP.NET 漂亮美观的验证码
  9. 笔记-Cats Transport&lt;已写题解&gt;
  10. 7、Spring Cloud Hystrix