2016-03-25 21 views
0

Bir .txt dosyasındaki runları bir ArrayList<Integer> içine koyarak bulmak için bir yöntem yazıyorum. Bir koşunun bir int değeri ile yukarı veya aşağı gidip gitmediğini nasıl belirtmem gerektiği konusunda kafam karıştı.Yukarı veya aşağı koşuyu temsil etmek için tamsayı değeri nasıl kullanılır?

I uyarılması için bu pseudocode verildi:

yöntem FindRuns (In: PLIST Tamsayılar ArrayList olduğu; pDir RUNS_UP veya RUNS_DN int) yöntemi için Tamsayılar

ArrayList döndürür, böylece kadarıyla var:

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

yalancı kod daha sonra başka bir dizi sorar/eğer pDir RUNS_UP veya RUNS_DN olup olmadığı hakkında bildirimler. Bu noktada ne yapacağımdan emin değilim.

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 
+0

Lütfen küçük örneklerle açıklayabilir misiniz? –

+0

"ArrayList FindUpRuns (ArrayList pList)" yöntemini yazabilir misiniz? Öyleyse, ArrayList FindDownRuns (ArrayList pList) 'adlı ikinci bir yöntem yazın, sonra bunları nasıl bir araya getireceğinizi düşünün. –

+0

Hayır Tassos, projenin her ikisini de bulmak için FindRuns yöntemini kullanacağını belirtir. I/else ifadeleri için tüm pseudocode'ları gönderirim: i

cevap

1

Programınız muhtemelen böyle, sabitleri tanımlamak gerekir:

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

    // place the method you are writing here 
} 

Ardından bu yüzden gibi yöntemde bunları kullanabilirsiniz:

İşte

tam sözde koddur
if (pDir == RUNS_UP) 
{ 
    // runs up 
} 

ve

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