初学编程100个代码大全

从零开始,系统学习编程基础知识,涵盖主流编程语言100+实用示例

🐍
Python

25个示例

🌐
JavaScript

25个示例

Java

25个示例

⚙️
C++

25个示例

0

代码示例

0

编程语言

0

基础概念

0

代码行数

Python编程示例

示例1:Hello World

最简单的Python程序

# Python Hello World示例
print("Hello, World!")

# 输出:Hello, World!

# 多行字符串示例
message = """
这是一个多行字符串
可以包含多行文本
"""
print(message)

示例2:变量和数据类型

Python基础数据类型演示

# 变量赋值
name = "张三"
age = 25
height = 1.75
is_student = True

# 数据类型检查
print(type(name))    # 
print(type(age))     # 
print(type(height))  # 
print(type(is_student))  # 

# 类型转换
age_str = str(age)
print(age_str + "岁")

示例3:列表操作

Python列表的常用操作

# 创建列表
fruits = ["苹果", "香蕉", "橙子", "葡萄"]

# 访问元素
print(fruits[0])  # 苹果
print(fruits[-1]) # 葡萄

# 切片操作
print(fruits[1:3]) # ['香蕉', '橙子']

# 添加元素
fruits.append("西瓜")
fruits.insert(1, "梨")

# 删除元素
fruits.remove("香蕉")
popped = fruits.pop()

# 列表推导式
squares = [x**2 for x in range(10)]
print(squares)  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

JavaScript编程示例

示例1:基础语法

JavaScript基础语法和变量

// JavaScript变量声明
let name = "张三";
const age = 25;
var city = "北京";

// 数据类型
console.log(typeof name);    // string
console.log(typeof age);     // number
console.log(typeof undefinedVar); // undefined

// 模板字符串
const message = `你好,我是${name},今年${age}岁,来自${city}`;
console.log(message);

// 箭头函数
const add = (a, b) => a + b;
console.log(add(5, 3)); // 8

示例2:数组操作

JavaScript数组的常用方法

// 创建数组
const numbers = [1, 2, 3, 4, 5];

// 数组方法
const doubled = numbers.map(n => n * 2);
console.log(doubled); // [2, 4, 6, 8, 10]

const filtered = numbers.filter(n => n > 3);
console.log(filtered); // [4, 5]

const sum = numbers.reduce((acc, n) => acc + n, 0);
console.log(sum); // 15

// 解构赋值
const [first, second, ...rest] = numbers;
console.log(first, second, rest); // 1 2 [3, 4, 5]

示例3:DOM操作

JavaScript操作HTML元素

// 获取元素
const button = document.getElementById('myButton');
const paragraphs = document.querySelectorAll('p');

// 修改元素内容
button.textContent = '点击我';
paragraphs.forEach(p => {
    p.addEventListener('click', function() {
        this.style.color = 'blue';
    });
});

// 创建新元素
const newDiv = document.createElement('div');
newDiv.innerHTML = '

新创建的元素

'; document.body.appendChild(newDiv); // AJAX请求示例 fetch('/api/data') .then(response => response.json()) .then(data => console.log(data)) .catch(error => console.error('Error:', error));

Java编程示例

示例1:Hello World

Java入门程序

// HelloWorld.java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        
        // 基本数据类型
        int age = 25;
        double height = 1.75;
        boolean isStudent = true;
        char grade = 'A';
        
        // 格式化输出
        System.out.printf("年龄:%d,身高:%.2f米%n", age, height);
    }
}

示例2:类和对象

Java面向对象基础

// Student.java
public class Student {
    private String name;
    private int age;
    
    // 构造方法
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    
    // Getter和Setter方法
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    // 实例方法
    public void displayInfo() {
        System.out.println("姓名:" + name + ",年龄:" + age);
    }
    
    // 静态方法
    public static int calculateAge(int birthYear) {
        return java.time.Year.now().getValue() - birthYear;
    }
}

示例3: *** 框架

Java *** 的使用

import java.util.*;

public class CollectionExample {
    public static void main(String[] args) {
        // ArrayList使用
        List fruits = new ArrayList<>();
        fruits.add("苹果");
        fruits.add("香蕉");
        fruits.add("橙子");
        
        // 遍历列表
        for (String fruit : fruits) {
            System.out.println(fruit);
        }
        
        // HashMap使用
        Map scores = new HashMap<>();
        scores.put("数学", 90);
        scores.put("英语", 85);
        scores.put("语文", 88);
        
        // 遍历Map
        for (Map.Entry entry : scores.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        
        // HashSet使用
        Set uniqueNumbers = new HashSet<>();
        uniqueNumbers.add(1);
        uniqueNumbers.add(2);
        uniqueNumbers.add(1); // 重复元素不会被添加
        
        System.out.println("唯一数字:" + uniqueNumbers);
    }
}

C++编程示例

示例1:基础程序

C++入门示例

#include 
using namespace std;

int main() {
    // 输出Hello World
    cout << "Hello, World!" << endl;
    
    // 基本数据类型
    int age = 25;
    float height = 1.75f;
    char grade = 'A';
    string name = "张三";
    
    // 输入输出
    cout << "请输入您的年龄:";
    cin >> age;
    cout << "您的年龄是:" << age << "岁" << endl;
    
    return 0;
}

示例2:函数和指针

C++函数和指针的基本用法

#include 
using namespace std;

// 函数声明
int add(int a, int b);
void swap(int *a, int *b);
void printArray(int arr[], int size);

int main() {
    int x = 10, y = 20;
    
    // 调用函数
    cout << "和:" << add(x, y) << endl;
    
    // 指针操作
    swap(&x, &y);
    cout << "交换后:x=" << x << ", y=" << y << endl;
    
    // 数组和指针
    int arr[5] = {1, 2, 3, 4, 5};
    int *ptr = arr;
    
    cout << "数组元素:";
    for (int i = 0; i < 5; i++) {
        cout << *(ptr + i) << " ";
    }
    cout << endl;
    
    return 0;
}

// 函数定义
int add(int a, int b) {
    return a + b;
}

// 使用指针交换两个数
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

示例3:类和对象

C++面向对象编程

#include 
#include 
using namespace std;

class Rectangle {
private:
    double width;
    double height;
    
public:
    // 构造函数
    Rectangle(double w, double h) {
        width = w;
        height = h;
    }
    
    // 成员函数
    double getArea() {
        return width * height;
    }
    
    double getPerimeter() {
        return 2 * (width + height);
    }
    
    // Getter和Setter
    double getWidth() { return width; }
    double getHeight() { return height; }
    void setWidth(double w) { width = w; }
    void setHeight(double h) { height = h; }
};

int main() {
    // 创建对象
    Rectangle rect(5.0, 3.0);
    
    cout << "宽度:" << rect.getWidth() << endl;
    cout << "高度:" << rect.getHeight() << endl;
    cout << "面积:" << rect.getArea() << endl;
    cout << "周长:" << rect.getPerimeter() << endl;
    
    return 0;
}

Web开发示例

示例1:HTML5基础结构

语义化HTML5结构




    
    
    我的网站
    


    

欢迎访问

这是主要内容区域

© 2024 我的网站

专业SEO优化服务 | 广州搜索引擎营销专家 - seo.19784.com注:本页面为AI生成。如发现本站(文章、内容、图片、音频、视频)有涉嫌抄袭侵权/违法违规的内容,请发送邮件至31 555555 35@qq.com举报,一经查实,本站将立刻删除、维护您的正当权益。
发布时间: