如何使用整数值来表示向上或向下运行?

时间:2016-03-25 07:24:44

标签: java arraylist methods

我正在编写一种方法,通过将它们放入ArrayList<Integer>来查找.txt文件中的运行。我很困惑,我应该如何指示一个运行是以int值上升还是下降。

我被赋予了这个伪代码以提示:

  

方法FindRuns(在:pList是整数的ArrayList; int pDir是RUNS_UP或RUNS_DN)返回整数的ArrayList

到目前为止我的方法是:

private ArrayList<Integer> FindRuns(ArrayList<Integer> pList, int pDir)

然后伪代码会提示一系列关于pDir是RUNS_UP还是RUNS_DN的else / if语句。我不确定此时该怎么做。

这是完整的伪代码:

Method FindRuns(In: pList is ArrayList of Integers; int pDir is RUNS_UP or RUNS_DN) Returns ArrayList of Integers 
    listRunsCount ← arrayListCreate(pList.size(), 0)
    Declare int varaibles i ← 0, k ← 0
    While i < pList.size() - 1 Do 
        If pDir is RUNS_UP and pList element at i is  ≤ pList element at i + 1 Then
            Increment k
        Else If pDir is RUNS_DN and pList element at i is  ≥ pList element at i + 1 Then
            Increment k
        Else If k  0 ≠ Then
            Increment the element at index k of listRunsCount
            k ← 0
        End If
        Increment i
    End While

    If k  0 ≠ Then 
        Increment the element at index k of listRunsCount
    End If

    Return listRunsCount 

End Method FindRuns

1 个答案:

答案 0 :(得分:1)

您的程序应该定义常量,如下所示:

public class TheProgram
{
    public static final int RUNS_UP = 1;
    public static final int RUNS_DOWN = 2;

    // place the method you are writing here
}

然后您可以在方法中使用它们,如下所示:

if (pDir == RUNS_UP)
{
    // runs up
}

if (pDir == RUNS_DOWN)
{
    // runs down
}