二、第二節課內容

由于第一節課是環境和安裝,所以不問了

提問

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
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。