写下算法

写下算法

我试图写下我的算法,并假装我写了 Foreach 循环。否则没有命令\Foreach。以下我的程序无法执行。我还使用了包\usepackage{algpseudocode}\usepackage{algorithm}

\begin{algorithm}
  \caption{Algorithm for generating atn.json file}
  \begin{algorithmic}
    \Statex \Comment { \textbf{Input:} ListofOrderedSelectedJobs, testDuration, beginDate, endDate, MapDeviceTranmissionTime, \textbf{Output:} atn.json
   \For{\textbf{each} selectJob \textit{in} ListofOrderedSelectedJobs \textit{do}} 
        \If {selectJob \textit{contains} Adevice \textit{then} \textit{addAll} Adevices}
    \Statex\Comment{ \%comment: Do the same process for group B, C and D devices\%}
        \EndIf 
        \If {$selectJob == 'U'$ \textit{then}}
        \For{\textbf{each} device \in choosenDevice \textit{do}}
    \State  MapDeviceTranmissionTime.put(device,beginDate)  
    \Statex\Comment{ \%comment: Put begin test date for each device and store that value in the map\%} 
    \If {$selectJobType == 'Z'$ \textit{then}}
        \If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
        \Else { addRandomExecutionDate()}
        \EndIf
        \EndIf
    \EndFor 
        \If { $selectJobInterval \leq testDuration$ \textit{then}}
        \State $times = testDuration / selectJobInterval$
        \Else
        \State $times = (testDuration / selectJobInterval) + 1$
        \EndIf
    \For{$i = 1$ to times \textit{do}}
        \If {$selectJob == 'U'$ \textit{then}}
        \For{\textbf{each} device \in choosenDevice \textit{do}}
        \State timesDate = timesDate + Interval 
        \State MapDeviceTranmissionTime.put(device,timesDate) 
        \Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
    \If {$selectJobType == 'Z'$ \textit{then}}
        \If { randomTransmissionDate \textit{exists} \textit{then} readRandomExecutionDate() }
        \Else { addRandomExecutionDate()}
        \EndIf
    \EndFor
        \EndIf
        \EndFor
        \EndFor
  \end{algorithmic}
\end{algorithm} 

有人可以帮我解决我做错的事吗?

答案1

您的问题是缺少一些结束\EndIf\EndFor。我对您的代码做了一些更改,并正确缩进,我可以编译它。此外,您没有正确使用软件包提供的宏。

\documentclass{article}

\usepackage{algpseudocode}
\usepackage{algorithm}

\begin{document}
\begin{algorithm}
  \caption{Algorithm for generating atn.json file}
  \begin{algorithmic}
    \Statex{\Comment{\textbf{Input:} ListofOrderedSelectedJobs, testDuration,
      beginDate, endDate, MapDeviceTranmissionTime; \textbf{Output:}
      atn.json}}
    \Statex
    \ForAll{selectJob $\in$ ListofOrderedSelectedJobs}
      \If{selectJob \textit{contains} Adevice}
        \textit{addAll} Adevices
        \Statex\Comment{\%comment: Do the same process for group B, C and D devices\%}
      \EndIf
      \If{$selectJob == 'U'$}
        \ForAll{device $\in$ choosenDevice}
          \State MapDeviceTranmissionTime.put(device,beginDate)
          \Statex\Comment{\%comment: Put begin test date for each device and store that value in the map\%}
          \If{$selectJobType == 'Z'$}
            \If{$\exists$ randomTransmissionDate}
              \State readRandomExecutionDate()
            \Else
              \State addRandomExecutionDate()
            \EndIf
          \EndIf
        \EndFor
      \EndIf
      \If{ $selectJobInterval \leq testDuration$}
        \State $times = testDuration / selectJobInterval$
      \Else
        \State $times = (testDuration / selectJobInterval) + 1$
      \EndIf
      \For{$i = 1 \to times$}
        \If {$selectJob == 'U'$}
          \ForAll{device $\in$ choosenDevice}
            \State timesDate = timesDate + Interval
            \State MapDeviceTranmissionTime.put(device,timesDate)
            \Statex\Comment{ \%comment: Create URL string and encode it by using timesDate date\%}
            \If{$selectJobType == 'Z'$}
              \If{$\exists$ randomTransmissionDate}
                \State readRandomExecutionDate()
              \Else
                \State addRandomExecutionDate()
              \EndIf
            \EndIf
          \EndFor
        \EndIf
      \EndFor
    \EndFor
  \end{algorithmic}
\end{algorithm}
\end{document}

Overleaf 上的演示

相关内容