使用switch时注意事项:
- 表达式类型只能是byte、short、int、char,JDK5开始支持枚举,JDK7开始支持String,不支持double、float、long(精确度问题,小数有点不精确)。
- case给出的值不允许重复,且只能是字面量,不能是变量。
- 不要忘记写break。
写for循环的快捷键:fori+回车
ctrl+shift+Alt+j 可以选中所有的和当前一样的部分,然后一起修改
//生成随机数,1-100之间(两种方法)int num1 = (int)(Math.random()*100) + 1;Random r = new Random();int num2 = r.nextInt(100) + 1;
随机数是前闭后开的,如何生成65-91之间的随机数?
答:int number = r.nextInt(27)+65;//r.nextInt(27)是生成0-26之间的随机数,加上65就是65-91之间了
小案例:随机生成n
public static String getCode(int n){String code = "";for (int i = 0; i < n; i++) {int type = (int)(Math.random()*3);//0代表数字,1代表大写字母,2代表小写字母switch (type) {case 0:code += (int)(Math.random()*10);break;case 1:code += (char)(Math.random()*26+'A');break;case 2:code += (char)(Math.random()*26+'a');break;}}return code;}
静态初始化数组:数据类型[ ] 数组名 = {元素1,元素2,...} 例:int[ ] arr = {12,24,36};
动态初始化数组:数据类型[ ] 数组名 = new 数据类型[长度] 例:int[ ] arr = new int[3];
数组名.fori +回车,快捷键可以快速写出 for(int i = 0;i<nums.length;i++)
二维数组静态:int[][] arr={{1,2,3},{4,5,6},{7,8,9}};
二维数组动态:int[][] arr = new int[3][5];