publicclassMain{publicstaticvoidmain(String[] args){int count =0;for(int i =101; i <=200; i++){boolean flag =true;for(int j =2; j < i; j++){if(i % j ==0){
flag =false;break;}}if(flag){System.out.println(i +"是一个质数");
count++;}}System.out.println("一共有"+ count +"个质数");}}
四、开发验证码
需求:定义方法实现随机产生一个5位的验证码。
验证码格式:
长度为5
前四位是大写字母或者小写字母
最后一位是数字
importjava.util.Random;publicclassMain{publicstaticvoidmain(String[] args){String code =getCode();System.out.println("验证码是 "+ code);}publicstaticStringgetCode(){String result ="";char[] chs =newchar[52];for(int i =0; i < chs.length; i++){if(i <26){
chs[i]=(char)(97+ i);}else{
chs[i]=(char)(65+ i -26);}}Random random =newRandom();for(int i =0; i <4; i++){int randomIndex = random.nextInt(chs.length);
result += chs[randomIndex];}int number = random.nextInt(10);
result += number;return result;}}
五、数组元素的复制
需求:把一个数组中的元素复制到另一个新数组中去。
importjava.util.Arrays;publicclassMain{publicstaticvoidmain(String[] args){int[] oldArr ={1,2,3,4,5};int[] newArr =copyArr(oldArr);System.out.println(Arrays.toString(newArr));}publicstaticint[]copyArr(int[] arr){int[] newArr =newint[arr.length];for(int i =0; i < arr.length; i++){
newArr[i]= arr[i];}return newArr;}}
六、评委打分
需求:在唱歌比赛中,有6名评委给选手打分,分数范围是[0~100]之间的整数。
选手的最后得分为:去掉最高分、最低分后的4个评委的平均分,请完成上述过程并计算出选手的得分。
importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[] args){int[] scores =getScores();int max =getMax(scores);int min =getMin(scores);int sum =getSum(scores);int avg =(sum - max - min)/(scores.length -2);System.out.println("选手的最终得分为:"+ avg);}publicstaticint[]getScores(){int[] scores =newint[6];Scanner scanner =newScanner(System.in);for(int i =0; i < scores.length;){System.out.println("请输入第"+(i +1)+"个评委的打分:");int score = scanner.nextInt();if(score >=0&& score <=100){
scores[i]= score;
i++;}else{System.out.println("成绩超出范围,继续录入,当前是"+(i +1)+"第个评委");}}return scores;}publicstaticintgetMax(int[] arr){int max = arr[0];for(int i =0; i < arr.length; i++){if(arr[i]> max){
max = arr[i];}}return max;}publicstaticintgetMin(int[] arr){int min = arr[0];for(int i =0; i < arr.length; i++){if(arr[i]< min){
min = arr[i];}}return min;}publicstaticintgetSum(int[] arr){int sum =0;for(int i =0; i < arr.length; i++){
sum += arr[i];}return sum;}}
七、数字加密
需求:某系统的数字密码(大于0),比如1983,采用加密方式进行传输。
规则:先得到每位数,然后每位数都加上5,在对10求余,最后将所有数字反转,得到一串新数。
publicclassMain{publicstaticvoidmain(String[] args){// 定义一个数int number =1983;// 计算数字有多少位int count =getNumberCount(number);// 定义一个数组,元素个数为数字位数int[] arr =getArr(number, count);// 加密int[] encryptArr =encrypt(arr);// 拼接int result =getResult(encryptArr);System.out.println(result);// 8346}privatestaticintgetResult(int[] encryptArr){int result =0;for(int i =0; i < encryptArr.length; i++){
result = result *10+ encryptArr[i];}return result;}privatestaticint[]encrypt(int[] arr){for(int i =0; i < arr.length; i++){
arr[i]= arr[i]+5;}for(int i =0; i < arr.length; i++){
arr[i]= arr[i]%10;}for(int i =0, j = arr.length-1; i < j; i++,j--){int t = arr[i];
arr[i]= arr[j];
arr[j]= t;}return arr;}privatestaticint[]getArr(int temp,int count){int[] arr =newint[count];// 把数字一个一个塞进数组int index = arr.length -1;while(temp !=0){int everyNumber = temp %10;
temp = temp /10;
arr[index]= everyNumber;
index--;}return arr;}privatestaticintgetNumberCount(int number){int count =0;while(number !=0){
number = number /10;
count++;}return count;}}
八、数字解密
需求:把上面加密的数据解密。
publicclassMain{publicstaticvoidmain(String[] args){// 定义需要解密的数字int number =8346;// 获取位数int count =getNUmberCount(number);// 把数字塞进数组int[] arr =getArr(number, count);// 解密int result =decode(arr);System.out.println(result);// 1983}privatestaticintdecode(int[] arr){// 反转for(int i =0, j = arr.length -1; i < j; i++, j--){int temp = arr[i];
arr[i]= arr[j];
arr[j]= temp;}for(int i =0; i < arr.length; i++){if(arr[i]>=0&& arr[i]<=4){
arr[i]= arr[i]+10;}}for(int i =0; i < arr.length; i++){
arr[i]= arr[i]-5;}int result =0;for(int i =0; i < arr.length; i++){
result = result *10+ arr[i];}return result;}privatestaticint[]getArr(int number,int count){int[] arr =newint[count];for(int i =0; i < count; i++){int everyNumber = number %10;
number = number /10;
arr[count - i -1]= everyNumber;}return arr;}privatestaticintgetNUmberCount(int number){int count =0;while(number !=0){
number = number /10;
count++;}return count;}}
publicclassMain{publicstaticvoidmain(String[] args){int[][] yearArrArr ={{22,66,44},{77,33,88},{25,45,65},{11,66,99}};int yearSum =0;for(int i =0; i < yearArrArr.length; i++){int[] quarterArr = yearArrArr[i];int sum =getSum(quarterArr);System.out.println("第"+ i +"个季度的总营业额为:"+ sum);
yearSum += sum;}System.out.println("全年总营业额为:"+ yearSum);}publicstaticintgetSum(int[] arr){int sum =0;for(int i =0; i < arr.length; i++){
sum += arr[i];}return sum;}}