不结束的循环语句

不结束的循环语句

usepackage{algpseudocode}在 latex 中使用伪代码,但我发现循环语句后只有一个end语句,而不是end for。我的算法出了什么问题。语句的其余部分都很好。谢谢

\usepackage{textcomp}
\usepackage{amsmath,amssymb,amsfonts}
%\usepackage{algorithmic}
\usepackage[ruled,vlined, linesnumbered]{algorithm2e}
\usepackage{algpseudocode} 

    \begin{algorithm}[!t]
        \SetAlgoLined
        %\KwResult{SF and TP}
        \SetKwInOut{Input}{Input}\SetKwInOut{Output}{Output} 
        \Input{Scanned beacon signal {($B_{scan})$} }
        \Output{$LA_{BLE}$}
        
        // $Initialization$:  \\
        Initialize RSS memory size $(RSS_{queue}$)\\
        Initialize $B_{i}$ $\rightarrow$  $B_{N}$ \\
        // $Landmark$ $detection$:  \\
        \If{($B_{scan})$}{
            \If { user ($U_{i}$) passes ($PB_i$)}{      
                \Comment Store RSS as ascending order\\
                $RSS_{max}=Max(RSS_{queue}$) \\
    %           \Comment Choose $B_{i}$ with the highest measured  RSS signal strength\\
                    \If{$RSS_{max}$$\geq$$LA_{th}$}{
                        \Comment Compare with threshold value \\
                        \Comment Equation (1) \\
                        $LA_{i}=argmax(RSS_{queue})$ \\
                        \Comment Select landmark $i$ \\
                        $X_{B_{N}}$, $Y_{B_{N}}$ = $LA_{BLE}$ \\
                    }
                
                { %else of \gamma
                    
                    \For{$B_{i}$ $\rightarrow$  $B_{N}$} {%$\ge$
                        Repeat (6) until all landmark found \\
                    }
                    
                }
                
            }
    
            
        }
    
        
        \caption{detection algorithm.}
        \label{algo:algo1} 
    \end{algorithm}

在此处输入图片描述

答案1

您正在使用algorithm2e而不是algpseudocode

如果想要“endfor”和“endif”,则需要自定义。

\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage[ruled,vlined,linesnumbered,commentsnumbered]{algorithm2e}

\SetCommentSty{textit}
\SetKwFor{For}{for}{do}{endfor}
\SetKwIF{If}{ElseIf}{Else}{if}{then}{else if}{else}{endif}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output} 

\begin{document}

    \begin{algorithm}[!tp]
        \SetAlgoLined
        \KwResult{SF and TP}
        \Input{Scanned beacon signal {($B_{\mathrm{scan}})$} }
        \Output{$LA_{\mathrm{BLE}}$}
        \tcp{Initialization:}
        Initialize RSS memory size $(RSS_{queue}$)\\
        Initialize $B_{i}\rightarrow B_{N}$ \\
        \tcp{Landmark detection:}
        \If{$(B_{\mathrm{scan}})$}{
            \If {user $(U_{i})$ passes $(PB_i)$}{      
                \tcp{Store RSS as ascending order}
                $RSS_{\max}=\max(RSS_{\mathrm{queue}}$) \\
                \tcp{Choose $B_{i}$ with the highest measured  RSS signal strength}
                    \If{$RSS_{\max}\geq LA_{th}$}{
                        \tcp{Compare with threshold value}
                        \tcp{Equation (1)}
                        $LA_{i}=\operatorname{argmax}(RSS_{\mathrm{queue}})$ \\
                        \tcp{Select landmark $i$}
                        $X_{B_{N}}, Y_{B_{N}} = LA_{\mathrm{BLE}}$ \\
                    }
                { %else of \gamma
                    \For{$B_{i}\rightarrow B_{N}$} {%$\ge$
                        Repeat (6) until all landmark found \\
                    }
                }
            }
        }
        \caption{detection algorithm.}
        \label{algo:algo1} 
    \end{algorithm}

\end{document}

请检查我为改进排版而做的所有更改。

在此处输入图片描述

相关内容