为了在文章中以高亮形式展示代码,你可以使用 [pre ][/pre](实际使用时取消空格) 或 [code ][/code](实际使用时取消空格) 标签包裹代码。在旧版本中,这种方式可能会导致注释和实际代码的亮度混乱,让阅读者感到困扰。现在,注释内容将显示得更为暗淡,以区分于其他代码。
现已优化显示效果
高亮中的关键词、注释、字符串 一目了然
LUA:
- local i = 1
- while i <= 5 do
- print("Iteration:", i)
- i = i + 1
- end -- 输出 Iteration: 1 到 Iteration: 5
复制代码
C++
- #include <iostream>
- int main() {
- int a, b;
- std::cout << "Enter two integers: ";
- std::cin >> a >> b;
- int sum = a + b;
- std::cout << "The sum of " << a << " and " << b << " is " << sum << std::endl;
- return 0;
- }
复制代码
Python
- def multiply_two_numbers(a, b):
- return a * b
- # 获取用户输入
- num1 = float(input("Enter the first number: "))
- num2 = float(input("Enter the second number: "))
- # 计算乘积
- product = multiply_two_numbers(num1, num2)
- # 输出结果
- print(f"The product of {num1} and {num2} is {product}")
复制代码
XML:
- <?xml version="1.0" encoding="UTF-8"?>
- <library>
- <book id="1">
- <title>The Great Gatsby</title>
- <author>F. Scott Fitzgerald</author>
- <year>1925</year>
- </book>
- <book id="2">
- <title>To Kill a Mockingbird</title>
- <author>Harper Lee</author>
- <year>1960</year>
- </book>
- <book id="3">
- <title>1984</title>
- <author>George Orwell</author>
- <year>1949</year>
- </book>
- </library>
复制代码
Java
- public class HelloWorld {
- public static void main(String[] args) {
- System.out.println("Hello, World!");
- }
- }
复制代码
JavaScript
- // 函数定义,用于计算两个数的和并在控制台输出结果
- function addNumbers(a, b) {
- let sum = a + b;
- console.log("Sum: " + sum);
- return sum;
- }
- // 事件监听器,当点击 HTML 页面上的按钮时,执行 addNumbers 函数
- document.getElementById("sumButton").addEventListener("click", function() {
- let num1 = parseInt(document.getElementById("number1").value);
- let num2 = parseInt(document.getElementById("number2").value);
- addNumbers(num1, num2);
- });
复制代码
顺便记录如何使用highlight.js定制代码高亮:
|