Java编程基础教程

Java编程基础教程

经验文章nimo972025-03-31 16:12:559A+A-

1. 变量与数据类型

1.1 变量声明

public class VariablesDemo {
    public static void main(String[] args) {
        // 声明变量三部曲:类型 + 名称 + 值
        int age = 25;                   // 整数类型
        double price = 9.99;            // 双精度浮点数
        char grade = 'A';               // 单个字符
        boolean isJavaFun = true;       // 布尔值
        String message = "Hello Java";  // 字符串
        
        System.out.println("年龄:" + age);
        System.out.println("价格:" + price);
        System.out.println("评分:" + grade);
        System.out.println("Java有趣吗?" + isJavaFun);
        System.out.println(message);
    }
}

关键点

  • int 存储整数(如 10、-5)
  • double 存储小数(如 3.14、-0.5)
  • char 用单引号包裹单个字符
  • boolean 只有 true 或 false
  • String 用双引号包裹文本(非基本类型)

1.2 数据类型转换

public class TypeConversion {
    public static void main(String[] args) {
        // 自动类型转换(小类型→大类型)
        int num = 100;
        double bigNum = num;  // int → double
        System.out.println(bigNum);  // 输出 100.0

        // 强制类型转换(可能丢失精度)
        double pi = 3.1415;
        int intPi = (int) pi;        // double → int
        System.out.println(intPi);   // 输出 3
    }
}

2. 运算符

2.1 算术运算符

public class ArithmeticDemo {
    public static void main(String[] args) {
        int a = 10;
        int b = 3;
        
        System.out.println(a + b);  // 13
        System.out.println(a - b);  // 7
        System.out.println(a * b);  // 30
        System.out.println(a / b);  // 3 (整数除法)
        System.out.println(a % b);  // 1 (取余)
        
        // 自增运算
        int count = 5;
        count++;                    // 等同于 count = count + 1
        System.out.println(count);  // 6
    }
}

2.2 比较运算符

public class ComparisonDemo {
    public static void main(String[] args) {
        int x = 5;
        int y = 7;
        
        System.out.println(x == y);  // false
        System.out.println(x != y);  // true
        System.out.println(x > y);   // false
        System.out.println(x <= y);  // true
    }
}

2.3 逻辑运算符

public class LogicDemo {
    public static void main(String[] args) {
        boolean hasMoney = true;
        boolean isSunny = false;
        
        // && 逻辑与(两个条件都要满足)
        System.out.println(hasMoney && isSunny);  // false
        
        // || 逻辑或(满足一个即可)
        System.out.println(hasMoney || isSunny);  // true
        
        // ! 逻辑非(取反)
        System.out.println(!isSunny);             // true
    }
}

3. 流程控制

3.1 if-else 语句

public class IfDemo {
    public static void main(String[] args) {
        int score = 85;
        
        if (score >= 90) {
            System.out.println("优秀");
        } else if (score >= 80) {
            System.out.println("良好");  // 这里会执行
        } else {
            System.out.println("加油");
        }
    }
}

3.2 for 循环

public class ForDemo {
    public static void main(String[] args) {
        // 打印1-5的数字
        for (int i = 1; i <= 5; i++) {
            System.out.println("当前值:" + i);
        }
        
        // 计算1-100的和
        int sum = 0;
        for (int j = 1; j <= 100; j++) {
            sum += j;
        }
        System.out.println("总和:" + sum);  // 5050
    }
}

3.3 while 循环

public class WhileDemo {
    public static void main(String[] args) {
        // 基本while循环
        int count = 3;
        while (count > 0) {
            System.out.println("倒计时:" + count);
            count--;
        }

        // do-while循环(至少执行一次)
        int num = 10;
        do {
            System.out.println("数值:" + num);
            num++;
        } while (num < 5);  // 条件不成立时退出
    }
}

4. 综合练习

计算成绩等级 + 找质数

public class Practice {
    public static void main(String[] args) {
        // 练习1:根据分数输出等级
        int score = 78;
        if (score >= 90) {
            System.out.println("A");
        } else if (score >= 75) {
            System.out.println("B");  // 执行这里
        } else {
            System.out.println("C");
        }

        // 练习2:找出1-20之间的质数
        System.out.print("质数:");
        for (int i = 2; i <= 20; i++) {
            boolean isPrime = true;
            for (int j = 2; j <= i/2; j++) {
                if (i % j == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                System.out.print(i + " ");  // 输出 2 3 5 7 11 13 17 19
            }
        }
    }
}

学习建议

  1. 实操步骤:
  2. 在IDE中逐行手敲代码(不要复制粘贴)
  3. 修改数值观察不同输出结果(例如把score改成95)
  4. 尝试用while改写for循环的案例
  5. 常见错误规避:
  6. 变量未初始化就使用 → int x; System.out.println(x);
  7. 错误使用=代替== → if (x = 5)
  8. 忘记循环终止条件 → 导致无限循环
  9. 扩展练习:
  • 用三种循环分别实现阶乘计算(5! = 120)
  • 编写温度转换程序(摄氏度→华氏度)
  • 制作一个简单的计算器(支持加减乘除)

掌握这些基础后,可以进入下一阶段的面向对象编程学习。

点击这里复制本文地址 以上内容由nimo97整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
qrcode

尼墨宝库 © All Rights Reserved.  蜀ICP备2024111239号-7