2012-04-23 67 views
3

我正在爲一所學校的項目工作,但無法弄清楚這一點。我是一個非常初級的初學btw。如何將6個整數的數組與一個2d的整數數組[19] [5]進行比較?

我有一個叫做tickets[19][6]的二維數組,每張票中有6張票。 我試圖將這20張票與一個常規的整數數組進行比較,這6個數字叫做winner[5],我從.txt文件中讀取這些票。

兩個陣列說明如下:

public static int[] winner = new int[5] 
public static int[][] tickets = new int[19][5] 

請記住我是很新的這一點,我感謝所有幫助提前!

編輯這是我用來分配用戶輸入到我的2D數組的循環,當我經歷整個事情時,它是一個無限循環。我認爲編寫代碼會更多....好吧,寫作!似乎更像目前爲止的調試技巧。

static void ticketNumberArray(){ 

    int number = 1;  // which of the six numbers you need from the ticket 
    int ticketCount = 1; // which ticket (out of 20) you are currently on 

    while(ticketCount<21){ // sentinel controlled while loop, 
          // will continue until the twentieth ticket is entered 
     System.out.println("Please type number " +number+ " of ticket number " +ticketCount+ "."); 
          //asks for the numbers of the ticket your currently on 

     Scanner keyboard = new Scanner(System.in); // initiates a scanner variable 

     int ticketNumber = keyboard.nextInt();  // assigns user input to the double variable ticketNumber 
                // and initializes as a double 

     tickets[ticketCount-1][number-1]=ticketNumber; // assigns user input into a 2-d array 

     number++;   //Sentinel variable 

     if(number==7){  //loop that controls the ticket count, every 6 numbers ='s one ticket 
      ticketCount++; 
      number=1; 
     } 
    } 
} 
+1

你是什麼意思,「比較」?測試平等? – 2012-04-23 00:29:07

+1

我相信測試是平等的。我試圖比較獲勝者[]和門票[] [],目的是通過搜索門票[] []來獲勝[] – BBradshaw1 2012-04-23 00:38:23

+0

您將使用所謂的「迭代」 - 「for '循環,例如。可能有3個嵌套循環。 (好吧,可能是2個循環,嵌套,重新讀你的問題。) – 2012-04-23 00:39:21

回答

1

門票是5個整數還是6?你的問題自相矛盾。

無論如何,如果您只是想檢查票中的整數是否匹配 - 如果它們的確切順序完全相同 - 最簡單的解決方案就是使用Arrays.equals(int[], int[])

+0

我已經試過,但是,票[] []是一個二維數組,它不會與勝利者[]比較......我嘗試過'deep.equals',但那並不是'不管工作。 =/ – BBradshaw1 2012-04-23 00:40:00

+1

你試過for循環嗎? – 2012-04-23 00:41:22

+0

@ user1350269:當然不是。你必須比較一個票與'贏家'。所以'Arrays.equals(ticket [i],winner)'會告訴你'tickets [i]'是否等於'winner'。 – 2012-04-23 00:42:33

3

首先,當您聲明數組時,您在[ ]中放入的數字就是數組的大小。因此,要製作包含六個項目的數組,您需要輸入[6]。索引將編號爲0 - > 5。

您只需循環tickets數組中的「行」,然後將其與贏家數組進行比較。每一行都是一張單獨的票。二維數組中的「列」將是構成票證的個別麻煩。

如果票中單個號碼的順序很重要,您可以使用Louis的建議Arrays.equal。 (即你只能帶票0-1-2-3贏得如果獲勝者是0-1-2-3 - 最彩票讓你贏得任意組合。)

for(int i=0; i < tickets.length; i++) 
{ 
    int[] ticket = tickets[i]; 

    if(Arrays.equals(ticket, winner)) 
    { 
     // This one is the winner 

     // For efficiency you should probably stop looping 
     break; 
    } 
} 

編輯:

很多教授介紹唐」不喜歡當學生使用API​​時。所以,你必須寫你自己的等號功能。

private static boolean areEqual(int[] a, int[] b) 
{ 
    if(a == null && b == null) 
     return true; 

    // Not equal if one is null and the other is not  
    if(a == null || b == null) 
     return false; 

    if(a.length != b.length) 
     return false; 

    // When we get here we have to check each element, one by one 
    // Implementation left as exercise :-) 
} 
+0

mk,所以我假設我必須分別填寫a和b以獲得勝利者和門票......但是,我不確定您的實施意味着什麼。這看起來不像我到目前爲止教過的任何代碼=/ – BBradshaw1 2012-04-23 01:01:12

+0

'areEqual'是一個函數,它應該和'Arrays.equals()'做同樣的事情。正如我所說,很多教授不喜歡打電話給API的學生。你只需調用'areEqual(ticket,winner)'而不是調用'Arrays ...'函數。我離開了實際檢查數組中元素的實現(在'areEqual()')中,你做... – debracey 2012-04-23 01:09:25

+0

好的,所以我認爲我可以從那裏... ...我也遇到了麻煩將用戶輸入分配給2d陣列。我只是試圖獲得所有20張入場券,而且我將無限循環。這是我得到的。 *添加循環到原始問題* – BBradshaw1 2012-04-23 01:20:47

相關問題