我正在尝试在 for 循环内使用 if-else 语句。这是我的代码,内部 for 循环和 if-else 语句存在一些问题。代码如下
\begin{algorithm}
\caption{Euclid’s algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\State U: a set of authentic users
\State T: a set containing trustor and trustee information
\Procedure{DepthDetection}{$T_t_h, U_i$}
% \State $r\gets a\bmod b$
\State $T_u_i \gets list\ of\ trustee\ taking\ U_i\ as\ trustor$
\State $I_u_i \gets list\ of\ items\ rated\ by\ U_i$
\State Build ${U_i}'s\ trust\ network\ using\ $T_t_h$
\For{each item $i$ in $I_u_i$ }
\State $NU_u_i \gets list\ of\ users\ who\ rated\ I_i$
\For{each user $u$ in $NU_u_i$ }
\IF{$u$ is in $T_u_i$}
\STATE use the rating and trust of $u$ for prediction of rating
\ENDIF
\EndFor
\EndFor
\State \textbf{return} $L$
\EndProcedure
\end{algorithmic}\end{algorithm}
如您所见,内循环之后还有 if 条件和语句的延续。我希望采用嵌套格式。
答案1
你正在混合algorithmic
语法algpseudocode
。
\documentclass{article}
\usepackage{algorithm,algpseudocode}
\begin{document}
\begin{algorithm}
\caption{Euclid’s algorithm}
\label{alg:euclid}
\begin{algorithmic}[1]
\State $U$: a set of authentic users
\State $T$: a set containing trustor and trustee information
\Procedure{DepthDetection}{$T_{t_h}$, $U_i$}
\State $T_{u_i} \gets$ list of trustee taking $U_i$ as trustor
\State $I_{u_i} \gets$ list of items rated by $U_i$
\State Build $U_i$'s trust network using $T_{t_h}$
\For{each item $i$ in $I_{u_i}$}
\State $NU_{u_i} \gets$ list of users who rated $I_i$
\For{each user $u$ in $NU_{u_i}$ }
\If{$u$ is in $T_{u_i}$}
\State use the rating and trust of $u$ for prediction of rating
\EndIf
\EndFor
\EndFor
\State \textbf{return} $L$
\EndProcedure
\end{algorithmic}
\end{algorithm}
\end{document}
我还修复了数学模式和双下标的误用。