(1)简单的99乘法表
package cn.itcast.day;
import java.util.Scanner;
public class Demo9 {
public static void main(String[] args) {
for (int x = 1; x <= 9; x++) {
for (int y = 1; y <= x; y++) {
System.out.print(y + "*" + x + "=" + x * y + '\t');
}
System.out.println(" ");
}
}
}
运行结果如下:
(2)增强型for循环
foreach循环
Java 增强 for 循环语法格式如下:
for(声明语句 : 表达式) { //代码句子 } |
声明语句:
声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:
表达式是要访问的数组名,或者是返回值为数组的方法。
for(数组类型 变量名 : 数组名){
System.out.println(变量名);
}
示例:
int[] a = {1,2,3,4,5,6}; //增强for循环 for(int i : a){ System.out.println(i); } |