在我的方法public static int PointsCalculator()
中,我似乎无法使用其他两种方法中的值来比较两个数组中的值并给出点。
我不确定如何调用或初始化值以开始但尝试快速模型。
我的目标是为GetUserGuesses
方法中的每个用户值添加一个与RandomBingoNumbers
方法中的随机值相同的点。
class UserBingo // Creates a class to store all user related
{
static int[] RandomNrs;
static int[] userGuesses;
static int num = 0;
public static int[] GetUserGuess() // A method for collecting user guesses and displaying them (SPLIT THEM UP)
{
userGuesses = new int[10]; // Creates the array to store 10 user inputs
for (int i = 0; i < userGuesses.Length; i++) // For loop itterates until 10 user values are put into the array
{
try
{
int input = Convert.ToInt32(Console.ReadLine());
if (input < 1 || input > 25) // Only values between 1-25
{
Console.WriteLine("Only enter a number between 1-25");
i--;
}
else if (userGuesses.Contains(input)) // Checks for repeated values
{
Console.WriteLine("You have already entered {0}", input);
i--;
}
else
{
userGuesses[i] = input; //When they do meet the correct values
Console.WriteLine("Thanks for entering the number " + input);
}
}
catch (Exception)
{
Console.WriteLine("Only numbers can be entered, ");
i--;
}
}
for (int i = 0; i < userGuesses.Length; i++) // Loop to display all the input values in the array
{
Console.Write(userGuesses[i]);
}
return userGuesses;
}
public static int[] RandomBingoNumbers() // Method for creating 10 random integers
{
RandomNrs = new int[10]; // Creates an array to store 10 integer
Random rnd = new Random();
for (int i = 0; i < RandomNrs.Length; i++)
{
num = rnd.Next(1, 25);
if (RandomNrs.Contains(num))
i--;
else
RandomNrs[i] = num;
}
return RandomNrs;
}
public static int PointsCalculator() //Method for calculating score
{
int points = 0; // Integer to hold the amount of correct guesses
for (int i = 0; i < 10; i++)
{
if (RandomNrs[i] == userGuesses[i]) // Check if the 10 user inputs is the same as the 10 randomized numbers
{
points++;
}
}
Console.WriteLine("Your Numbers: \n [{0}]", string.Join(", ", userGuesses)); // Display user inputed numbers
Console.WriteLine("Bingo Numbers: \n [{0}]", string.Join(", ", RandomNrs)); // Display the random bingo numbers
Console.WriteLine("You got: " + points + "poäng"); // Display the amount of correct guesses
return points;
}
}
}
答案 0 :(得分:0)
添加using System.Linq
;
很多错误。
if (GetUserGuess == RandomBingoNumbers
) // Check if the 10 user inputs is the same as the 10 randomized numbers
{
points++;
}
应该是
if (GetUserGuess().SequenceEqual(RandomBingoNumbers())
// Check if the 10 user inputs is the same as the 10 randomized numbers
{
points++;
}
许多其他错误,例如 RandomNrs 必须是在函数外部声明的静态int []。等等..
此分配错误RandomNrs[i] = new UserBingoNrs;
。我建议你仔细阅读函数的整个语法和用法。
更新1
而不是这个
int[] RandomNrs = new int[10]; // Creates an array to store 10 integer
int[] userGuesses = new int[10]; // Creates the array to store 10 user inputs
你应该使用
RandomNrs = new int[10]; // Creates an array to store 10 integer
userGuesses = new int[10]; // Creates the array to store 10 user inputs
因为已经全局定义了RandomNrs和userGuesses。
而不是
RandomNrs == userGuesses
和RandomNrs != userGuesses
你必须使用
RandomNrs[i] == userGuesses[i])
和RandomNrs[i]!=userGuesses[i]
else if (RandomNrs[i] != userGuesses[i]) // If user guesses are not equal to array points
i--;
这可能会导致无限循环,而不是i--,做点 - ;
更新2
这很简单
int num= rnd.Next(1, 25);
while(RandomNrs.Contains(num))
num = rnd.Next(1,25);
更新3
将PointsCalculator更改为此循环
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
if (RandomNrs[i] == userGuesses[j]) // Check if the 10 user inputs is the same as the 10 randomized numbers
{
points++;
}
}