二、第二节课内容

由于第一节课是环境和安装,所以不问了

提问

1

上面回答的内容里,每个小点没有例子,所以补充了问题:


2

回答

第2节课:Java程序的基本结构和语法规则

1. 基础语法和结构

  • 程序的基本结构和组成部分:包、导入、类、方法
package com.example; // 包名

import java.util.*; // 导入类库

public class Main { // 类定义
    public static void main(String[] args) { // main方法
        System.out.println("Hello, World!"); // 输出
    }
}
  • 注释和注释的种类:单行注释、多行注释、文档注释
// 单行注释

/*
多行注释
*/

/**
 * 文档注释
 */

  • 命名规范:类、变量、方法、常量的命名规范
public class MyClass { // 类名的命名规范
    private int myVariable; // 变量名的命名规范
    public void myMethod() { // 方法名的命名规范
        final int MY_CONST = 10; // 常量名的命名规范
    }
}

2.变量和数据类型

  • 变量的定义和命名规范
int age; // 定义整数型变量
double salary; // 定义双精度型变量
String name; // 定义字符串型变量
boolean isMarried; // 定义布尔型变量
  • 数据类型:基本数据类型和引用数据类型
// 基本数据类型
byte myByte = 100;
short myShort = 10000;
int myInt = 1000000;
long myLong = 100000000L;
float myFloat = 1.23f;
double myDouble = 1.23;
char myChar = 'A';
boolean myBoolean = true;

// 引用数据类型
String myString = "Hello, World!";
int[] myArray = {1, 2, 3};
List<String> myList = new ArrayList<String>();

3. 方法定义

  • 方法的定义和语法规则
public static int add(int a, int b) { // 方法定义
    return a + b;
}
  • 方法的参数和返回值
public static int add(int a, int b) { // 有两个参数
    return a + b; // 返回a和b的和
}
  • 方法的重载和调用
public static int add(int a, int b) { // 第一个add方法
    return a + b;
}

public static double add(double a, double b) { // 第二个add方法
    return a + b;
}

// 调用方法
int sum1 = add(1, 2); // 调用第一个add方法
double sum2 = add(1.5, 2.5); //

4. 运算符和表达式

  • 基本运算符:算术运算符、赋值运算符、关系运算符、逻辑运算符、位运算符
int a = 10;
int b = 3;
int sum = a + b; // 算术运算符
a += 5; // 赋值运算符
boolean isEqual = a == b; // 关系运算符
boolean isTrue = a > b && a < 20; // 逻辑运算符
int c = 0b1010 & 0b1100; // 位运算符
  • 表达式和语句:表达式的值和语句的作用
int a = 10;
int b = 3;
int sum = a + b; // 表达式
System.out.println(sum); // 语句

5. 控制流语句

  • 条件语句:if语句、switch语句
int score = 85;

if (score >= 90) {
    System.out.println("A");
} else if (score >= 80) {
    System.out.println("B");
} else {
    System.out.println("C");
}

int num = 2;

switch (num) {
    case 1:
        System.out.println("一");
        break;
    case 2:
        System.out.println("二");
        break;
    default:
        System.out.println("其他");
}
  • 循环语句:for语句、while语句、do-while语句
int sum = 0;

for (int i = 1; i <= 100; i++) {
    sum += i;
}

int i = 1;
int sum = 0;

while (i <= 100) {
    sum += i;
    i++;
}

int i = 1;
int sum = 0;

do {
    sum += i;
    i++;
} while (i <= 100);

6. 练习题

  • 编写一个Java程序,输出1-100之间的偶数
for (int i = 1; i <= 100; i++) {
    if (i % 2 == 0) {
        System.out.println(i);
    }
}
  • 编写一个Java程序,判断一个数是不是素数
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();

        boolean isPrime = true;

        for (int i = 2; i < num; i++) {
            if (num % i == 0) {
                isPrime = false;
                break;
            }
        }

        if (isPrime) {
            System.out.println(num + "是素数");
        } else {
            System.out.println(num + "不是素数");
        }
    }
}

  • 编写一个Java程序,计算n的阶乘
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        int factorial = 1;

        for (int i = 1; i <= n; i++) {
            factorial *= i

GPT部分答案截图

1
2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。