- 前端(HTML/CSS/JavaScript)
前端主要负责展示商品、处理用户交互等。
HTML (仅展示商品列表的示例):
html
商品列表
javascript
// 假设这是从后端获取的数据
const products = [
{ id: 1, name: ‘商品1’, price: 100 },
// …更多商品
];
// 动态生成商品列表
const productList = document.getElementById(‘productList’);
products.forEach(product => {
const li = document.createElement(‘li’);
li.textContent = ${product.name} - 价格: ${product.price}
;
productList.appendChild(li);
});
2. 后端(Node.js/Express)
后端主要负责处理前端请求、与数据库交互等。
Node.js + Express (仅展示路由和伪数据处理的示例):
javascript
const express = require(‘express’);
const app = express();
const port = 3000;
// 模拟数据库中的商品数据
let products = [
// …商品数据
];
// 商品列表路由
app.get(‘/products’, (req, res) => {
res.json(products); // 返回商品数据给前端
});
// 启动服务器
app.listen(port, () => {
console.log(Server is running on port ${port}
);
});
3. 数据库(MySQL/MongoDB)
数据库用于存储商品、用户、订单等数据。
MySQL (仅展示创建商品表的SQL语句):
sql
CREATE TABLE products (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
price DECIMAL(10, 2) NOT NULL
– …其他字段
);
MongoDB (仅展示商品集合的示例数据结构):
在MongoDB中,你通常不会定义表结构,但可以通过示例文档来描述数据结构:
json
{
“_id”: ObjectId(“…”), // MongoDB自动生成的唯一ID
“name”: “商品1”,
“price”: 100,
// …其他字段
}
注意:
这里的代码只是非常基础的示例,用于说明每个部分的作用和如何相互协作。
一个完整的购物商城还需要考虑很多其他因素,如用户认证、支付接口、订单处理、库存管理、搜索功能、推荐算法等。
根据你的具体需求和技术栈,你可能会选择使用不同的编程语言、框架和数据库。由于篇幅限制,我将为您提供几种不同编程语言中非常简单的“猜数字”游戏的示例代码。
- Python
python
import random
def guess_number():
number_to_guess = random.randint(1, 100)
guess = None
attempts = 0
while guess != number_to_guess:
guess = int(input('猜一个1到100之间的数字: '))
attempts += 1
if guess < number_to_guess:
print('太小了!')
elif guess > number_to_guess:
print('太大了!')
print(f'恭喜你,你猜对了! 你尝试了{attempts}次。')
guess_number()
2. JavaScript (在浏览器控制台或Node.js中运行)
javascript
function guessNumber() {
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let guess = null;
let attempts = 0;
while (guess !== numberToGuess) {
guess = parseInt(prompt('猜一个1到100之间的数字: '));
attempts++;
if (guess < numberToGuess) {
console.log('太小了!');
} else if (guess > numberToGuess) {
console.log('太大了!');
}
}
console.log(`恭喜你,你猜对了! 你尝试了${attempts}次。`);
}
guessNumber();
3. Java
java
import java.util.Random;
import java.util.Scanner;
public class GuessNumberGame {
public static void main(String[] args) {
Random rand = new Random();
int numberToGuess = rand.nextInt(100) + 1;
int guess = 0;
int attempts = 0;
Scanner scanner = new Scanner(System.in);
while (guess != numberToGuess) {
System.out.print("猜一个1到100之间的数字: ");
guess = scanner.nextInt();
attempts++;
if (guess < numberToGuess) {
System.out.println("太小了!");
} else if (guess > numberToGuess) {
System.out.println("太大了!");
}
}
System.out.println("恭喜你,你猜对了! 你尝试了" + attempts + "次。");
}
}
4. C#
csharp
using System;
class Program {
static void Main() {
Random rand = new Random();
int numberToGuess = rand.Next(1, 101);
int guess = 0;
int attempts = 0;
Console.WriteLine("猜一个1到100之间的数字:");
while (guess != numberToGuess) {
guess = int.Parse(Console.ReadLine());
attempts++;
if (guess < numberToGuess) {
Console.WriteLine("太小了!");
} else if (guess > numberToGuess) {
Console.WriteLine("太大了!");
}
}
Console.WriteLine("恭喜你,你猜对了! 你尝试了" + attempts + "次。");
}
}
这些代码都实现了一个简单的“猜数字”游戏,其中计算机随机选择一个1到100之间的数字,然后用户尝试猜测这个数字。程序会告诉用户他们的猜测是太大、太小还是正确,并记录用户尝试的次数。