Java 是静态类型语言,变量必须先声明类型。基本类型包括 int、long、double、boolean、char 等。
本节要点
- String 是不可变引用类型。
- if、switch、for、while 语法与多数语言类似。
- 增强 for 循环适合遍历数组和集合。
代码示例
public class Demo {
public static void main(String[] args) {
int[] scores = {88, 92, 76};
int sum = 0;
for (int score : scores) {
sum += score;
}
double avg = (double) sum / scores.length;
System.out.println("平均分:" + avg);
}
}