一,菱形
/**
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
**/
//for循环
public class Demo01 {
public static void main(String[] args) {
int i,j;
for (i=1;i<=5;i++){
for (j = 5; j >= i; j--){
System.out.print(" ");
}
for (j=0; j < i * 2 - 1; j++){
System.out.print(" * ");
}
System.out.println();
}
for (i=1;i<=5;i++){
for (j = 0; j <= i;j++){
System.out.print(" ");
}
for (j=8; j > i * 2 - 1; j--){
System.out.print(" * ");
}
System.out.println();
}
}
}
二,数字炸弹游戏
import java.util.Random;
import java.util.Scanner;
public class Demo09 {
public static void main(String[] args) {
System.out.println("数字炸弹游戏(0~100)");
Scanner key = new Scanner(System.in);
Random rd = new Random();
int a = rd.nextInt(100)+1;
while (true){
System.out.println("请输入一个数进行比较:");
int b = key.nextInt();
if (b>a){
System.out.println("猜大了,请您继续猜");
}else if(b<a){
System.out.println("猜小了,请您继续猜");
}else if (b==a){
System.out.println("恭喜您,猜对了");
break;
}
}
}
}
三,六行六列的三角形
public class Demo06 {
public static void main(String[] args) {
for (int i=1;i<=6;i++){
for (int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println();
}
}
}
四,九九乘法表
public class Demo07 {
public static void main(String[] args) {
for (int i=1;i<=9;i++){
for (int j=1;j<=i;j++){
System.out.print(i+"*"+j+"="+i*j);
}
System.out.println();
}
}
}
本文通过三个Java编程实例展示了如何使用for循环绘制菱形图案、实现数字炸弹游戏以及打印六行六列的三角形,并附带了一个九九乘法表的代码示例。
&spm=1001.2101.3001.5002&articleId=119378364&d=1&t=3&u=b731c95ecf994c7393c002a3f4660d38)
1万+

被折叠的 条评论
为什么被折叠?



