java入门练习题二

时间:2023-02-12 20:56:23

练习一:找出101-200之间的所有素数

package com.pratice.daily;

public class Sushu {
public static void main(String[] args) {
for(int i=101;i<=200;i++){
int j=2;
while(j<i){
if(i%j==0) break;
else j++;
}
if(j>=i) System.out.print(i+" ");
}
}
}

练习二:排序算法

package com.pratice.daily;

public class Sort {
public static void popSort(int[] b){
int n = b.length;
int countTimes = 0; //count time
for(int i=0;i<b.length;i++){
for(int j=0;j<n-1;j++){
countTimes++;
if(b[j]>b[j+1]) {
int temp = b[j+1];
b[j+1]=b[j];
b[j]=temp;
}
}
n--; //reduce count time
}
for(int i=0;i<b.length;i++){
System.out.print(b[i]+" ");
}
System.out.print(" "+countTimes);
}

public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a={1,3,99,17,10,22,5,15,0};
popSort(a);
}
}