繁体   English   中英

生成 20 个随机数并在数组中搜索一个数字的程序

[英]Program that generates 20 random numbers and search the array for a number

我想制作一个生成 20 个随机数并在数组中搜索一个数字的程序。 如果在输入中输入了 20 个随机数之一,则 output 应该说“它在这里”。 如果数字不在 ReadLine 中,它应该说“是的,它在那里”。我想知道如何使所有 20 个随机数都能够搜索。 现在的代码只能搜索右边的数字。 即使输入是 20 个随机数之一,除了右边的那个,它也会说“不,它不在这里”。

图片是我现在的output。 它显示 20 个数字。 输出1输出2

public static void Main(string[] args)
{
    Random random = new Random();
    int[] myIntArray = new int[100];

    for (int i = 1; i <= 20; i++)
    {
        int x = random.Next(100);

        myIntArray[i] = x;

        Console.Write(myIntArray[i] + " ");

        if (i == 20)
        {

            Console.Write("\nType a number to search for:");
            int z = Convert.ToInt32(Console.ReadLine());
            if (z == x)
            {
                Console.WriteLine("Yes it is there.");
            }
            else
            {
                Console.WriteLine("No it is not here.");
            }

        }
    }

    Console.ReadKey();
}

您当前的代码检查 20tyh 项 ( x ) 是否等于用户输入 ( z ):

    if (i == 20) // 20th item only
    {    
        int z = Convert.ToInt32(Console.ReadLine());

        if (z == x) // if 20th item (x) equals to user input (z)
        {
            Console.WriteLine("Yes it is there.");
        }
        else
        {
            Console.WriteLine("No it is not here.");
        } 
    } 

尝试逐步解决问题:

  1. 声明数组
  2. 用随机值填充它
  3. 打印出数组
  4. 向用户询问号码
  5. 执行搜索

代码:

public static void Main(string[] args)
{
    // Array...    
    int[] myIntArray = new int[20]; // We want 20 items, not 100, right? 

    // Of random items
    Random random = new Random(); 

    for (int i = 0; i < myIntArray.Length; ++i)
        myIntArray[i] = random.Next(100);

    // Debug: let's have a look at the array:     
    Console.WriteLine(string.Join(" ", myIntArray));

    // myIntArray is built. Time to ask user for a number
    Console.Write("Type a number to search for:");

    if (int.TryParse(Console.ReadLine(), out int number)) {
         // number is a valid integer number, let's scan myIntArray 
         bool found = false;

         foreach (int item in myIntArray)
             if (item == number) {
                 found = true;

                 break; 
             }   

        if (found) 
            Console.WriteLine("Yes it is there.");
        else
            Console.WriteLine("No it is not here.");                
    }
    else 
        Console.WriteLine("Not a valid integer");

    Console.ReadKey();
}

编辑:在现实生活中,我们经常使用Linq来查询 collections(数组):

using System.Linq;

... 

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

    int[] myIntArray = Enumerable
      .Range(0, 20)
      .Select(i => random.Next(100)) 
      .ToArray();   

    Console.WriteLine(string.Join(" ", myIntArray));

    Console.Write("Type a number to search for:");

    if (int.TryParse(Console.ReadLine(), out int number)) 
        if (myIntArray.Any(item => item == number)) 
            Console.WriteLine("Yes it is there.");
        else
            Console.WriteLine("No it is not here.");                
    else 
        Console.WriteLine("Not a valid integer");

    Console.ReadKey();
}

所以基本上你应该做一个循环,用一个数组用一个随机数初始化你的数组

for (int i = 1; i <= 20; i++)
    {
        int x = random.Next(100);

        myIntArray[i] = x;
    }

之后,您可以在另一个循环中在控制台中搜索键入的值

Console.Write("\nType a number to search for:");
bool isValueFound = false;
int z = Convert.ToInt32(Console.ReadLine());
for(int i=0; i<=20; i++){
    if (z == x)
            {
                Console.WriteLine("Yes it is there.");
            }
            else
            {
                Console.WriteLine("No it is not here.");
            }
}

所有代码:

public static void Main(string[] args)
{
    Random random = new Random();
    int[] myIntArray = new int[100];

    for (int i = 1; i <= 20; i++)
    {
        int x = random.Next(100);

        myIntArray[i] = x;
    }
    Console.Write("\nType a number to search for:");
    bool isValueFound = false;
    int z = Convert.ToInt32(Console.ReadLine());
    for(int i=0; i<=20; i++){
        if (z == x)
            {
                Console.WriteLine("Yes it is there.");
            }
            else
            {
                Console.WriteLine("No it is not here.");
            }
}
    Console.ReadKey();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM