console
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>试卷生成器</title>
<style>
@media print {
#printButton {
display: none;
}
}
body {
margin: 40px;
padding: 20px;
font-family: "SimSun", serif;
font-size: 16px;
line-height: 1.5;
width: 80%;
max-width: 800px;
margin: auto;
background: #fff;
}
.header {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.score {
text-align: right;
font-size: 18px;
margin-bottom: 20px;
}
ol {
padding-left: 20px;
}
li {
margin-bottom: 15px;
}
</style>
<script>
function addQuestion() {
let question = prompt("请输入题目:");
if (!question) return;
let options = [];
for (let i = 0; i < 4; i++) {
let option = prompt(`请输入选项 ${String.fromCharCode(65 + i)}:`);
options.push(option ? option : "");
}
let storedQuestions = JSON.parse(localStorage.getItem("questions")) || [];
storedQuestions.push({ question, options });
localStorage.setItem("questions", JSON.stringify(storedQuestions));
alert("题目已添加!");
}
function generateExam() {
let storedQuestions = JSON.parse(localStorage.getItem("questions")) || [];
let numQuestions = parseInt(prompt("请输入试卷题目数量:"));
let currentScore = parseInt(prompt("请输入分数:"));
if (isNaN(numQuestions) || isNaN(currentScore)) {
alert("请输入正确的数值!");
return;
}
if (storedQuestions.length < numQuestions) {
alert(`题目库不足 ${numQuestions} 道,请先添加更多题目!`);
return;
}
let shuffledQuestions = storedQuestions.sort(() => 0.5 - Math.random());
let selectedQuestions = shuffledQuestions.slice(0, numQuestions);
let examContent = `
<html>
<head>
<title>试卷</title>
<style>
@media print {
#printButton {
display: none;
}
}
body {
margin: 40px;
padding: 20px;
font-family: "SimSun", serif;
font-size: 16px;
line-height: 1.5;
width: 80%;
max-width: 800px;
margin: auto;
background: #fff;
}
.header {
text-align: center;
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.score {
text-align: right;
font-size: 18px;
margin-bottom: 20px;
}
ol {
padding-left: 20px;
}
li {
margin-bottom: 15px;
}
</style>
</head>
<body>
<div class="header">试卷</div>
<div class="score">总分:${currentScore}</div>
<hr>
<ol>
`;
selectedQuestions.forEach((q) => {
examContent +=`<li>${q.question??''}<br>A. ${q.options[0]??''}<br>B. ${q.options[1]??''}<br>C. ${q.options[2]??''}<br>D. ${q.options[3]??''}</li>`
});
examContent += `
</ol>
<button id="printButton" onclick="window.print()">打印试卷</button>
</body>
</html>
`;
let examWindow = window.open("", "_blank");
examWindow.document.write(examContent);
examWindow.document.close();
}
</script>
</head>
<body>
<h1>试卷生成器</h1>
<button onclick="addQuestion()">输入题目</button>
<button onclick="generateExam()">生成试卷</button>
</body>
</html>