学习目标:
回顾Java基础
学习内容:
1.生成随机验证码
public static String createCode(int n){
Ramdom r = new Random();
String code = "";
for(int i=0; i<n; i++){
// 0代表数字,1代表大写字母,2代表小写字母
int type = r.nextInt(3);
// 遇到数值判断用switch
switch(type) {
case 0:
code += r.nextInt(10);
break;
case 1:
// A:97 Z:97+25 (0-25)+97
char c1 = (char)r.nextInt(26)+97;
code += c1;
break;
case 2:
// a:65 z:65+25 (0-25)+65
char c2 = (char)r.nextInt(26)+65;
code += c2;
break;
}
}
return code;
}
2.数据拷贝
public static void print(int[] arr){
sout("[");
for(int i=0; i<arr.length; i++){
sout(i == arr.length-1 ? arr[i] : arr[i] + ",")
}
sout("]")
}
public static int[] copy(int[] arr){
int[] arr2 = new int[arr.length];
for(int i=0; i<arr.length; i++){
arr2[i] = arr[i];
}
return arr2
}
3.判断是否为素数
public static void main(String[] args) {
for(int i=101; i<=200; i++){
if(check(i)){
count++;
System.out.println(i);
}
}
System.out.println("当前素数个数是:" + count);
}
pubic static boolean check(int data){
for(int i=2; i<=data/2; i++){
if(data % i == 0){
return false;
}
}
return true;
}
4.打印乘法表
public static void main(String[] args){
// 9行
for(int i=1; i<=9; i++){
//i 1 2 3 4 5 6 7 8 9
for(int j=1; j<=i; j++){
//i 行 j列
System.out.print(i + "*" + j + "=" + (i*j) + "\t");
}
System.out.println();
}
}
5.打印三角形
public static void main(String[] args){
int n=4;
for(int i=1; i<=n; i++){
//打印空格
for(int j=0; j<(n-i); j++){
System.out.print(" ");
}
//打印*
for(int j=0; j<(2*i-1); j++){
System.out.print("*");
}
//换行
System.out.println();
}
}