2013-03-12 108 views
-1

C#的新手只是想知道錯誤是什麼,無法理解錯誤。不需要更正,只是想知道錯誤。 錯誤: 'Systems.Collections.Generic.List.BinarySearch(int,System.Collections.Generic.Icomparer)'的最佳重載方法匹配有一些無效參數 參數1:無法從system.collections.generic.list轉換爲詮釋 ARGUMENT2:不能從int轉換爲system.collections.generic.icomparer無法從int轉換爲system.collections.generic.icomparer <int>

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Text;enter code here 
using System.Windows.Forms; 

namespace LotoNumbers 
{ 
    public partial class FrmLotto : Form 
{ 
    // declare class level variables 
    List<int> player = new List<int>(7); 
    List<int> computer = new List<int>(6); 

    public FrmLotto() 
    { 
     InitializeComponent(); 
    } 

    // event method to generate player's numbers 
    private void btnPlayer_Click(object sender, EventArgs e) 
    { 
     // declare and initalize local variables 
     string display = ""; 

     // reset the winning number label before generating the player's numbers 
     lblComputerNumber.Text = "00 00 00 00 00 00"; 

     // generate unique random numbers 
     GenerateRandomNumbers(player); 
     player.Sort(); 
     // build display string 
     display = BuildDisplayString(player); 

     // display winning number in label 
     lblPlayerNumber.Text = display; 
    } 

    // method to generate computer's random numbers (the 'winning numbers') 
    // and determine how many winning numbers 
    private void btnComputer_Click(object sender, EventArgs e) 
    { 
     // declare and initalize local variables 
     int winCount = 0; 
     string display = ""; 

     // generate unique random numbers 
     GenerateRandomNumbers(computer); 

     // sort the array in ascending order 
     computer.Sort(); 

     // build display string 
     display = BuildDisplayString(computer); 

     // display winning number in label 
     lblComputerNumber.Text = display; 

     // determine if this number matches any of the players numbers 
     winCount = CompareTwoList(player, computer); 

     // display the total winning numbers 
     lblMatching.Text = winCount.ToString("D2"); 
    } 

    private void GenerateRandomNumbers(List<int> numberList) 
    { 


     Random lucky = new Random(); 

     // generate unique random numbers 
     for (int index = 0; index < numberList.Capacity; index++) 
     { 
      int temp = lucky.Next(1, 50); 
      if (numberList.IndexOf(temp) < 0) 
      { 
       numberList.Add(temp); 
      } 
      else 
      { 
       index--; 
      } 
     } 

    } 

    private string BuildDisplayString(List<int> numberList) 
    { 
     // declare method variable 
     string display = " "; 

     // loop through the array and build a display string 
     foreach (int number in numberList) 
      display += number.ToString("D2") + " "; 

     // return display string 
     return display; 
    } 

    private int CompareTwoList(List<int> list1, List<int> list2) 
    { 
     // declare method variable 
     int numberMatching = 0; 

     // loop through each element in the first array looking for a match in the second array 
     foreach (int value in list1) 
     { 
      if (player.BinarySearch(list2, value) >= 0) 
       numberMatching++; // a matching value is found 
     } 

     return numberMatching; 
    } 
} 

}

+4

如果你只是已經公佈的相關部分它會幫助,如果你提到的地方引發錯誤行。 – 2013-03-12 16:14:49

回答

2

它看起來像你想:

if (list2.BinarySearch(value) >= 0) 
    numberMatching++; // a matching value is found 

您正在致電List<int>.BinarySearch(int, IComparer<int>)。由於valueint您收到類型錯誤。

注意,你也可以做:

int numberMatching = list2.Intersect(list1).Count(); 
0

沒有爲List<int>.BinarySearch()無過載,它接受一個List<int>int作爲參數。 See MSDN

您需要使用player.BinarySearch(value)

相關問題