无法将类型'int'隐式转换为'int []' [英] Cannot implicitly convert type 'int' to 'int[]'

查看:230
本文介绍了无法将类型'int'隐式转换为'int []'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个策划游戏,我让用户猜测4-10而不是颜色之间的数字序列但由于某种原因我的GetRandomNumberCount,CountHits得到错误不能隐式地将类型转换为int []





任何指导都将不胜感激



 < span class =code-comment> //  此方法将随机数与用户猜测的随机数进行比较 
public static int CountHits( int userGuess, int [] randomNumbers)
{
int correctGuess = 0 ;


if (userGuess == randomNumbers)
{
Console.WriteLine( 点击);
Console.ReadLine();
correctGuess ++;
}
else
{
Console.WriteLine( 小姐);
Console.ReadLine();
}
return correctGuess;
}



  //  此方法获取在游戏中使用的随机数的数量。 
public static int GetRandomNumberCount( int 难度)
{
int [] randomNumber = 0 ;


if (难度== 1
{
randomNumber = GenerateRandomNumber( 0 4 );
}
其他 如果(难度== 2
{
randomNumber = GenerateRandomNumber( 1 6 < /跨度>);
}
其他 如果(难度== 3
{
randomNumber = GenerateRandomNumber( 1 11 < /跨度>);
}

return randomNumber;

}
// 此方法生成数字数组的随机数。
public static int [] GenerateRandomNumber( int min, int max)
{


// 这声明了一个包含5个元素的整数数组
// 并将所有这些值初始化为默认值
// 为零
int [] test2 = new int [ 5 ];

随机randNum = Random();
for int i = 0 ; i < test2.Length; i ++)
{
test2 [i] = randNum.Next(min,max);
}
return test2;
}

解决方案

错误是否来自此行

< pre lang =c#> int [] randomNumber = 0 ;

< br $>
应该 randomNumber 是单个变量而不是数组吗?


int [] randomNumber = new int [ ] {};


I am trying to make a mastermind game where I having the user guess a number sequence between 4-10 instead of colours but for some reason my GetRandomNumberCount, CountHits get the error Cannot implicitly convert type to int[]


Any guidance would be appreciated

// This method compares the random numbers to the ones the user has guessed
       public static int CountHits(int userGuess, int[] randomNumbers)
       {
           int correctGuess = 0;


           if(userGuess == randomNumbers)
           {
               Console.WriteLine("Hit");
               Console.ReadLine();
               correctGuess++;
           }
           else
           {
               Console.WriteLine("Miss");
               Console.ReadLine();
           }
           return correctGuess;
       }


// This method gets the quantity of random numbers to use in the game.
       public static int GetRandomNumberCount(int difficulty)
       {
           int[] randomNumber = 0;


           if(difficulty == 1)
           {
             randomNumber= GenerateRandomNumber(0, 4);
           }
           else if(difficulty == 2)
           {
                randomNumber = GenerateRandomNumber(1, 6);
           }
           else if (difficulty == 3)
           {
               randomNumber = GenerateRandomNumber(1, 11);
           }

           return randomNumber;

           }
       // This method generates the random numbers for the array of numbers.
       public static int[] GenerateRandomNumber(int min, int max)
       {


           // this declares an integer array with 5 elements
           // and initializes all of them to their default value
           // which is zero
           int[] test2 = new int[5];

           Random randNum = new Random();
           for (int i = 0; i < test2.Length; i++)
           {
               test2[i] = randNum.Next(min, max);
           }
           return test2;
       }

解决方案

Is the error coming perhaps from this row

int[] randomNumber = 0;


Should randomNumber be a single variable instead of an array ?


int[] randomNumber = new int[] { };


这篇关于无法将类型'int'隐式转换为'int []'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆