生成20个随机数并在数组中搜索数字的程序 [英] Program that generates 20 random numbers and search the array for a number

查看:69
本文介绍了生成20个随机数并在数组中搜索数字的程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个生成20个随机数并在数组中搜索一个数字的程序.如果在输入中键入了20个随机数之一,则输出应显示它在这里".如果该数字不在ReadLine中,则应该说是的,它在那里".我想知道如何制作所有20个随机数字以进行搜索.现在的代码只能搜索右边的数字.即使输入是20个随机数之一(右侧的数字除外),它也会说不,它不在这里."

I want to make a program that generates 20 random numbers and search the array for a number. If one of the 20 random numbers was typed in the input the output should say "it is here". If the number is not in the ReadLine it should say"Yes it is there".I want to know how to make all 20 random numbers to be able to search. The code right now can only search the number on the right. Even if the input is one of the 20 random numbers except for the one on the right it would say "No it is not here."

图片是我当前的输出.它显示了20个数字.输出1

The picture is my current output. IT IS SHOWING 20 NUMBERS.Output1Output2

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 ):

Your current code checks if 20tyh item (x) is equal to user input (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 查询集合(数组):

In real life, we often use Linq to query collections (arrays):

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();
}

这篇关于生成20个随机数并在数组中搜索数字的程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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