打印九九乘法表
关于for循环的使用
package com.chen;
public class Demo01 {
public static void main(String[] args) {
for (int i = 1; i <=9 ; i++) {
for (int j = 1; j <=i ; j++) {
System.out.print(j+"*"+i+"="+(i*j)+'\t');
}
System.out.println();
}
}
}
打印三角形
for循环的加强使用,加深理解
package com.chen;
public class Demo01 {
public static void main(String[] args) {
for (int i = 0; i <10 ; i++) {
for (int j = 10; j >=i ; j--) {
System.out.print(" ");
}
for (int j = 0; j <i ; j++) {
System.out.print("*");
}
for (int j = 0; j <=i ; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
冒泡排序
升序的冒泡排序,如果前面的数字大于后面的数字,则进行交换顺序,同时也要处理好j的范围
package com.chen;
import java.util.Arrays;
public class Demo01 {
public static void main(String[] args) {
int[] n = {5,45,12,48,4,51,12,115,45,12,48,41,2};
for (int i = 0; i <n.length-1 ; i++) {
for (int j = 0; j <n.length-1-i ; j++) {
if(n[j]>n[j+1])
{
int temp=n[j];
n[j]=n[j+1];
n[j+1]=temp;
}
}
}
System.out.println(Arrays.toString(n));
}
}