Java基础:乘法表、验证码、数据拷贝、素数、三角形

学习目标:

回顾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();
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值