创建随机数并将其存储在数组中



我试图让用户猜测随机数,然后检查他具体得到了多少,请帮忙。

using System;
using System.Threading;
namespace lab_12
{
class Program
{
static void Main()
{    
Random random = new Random();
int[] num = new int[7];
for (int i  = 0; i < num.Length; i++)
{
num[i] = random.Next(10);
Console.Write(num[i]);
}
Thread.Sleep(4000);
Console.Clear();
Console.WriteLine("type your answer");
string temp = Console.ReadLine();
int x = Convert.ToInt32(temp);                
}
}
}

我想这就是你想要的?

static void Main(string[] args)
{
Random random = new Random();
int[] num = new int[7];
for (int i = 0; i < num.Length; i++)
{
num[i] = random.Next(10);
Console.Write(num[i]);
}
Thread.Sleep(4000);
Console.Clear();
int correctCount = 0;
for (int i = 0; i < num.Length; i++)
{
bool canParse;
int answer;
do
{
Console.Write($"{i + 1}. Type your answer: ");
canParse = int.TryParse(Console.ReadLine(), out answer);
}
while (!canParse);
if (num[i] == answer)
{
correctCount++;
Console.WriteLine("Correct. Go Next");
}
else
{
Console.WriteLine($"You are wrong. Result is {num[i]}");
}
}
Console.WriteLine($"You answered {correctCount} out of {num.Length} correctly");
Console.ReadKey();
}

最新更新