【实操2】代码生成
五大AI编程工具(Copilot、Tabnine、Codeium)综合评估分析
https://www.zengqueling.com/wudaaibianchenggongjucopilottabninecodeiumzonghepinggufenxij/
【案例1】彩票生成prompt:
写一个彩票生成的完整程序代码,可以选择双色球或大乐透(输入1或2选择),然后可以输入注数,用Python。
import random
def generate_lottery(choice, num_of_bets):
if choice == 1:
red_balls = list(range(1, 34))
blue_balls = list(range(1, 17))
elif choice == 2:
red_balls = list(range(1, 36))
blue_balls = list(range(1, 13))
else:
return "请输入正确的彩票类型(1表示双色球,2表示大乐透)"
selected_red_balls = random.sample(red_balls, num_of_bets)
selected_blue_balls = random.sample(blue_balls, num_of_bets)
result = []
for i in range(num_of_bets):
result.append((selected_red_balls[i], selected_blue_balls[i]))
return result
choice = int(input("请输入彩票类型(1表示双色球,2表示大乐透):"))
num_of_bets = int(input("请输入注数:"))
result = generate_lottery(choice, num_of_bets)
print("生成的彩票号码为:", result)
改成面向对象的方法。
改成Java
改为可视化界面的java程序
以下是使用Java Swing库实现的可视化界面代码示例:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Lottery {
public static void main(String[] args) {
JFrame frame = new JFrame("双色球彩票");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 300);
JPanel panel = new JPanel();
frame.add(panel);
placeComponents(panel);
frame.setVisible(true);
}
private static void placeComponents(JPanel panel) {
panel.setLayout(null);
JLabel redBallLabel = new JLabel("红球:");
redBallLabel.setBounds(10, 20, 80, 25);
panel.add(redBallLabel);
JTextField redBallText = new JTextField(7); // 输入框,用于输入选中的红球数
redBallText.setBounds(100, 20, 165, 25);
panel.add(redBallText);
JLabel blueBallLabel = new JLabel("蓝球:");
blueBallLabel.setBounds(10, 50, 80, 25);
panel.add(blueBallLabel);
JTextField blueBallText = new JTextField(7); // 输入框,用于输入选中的蓝球数
blueBallText.setBounds(100, 50, 165, 25);
panel.add(blueBallText);
JButton generateButton = new JButton("生成彩票");
generateButton.setBounds(10, 80, 80, 25);
panel.add(generateButton);
JButton calculateButton = new JButton("计算奖金");
calculateButton.setBounds(180, 80, 80, 25);
panel.add(calculateButton);
generateButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String redBallStr = redBallText.getText(); // 获取选中的红球数字符串表示形式
int selectedRedBalls = Integer.parseInt(redBallStr); // 将字符串转换为整数类型,表示选中的红球数
int totalBalls = selectedRedBalls + selectedBlueBalls; // 总球数等于选中的红球数加上选中的蓝球数
int selectedBlueBalls = Math.min(totalBalls - selectedRedBalls, Math.pow(2, totalBalls)); // 根据规则计算选中的蓝球数
int bonusRate = 100000; /
【案例2】抢茅台酒
写一个京东抢茅台酒的Python程序。先到京东主页,然后搜索茅台酒,找到1499元的商品,然后购买。
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 请替换为你自己的京东账号和密码
username = "your_username"
password = "your_password"
# 请替换为你的电脑上的chromedriver路径
chromedriver_path = "your_chromedriver_path"
# 登录京东
def login():
driver.get("[https://www.jd.com](https://www.jd.com/)")
time.sleep(3)
driver.find_element_by_link_text("你好,请登录").click()
time.sleep(3)
driver.find_element_by_id("loginname").send_keys(username)
driver.find_element_by_id("nloginpwd").send_keys(password)
driver.find_element_by_id("loginsubmit").click()
time.sleep(5)
# 点击搜索茅台酒
def search_moutai():
driver.find_element_by_id("key").send_keys("茅台酒")
driver.find_element_by_id("key").send_keys(Keys.ENTER)
time.sleep(2)
# 点击购买1499元的商品
def buy_moutai():
moutai_item = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//div[**@class=**'gl-item']/div[**@class=**'p-name p-price js-p-price']")))
moutai_price = moutai_item.text.split()[-1]
if float(moutai_price) == 1499:
moutai_item.click()
time.sleep(3)
buy_button = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//span[**@class=**'go-btn go-cart-btn']")))
buy_button.click()
time.sleep(5)
print("购买成功!")
driver.quit()
return True
else:
print("未找到价格为1499元的商品,请检查商品信息。")
return False
if __name__ == "__main__":
driver = webdriver.Chrome(chromedriver_path)
login()
search_moutai()
buy_moutai()
原创文章,作者:曾确令,如若转载,请注明出处:https://www.zengqueling.com/7shicao2daimashengch/