publicclassPractice2{publicstaticvoidmain(String[] args){int count =0;for(int i =101; i <201; i++){boolean flag =true;for(int x =2; x <= i /2; x++){if(i % x ==0){
flag =false;break;}}if(flag){
count++;System.out.print(i +" ");}}System.out.println("为素数,素数有"+ count +"个");}}
2.生成随机验证码
packageWeek2day1;importjava.util.Random;importjava.util.Scanner;//生成指定数目的验证码,可能含有大小写字母或者数字publicclassPractice3{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);System.out.println("请输入你想要生成的验证码的位数:");int num = scanner.nextInt();Random random =newRandom();//这里是重点for(int i =0; i < num; i++){String code ="";//定义一个字符串接收随机产生的int type = random.nextInt(3);switch(type){case0:{char a =(char)(random.nextInt(26)+65);
code += a;break;}case1:{int b = random.nextInt(10);
code += b;break;}case2:{char c =(char)(random.nextInt(26)+97);
code += c;break;}}System.out.print(code);}}}