Java嵌套循环(乘法口诀和冒泡法排序为例)
1、乘法口诀
public class BubbleSort {
public static void main(String[] args) {
int i,j = 0;
for (i=1;i<10;i++) {
for(j=1;j<i+1;j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
}
2、冒泡法排序:
public class BubbleSort {
public static void main(String[] args) {
int[] arr={6,3,8,2,9,1};
for (int i=0;i<arr.length-1;i++) {
for(int j=0;j<arr.length-1-i;j++) {
if (arr[j]>arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(int num:arr){
System.out.print(num+" ");
}
}
}
联系QQ邮箱:2433973080@qq.com

浙公网安备 33010602011771号