/**
* 九九乘法表
*/
public class LoopFor07 {
public static void main(String[] args) {
for(int i=1;i<10;i++){
for(int j=i;j<10;j++) {
System.out.print(i + " x " + j + " = " + (i * j)+"\t");
}
System.out.println();
}
}
}
2.随机骰子
public class LoopFor06 {
public static void main(String[] args) {
//创建随机数对象
Random ran = new Random(System.currentTimeMillis());
int count=0;
boolean condition = true;
while(condition){
int num1 = ran.nextInt(6)+1;
System.out.print(num1+"---");
int num2 = ran.nextInt(6)+1;
System.out.print(num2+"---");
int num3 = ran.nextInt(6)+1;
System.out.print(num3);
count++;
System.out.println();
if((num1==6)&&(num2==6)&&(num3==6)){
condition = false;
}
}
System.out.println("一共掷出骰子"+count+"次");
}
}
3.for循环的简单应用
public class LoopFor04 {
public static void main(String[] args) {
for (int i = 1; i < 5; i++) {
for (int k = 4; k > i; k--) {
System.out.print(" ");
}
for (int j = 0; j < 2 * i - 1; j++) {
System.out.print("*");
}
for (int p = 4; p > i; p--) {
System.out.print(" ");
}
System.out.println();
}
System.out.println(" $ * $ ");
System.out.println(" * ");
System.out.println(" * ");
}
}