public class ArithmeticDemo {
public static void main(String[] args) {
int a = 2;
int b = 3;
System.out.println("a+b=" + (a + b)); // 输出5
System.out.println("a%b=" + (a % b)); // 输出2
System.out.println("a++=" + (a++)); // 输出2,a变为3
System.out.println("++a=" + (++a)); // 输出4,a变为4
}
}
public class AssignmentDemo {
public static void main(String[] args) {
short s = 3;
int i = 5;
s += i; // 自动强制转换,等价于s=(short)(s+i)
System.out.println("s=" + s); // 输出8
int x = 10;
x *= 2; // 等价于x=x*2
System.out.println("x=" + x); // 输出20
}
}
public class LogicDemo {
public static void main(String[] args) {
int x = 0, y = 0, z = 0;
boolean a = x > 0 & y++ > 1; // 右边执行,y变为1
boolean b = x > 0 && z++ > 1; // 右边不执行,z仍为0
System.out.println("a=" + a + ", y=" + y); // 输出a=false, y=1
System.out.println("b=" + b + ", z=" + z); // 输出b=false, z=0
}
}
(五)运算符优先级
核心规则:括号()优先级最高,算术运算>比较运算>逻辑运算>赋值运算。
编程案例:
java复制代码
public class PriorityDemo {
public static void main(String[] args) {
int a = 2;
int b = a + 3 * a; // 先算3*a=6,再算2+6=8
int c = (a + 3) * a; // 先算括号内a+3=5,再算5*2=10
System.out.println("b=" + b + ", c=" + c); // 输出b=8, c=10
}
}
二、选择结构语句
(一)if条件语句
三种形式:单条件if、双条件if-else、多条件if-else if-else。
关键规则:判断条件为布尔值,{}可省略但建议保留,避免逻辑错误。
编程案例:
java复制代码
public class IfDemo {
public static void main(String[] args) {
int grade = 75;
if (grade > 80) {
System.out.println("等级:优");
} else if (grade > 70) {
System.out.println("等级:良");
} else if (grade > 60) {
System.out.println("等级:中");
} else {
System.out.println("等级:差");
}
}
}
(二)三元运算符
语法格式:判断条件 ? 表达式1 : 表达式2,条件为true返回表达式1,否则返回表达式2。
关键规则:优先级低于算术和比较运算符,高于赋值运算符,可嵌套使用。
编程案例:
java复制代码
public class TernaryDemo {
public static void main(String[] args) {
int x = 5, y = 3;
int max = x > y ? x : y; // 取x和y的最大值
int result = x > 10 ? (x * 2) : (y + 5); // 嵌套使用
System.out.println("max=" + max + ", result=" + result); // 输出max=5, result=8
}
}
public class SwitchDemo {
public static void main(String[] args) {
int week = 5;
switch (week) {
case 1:
case 2:
case 3:
case 4:
case 5:
System.out.println("今天是工作日");
break;
case 6:
case 7:
System.out.println("今天是休息日");
break;
default:
System.out.println("输入错误");
}
}
}
三、循环结构语句
(一)while循环
语法格式:while(循环条件){循环体},适用于循环次数不确定的场景。
关键规则:先判断条件再执行循环体,需确保循环条件能最终为false,避免无限循环。
编程案例:
java复制代码
public class WhileDemo {
public static void main(String[] args) {
int x = 1;
while (x <= 4) { // 循环4次
System.out.println("x=" + x);
x++; // 更新循环变量
}
}
}
(二)do-while循环
语法格式:do{循环体}while(循环条件);,循环体至少执行一次。
关键规则:先执行循环体再判断条件,末尾需加英文分号。
编程案例:
java复制代码
public class DoWhileDemo {
public static void main(String[] args) {
int x = 1;
do {
System.out.println("x=" + x);
x++;
} while (x <= 4); // 输出1、2、3、4
}
}
(三)for循环
语法格式:for(初始化表达式; 循环条件; 操作表达式){循环体},适用于循环次数明确的场景。
关键规则:执行顺序为初始化→判断条件→循环体→操作表达式,依次循环。
编程案例:
java复制代码
public class ForDemo {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 4; i++) { // 1到4求和
sum += i;
}
System.out.println("sum=" + sum); // 输出10
}
}
(四)循环嵌套与跳转语句
循环嵌套:循环体内部嵌套另一个循环,常用for嵌套实现复杂逻辑。
跳转语句:break跳出当前循环,continue终止本次循环进入下一次。
编程案例:
java复制代码
public class LoopNestedDemo {
public static void main(String[] args) {
// 打印直角三角形(循环嵌套)
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
// 跳转语句示例
for (int i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // 跳过偶数
}
if (i > 7) {
break; // 大于7则退出循环
}
System.out.print(i + " "); // 输出1 3 5 7
}
}
}
四、方法
(一)方法的定义与调用
语法格式:修饰符 返回值类型 方法名(参数类型 参数名){执行语句; return 返回值;}。
关键规则:无返回值时返回值类型为void,可省略return;调用时实参与形参类型、个数需一致。
编程案例:
java复制代码
public class MethodDemo {
// 定义求矩形面积的方法
public static int getArea(int width, int height) {
return width * height;
}
public static void main(String[] args) {
int area = getArea(3, 5); // 调用方法
System.out.println("矩形面积:" + area); // 输出15
}
}
(二)方法的重载
定义:同一类中,方法名相同但参数个数或参数类型不同(返回值类型不影响重载)。
关键规则:调用时根据实参自动匹配对应的重载方法。
编程案例:
java复制代码
public class MethodOverloadDemo {
// 两个整数相加
public static int add(int a, int b) {
return a + b;
}
// 三个整数相加(参数个数不同)
public static int add(int a, int b, int c) {
return a + b + c;
}
// 两个小数相加(参数类型不同)
public static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(1, 2)); // 输出3
System.out.println(add(1, 2, 3)); // 输出6
System.out.println(add(1.2, 2.3)); // 输出3.5
}
}
五、数组
(一)数组的声明与初始化
声明方式:数据类型[] 数组名,初始化分为动态初始化(指定长度)和静态初始化(直接赋值)。
关键规则:数组索引从0开始,最大索引为数组长度-1,可通过数组名.length获取长度。
编程案例:
java复制代码
public class ArrayInitDemo {
public static void main(String[] args) {
// 动态初始化
int[] arr1 = new int[3];
arr1[0] = 1;
arr1[1] = 2;
// 静态初始化
int[] arr2 = {3, 4, 5};
int[] arr3 = new int[]{6, 7, 8};
System.out.println("arr1[0]=" + arr1[0]); // 输出1
System.out.println("arr2长度:" + arr2.length); // 输出3
}
}
(二)数组的常见操作
核心操作:遍历、求最值、插入元素、冒泡排序。
关键规则:数组长度不可变,插入元素需创建新数组;冒泡排序通过相邻元素比较交换实现升序/降序。
编程案例:
java复制代码
public class ArrayOperationDemo {
public static void main(String[] args) {
int[] arr = {4, 1, 6, 3, 9, 8};
// 遍历数组
System.out.print("遍历结果:");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
// 求最大值
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
System.out.println("\n最大值:" + max); // 输出9
// 冒泡排序(升序)
for (int i = 1; i < arr.length; i++) {
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.print("排序结果:");
for (int num : arr) {
System.out.print(num + " "); // 输出1 3 4 6 8 9
}
}
}
(三)二维数组
声明方式:支持固定行列、固定行数不固定列数、直接赋值三种方式。
关键规则:通过数组名[行索引][列索引]访问元素,适用于表格类数据存储。
编程案例:
java复制代码
public class TwoDimensionalArrayDemo {
public static void main(String[] args) {
// 定义3行2列的二维数组
int[][] arr = {{11, 12}, {21, 22}, {31, 32}};
// 遍历二维数组
int total = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
total += arr[i][j];
System.out.println("arr[" + i + "][" + j + "]=" + arr[i][j]);
}
}
System.out.println("总和:" + total); // 输出128
}
}