如何比较数组中的数字

时间:2015-10-20 22:14:27

标签: c# arrays comparison

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DiceGame
{
class Program
  {



    static void Main(string[] args)
    {
        Random randomnumber = new Random();

        int[] Player = new int[5];
        Player[0] = randomnumber.Next(1, 6); 
        Player[1] = randomnumber.Next(1, 6); 
        Player[2] = randomnumber.Next(1, 6); 
        Player[3] = randomnumber.Next(1, 6); 
        Player[4] = randomnumber.Next(1, 6);
        Console.WriteLine("You rolled a " + Player[0] + " " + Player[1] + " " + Player[2] + " " + Player[3] + " " + Player[4]);
         Console.WriteLine(" You have a " + however many are equal);
    }
  }
}

编辑:我需要将数组中的多个数字相互比较。他们随机1-6,如果2彼此相等,我需要显示对。如果3等于显示三种,如果是4种4种,5种是

edit2:我唯一能找到的是这个,但它只将Player [0]与数组的其余部分进行比较

for (int i = 0; i < Player.Length; i++)
        {
            int count = 0;
            for (int j = 0; j < Player.Length; j++)
            {
                if (Player[i] == Player[j])
                    count = count + 1;
            }

            Console.WriteLine("\t\n " + Player[i] + " occurs " + count);




            Console.ReadKey();

3 个答案:

答案 0 :(得分:1)

您可以使用foreach循环以传统方式编写此内容:

int theSame = 0;
foreach (var x in Player)
{    
     int localTheSame = 0;
     foreach (var y in Player)
     {
         if (x == y)
         {
             localTheSame++;
         }
     }

     if (localTheSame > theSame)
     {
         theSame = localTheSame;
     }
}       

我在edit2之前写了这个答案,所以我想你想要显示任何数字的最大出现次数。

修改

使用LINQ的相同解决方案:

int theSame = Player.Max(x => Player.Count(y => x == y));

答案 1 :(得分:1)

您可以使用LINQ来计算每个数字。

 Random randomnumber = new Random();

        int[] Player = new int[5];
        Player[0] = randomnumber.Next(1, 6); 
        Player[1] = randomnumber.Next(1, 6); 
        Player[2] = randomnumber.Next(1, 6); 
        Player[3] = randomnumber.Next(1, 6); 
        Player[4] = randomnumber.Next(1, 6);
        Console.WriteLine("You rolled a " + Player[0] + " " + Player[1] + " " + Player[2] + " " + Player[3] + " " + Player[4]);
        var selected = from x in Player.AsEnumerable()
                       group x by x into g
                       select new { Digit = g.Key, DigitCount = g.Count()};
     foreach(var sel in selected)
     {
       Console.WriteLine("You have rolled " + sel.Digit + " no of times " + sel.DigitCount);
     }

答案 2 :(得分:0)

你可以这么简单:

VERSION=72.0
METHOD=SetExpressCheckout
LOCALECODE=AU
RETURNURL=http://mysite.dev:1338/order/checkout-confirmation?id=1
CANCELURL=http://mysite.dev:1338/cart
PAYMENTREQUEST_0_PAYMENTACTION=Sale
PAYMENTREQUEST_0_CURRENCYCODE=AUD
NOSHIPPING=1
L_PAYMENTREQUEST_0_NAME0=The Kitty Ring
L_PAYMENTREQUEST_0_NUMBER0=2274
L_PAYMENTREQUEST_0_DESC0=
L_PAYMENTREQUEST_0_AMT0=50.00
L_PAYMENTREQUEST_0_QTY0=4.00
L_PAYMENTREQUEST_0_NAME1=GlassTop Digital Bathroom SLIM Scales Weight Body Fat Water Bone Muscle Calories
L_PAYMENTREQUEST_0_NUMBER1=2283
L_PAYMENTREQUEST_0_DESC1=
L_PAYMENTREQUEST_0_AMT1=32.95
L_PAYMENTREQUEST_0_QTY1=2.00
PAYMENTREQUEST_0_ITEMAMT=265.9000
PAYMENTREQUEST_0_HANDLINGAMT=0.00
PAYMENTREQUEST_0_AMT=265.9000