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

我是研究僧i 提交于 2021-02-11 15:53:33

问题


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."

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

回答1:


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.");
        } 
    } 

Try solving the problem step by step:

  1. Declare the array
  2. Fill it with random values
  3. Print the array out
  4. Ask user for a number
  5. Perform the search

Code:

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

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



回答2:


So basicly you should make a loop that initialize your array with a random numbers with one array

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

        myIntArray[i] = x;
    }

After that you can search for the typed value in console in another loop

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.");
            }
}

All code:

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


来源:https://stackoverflow.com/questions/61797877/program-that-generates-20-random-numbers-and-search-the-array-for-a-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!