二維數(shù)組的定義格式
- 靜態(tài)賦值
int[][] nums = {{0,1,2},{0,1,2,3}}; //靜態(tài)賦值
/*打印輸出:0 1 2
0 1 2 3
*/
- 動態(tài)賦值
int[][] 二維數(shù)組名稱 = new int[行][列(選填)]; //二維數(shù)組中的每行的列數(shù)可以不同汰扭,填了就固定了每行的列數(shù)
int[][] nums = new int[2][]; //動態(tài)賦值
a[0] = new int[3];
a[1] = new int[4];
for(int i = 0; i < 3; i++) {
a[0][i] = i;
}
for(int j= 0; j < 4; j++) {
a[1][j] =j;
}
/*打印輸出:0 1 2
0 1 2 3
*/
取模
小num % 大num = 小num
大num % 小num = 大num - 小num (大num < 2*小num)
偽隨機數(shù)
import java.util.Random;
...
Random random = new Random(); //創(chuàng)建一個用于生成偽隨機數(shù)的工具
int num1 = random.nextInt(100); //生成介于[0,100)間的隨機整數(shù)
double num2 = random.nextDouble(); //生成[0,1.0)區(qū)間的小數(shù)
double num3 = random.nextDouble()* 5; //生成[0,5.0)區(qū)間的小數(shù)
double num4 = random.nextDouble() * 1.5 + 1; //生成[1,2.5)區(qū)間的小數(shù)