①:定义两个变量,并同时计数
package Z1;
public class Test2 {
public static void main(String[] args) {
int sum = 0;
for (int a = 1, b = 100; a < b; a++, b--) {
sum += a + b;
}
System.out.println(sum);
}
}
②:5乘5矩阵
public class Test3 {
public static void main(String[] args) {
int i, j, k;
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
System.out.print("");
}
for (k = 0; k < 5; k++) {
System.out.print("*");
}
System.out.println();
}
}
运行效果:
③:九九乘法表
public class Test4 {
public static void main(String[] args) {
int i, j;
for (i = 1; i < 10; i++) {
for (j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\\t");
}
System.out.println();
}
for (i = 9; i > 0; i--) {
for (j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + i * j + "\\t");
}
System.out.println();
}
}
}
④:金字塔
package Z1;
public class Test5 {
public static void main(String[] args) {
int i, j;
for (i = 1; i <= 9; i++) {
for (j = 9 - i; j >= 0; j--) {
System.out.print(" "); // 输出左边的空格
}
for (j = 1; j <= 2 * i - 1; j++) {
System.out.print("*"); // 输出金字塔
}
System.out.println();
}
}
}
运行效果