gpt4 book ai didi

java - 如何使用整数值来表示上涨或下跌?

转载 作者:行者123 更新时间:2023-12-01 10:09:37 25 4
gpt4 key购买 nike

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

我得到了这个伪代码来提示:

Method FindRuns(In: pList is ArrayList of Integers; int pDir is RUNS_UP or RUNS_DN) Returns ArrayList of Integers

到目前为止我有的方法:

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

最佳答案

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

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
}

关于java - 如何使用整数值来表示上涨或下跌?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36215739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com