2011-09-19 82 views
1

在下面的代碼,我算字符串的編號,然後嘗試檢查數量匹配值 的代碼是正確的,但我不知道如何將一個矩陣比較計數值:如何比較表?

例如:計數器1給出:

10 
150 
10 
0 
200 

我需要檢查它是否匹配值?

int[][] values = { { 10, 150, 10, 0, 10, 200 }, { 20, 5, 18, 60, 14, 0 }, { 16, 5, 18, 20, 25, 30 } }; 

for (int i = 1; i < 4;i++) 
    {  
     //do action here 
     int counter1 = 0; 
     int counter2 = 0; 
     int counter3=0; 
     int counter4=0; 
     int counter5=0; 
     int counter6=0; 

     for (int j = 0; j < myTableNbRows; i++) 
     { 
     if (myTable.getValue(j, 0).matches("hello")) 
     { 
      if (myTable.getValue(j, 1).contains("tions.")) 
      { 
      counter1++; 
      } 
     if (myTable.getValue(j, 1).contains("ons.")) 
      { 
      counter2++; 
      } 
    if (myTable.getValue(j, 1).contains("an.")) 
      { 
      counter3++; 
      } 
     if (myTable.getValue(j, 1).contains("auns.")) 
      { 
      counter4++; 
      } 
    if (myTable.getValue(j, 1).contains("896.")) 
      { 
      counter5++; 
      } 
    if (myTable.getValue(j, 1).contains("1222.")) 
      { 
      counter6++; 
      } 

     } 
     } 
//here I nedd to compare values with counter 
assertEquals(values,counter1); 
assertEquals(values,counter2); 

....

+1

你能舉一個例子,你的意思是「比較值與計數器」?我假設例如counter1的值是否在數組「values」中的某處! – nyxz

+0

我需要使用assertEquals來驗證值==計數器 – lola

+0

提示:修復縮進以使代碼結構易於閱讀。 – Kelvin

回答

0

兩個選項... Store中的一個陣列櫃中,並調用斷言在一個循環

for (int t = 0; t<counters.length; t++) { 
    assertEquals(values[i][t], counters[t]); 
} 

另一種選擇是使用Hamcrest匹配器。例如,IsArrayContainingInOrder。

IsArrayContainingInOrder

for (int i = 1; i < 4;i++) 
{  
    //do action here 
    int[] counters = new int[6]; 

    for (int j = 0; j < myTableNbRows; i++) 
    { 
    if (myTable.getValue(j, 0).matches("hello")) 
    { 
     if (myTable.getValue(j, 1).contains("tions.")) 
     { 
     counters[0]++; 
     } 
    ... 
    ... 
    } 

    for (int t = 0; t<counters.length; t++) { 
    assertEquals(values[i][t], counters[t]); 
    } 
    // or preferred method 
    assertThat(counters, IsArrayContainingInOrder.arrayContaining(values[i])); 
} 
+0

爲什麼添加for(int t ???),我應該在代碼中添加它嗎? – lola

+0

您當前的** assertEquals **所在的位置。只需將您的計數器放在數組中,以便您可以循環使用它們。 –

+0

as遵循?int [] counter = {0,0,0,0,0,0}; for(int t = 0; t <5; t ++ assertEquals(values [i] [t],counters [t]) ; } – lola

0

也許你應該看一看的

Character.toString() 

方法。

+0

這樣做的價值? – lola

+0

請解釋這將如何幫助? – Kelvin

0

我不知道什麼values實際上代表,但假設它應該是一個組定義需要匹配計數(即你有(counter1==10 AND counter2==150 ...) OR (counter1==20 AND counter2==5 ...) ...),然後再嘗試這樣的事:

boolean matchFound = false; 
for(int[] valuesRow : values) { 
    matchFound = valuesRow[0] == counter1 && valuesRow[1] == counter2 ...; 
    if(matchFound) { 
    break; 
    } 
} 

注意,通常,你應該做一個counter陣列:int[] counter = new int[6];並增加數組內容。然後你可以遍歷如上圖所示,但在另一行中的元素比較值的行:

boolean matchFound = false; 
for(int[] valuesRow : values) { 
    boolean rowMatches = true; //assume a match 
    for(int i = 0; i < counters.length; i++) { 
    if(valuesRow[i] != counters[i]) { 
     rowMatches = false; 
     break; //break the inner loop 
    } 
    } 
    if(rowMatches) { 
    matchFound = true; 
    break; //the outer loop 
    } 
} 

補充說明:您也可以使用循環標籤和直接繼續外環一旦你發現一個不匹配值。

編輯:重新讀你的代碼,我意識到你有4個值的數組,並通過4(?)數據集/表循環。

不過,也有一些問題:

  1. for (int j = 0; j < myTableNbRows; i++)你遞增i,而不是作爲j我以爲會是正確的
  2. 你需要調用斷言在環外自等counter1是不可見。

這就是說,有你的斷言像這樣(在內環):

assertEquals(values[i-1][0], counter1); //note that you loop i from 1 to 4 so you need to subtract 1 (array indices are 0 to 3) 
assertEquals(values[i-1][1], counter2); 
... 

EDIT2: 如果你想要做你的外環後斷言,你需要做計數器2維數組:int[][] counter = new int[4][6];並且像這樣增加:counter[i-1][j]++;(注意如上所述的i-1)。然後循環valuescounter並進行比較。