今日識訓:
一、所有參考型別變數的初始化一定要使用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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/160282.html
標籤:Java
上一篇:嘆為觀止!GitHub標星過萬,騰訊技術官發布的這份“神仙檔案”圖解網路,簡直是秋招福音
下一篇:怎么才算掌握了JDK中的執行緒池
