【发布时间】:2021-09-03 08:27:58
【问题描述】:
我正在尝试创建一个画布测验,要求学生评估不同电路的有效电阻。该电路包含 2 到 4 个电阻,电阻值选自 E12 系列。为了回答这个问题,学生们展示了他们理解电阻器之间的并联和串联连接。
对于任何单个电路配置,我都可以提出问题(这里是两个串联电阻的示例)
\begin{question}
What is the total resistance between points a and b if $R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$ and $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$?
\setkeys{Gin}{width=0.3\textwidth}
<<fig=TRUE, height = 4, width = 4, echo=FALSE, eps=FALSE, results=hide>>=
plot.new()
rasterImage(im, 0, 0, 1, xmax)
@
\end{question}
\begin{solution}
Resistors in series should be added. So
\begin{eqnarray*}
R_{tot} & = & R_1 + R_2\\
R_{tot} & = & \Sexpr{r1_val} + \Sexpr{r2_val}\\
R_{tot} & = & \Sexpr{res}~\Omega
\end{eqnarray*}
\end{solution}
但我不知道如何随机选择问题类型。例如,如果脚本在两个串联电阻或三个串联电阻之间随机选择,则以下不起作用
\begin{question}
What is the total resistance between points a and b given the following:
<<>>=
if (sel==1)
{
@
$R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$, and $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$, arranged as shown in the schematic
<<>>=
} else if (sel == 2)
{
@
$R_1 = \Sexpr{r1}$ \Sexpr{char1_insert}$\Omega$, $R_2 = \Sexpr{r2}$ \Sexpr{char2_insert}$\Omega$, and $R_3 = \Sexpr{r3}$ \Sexpr{char3_insert}$\Omega$ arranged as shown in the schematic
<<fig=TRUE, height = 4, width = 4, echo=FALSE, eps=FALSE, results=hide>>=
plot.new()
rasterImage(im, 0, 0, 1, xmax)
@
\end{question}
#similar code for solution
我之所以会这样,是因为 if 语句被打断并且解释器对此感到窒息(我猜)。但是,有没有办法做到这一点?
【问题讨论】: