简体   繁体   English

C#程序,其中数组索引和元素的大小来自用户输入,然后搜索特定元素

[英]C# program where size of array index and elements are from user input and then search for a specific element

I'm trying to create a program where the size of array index and its elements are from user input. 我正在尝试创建一个程序,其中数组索引及其元素的大小来自用户输入。 And then the program will prompt the user to search for a specific element and will display where it is. 然后程序将提示用户搜索特定元素并显示它的位置。

I've already come up with a code: 我已经想出了一个代码:

using System;

namespace ConsoleApplication1
{

class Program
{

  public static void Main(String [] args)
  {
    int a;
    Console.WriteLine("Enter size of index:");
    a= int.Parse(Console.ReadLine());
    int [] index = new int [a];
    for (int i=0; i<index.Length;i++)
    {
      Console.WriteLine("Enter number:");
      index[i]=int.Parse(Console.ReadLine());
    }

  }
}
}

The problem with this is that I can't display the numbers entered and I don't have any idea how to search for an array element. 这个问题是我无法显示输入的数字,我不知道如何搜索数组元素。 I'm thinking of using if statement. 我正在考虑使用if语句。

Another thing, after entering the elements the program should display the numbers like Number 0 : 1 另外,在输入元素后,程序应该显示数字0:1

Is this correct: Console.WriteLine("Number"+index[a]+":"+index[i]); 这是正确的: Console.WriteLine("Number"+index[a]+":"+index[i]); ?

And where should I put the statement? 我应该在哪里发表声明? after the for loop or within it? 在for循环之后还是在它之内?

You're on the right track. 你走在正确的轨道上。 Look carefully at how you stuffed values into the array, and you might find a clue about "how to search for an array element" with specific value. 仔细查看如何将值填充到数组中,您可能会发现有关“如何搜索具有特定值的数组元素”的线索。 This is a core introductory algorithm, so no shortcuts! 这是一个核心的入门算法,所以没有捷径! You need to find the answer on your own :-). 你需要自己找到答案:-)。

你可以使用Array.IndexOf

What is the last line Console.WriteLine(index[i]); 什么是最后一行Console.WriteLine(index[i]); ? It seems like you are using the loop variable outside the loop. 看起来你在循环之外使用循环变量。

To display entered numbers (ie. if I understand well, the numbers in the array), you have just to walk through the array like this: 要显示输入的数字(例如,如果我理解的话,数组中的数字),你只需要像这样遍历数组:

for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

Since you want display numbers only after every number is entered, you may put this code only after finishing the loop where the user is entering the numbers: 由于您只想在输入每个数字后显示数字,因此您可以在完成用户输入数字的循环后放置此代码:

// The user is entering the numbers (code copied from your question).
for (int i = 0; i < index.Length; i++)
{
    Console.WriteLine("Enter number: ");
    index[i] = int.Parse(Console.ReadLine());
}

// Now display the numbers entered.
for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

// Finally, search for the element and display where it is.
int elementToSearchFor;
if (int.TryParse(Console.ReadLine(), out elementToSearchFor))
{
    // TODO: homework to do.
}

To search for a number, you can either walk through the array again and compare each element until finding a good one, or use Linq TakeWhile() method. 要搜索数字,您可以再次遍历数组并比较每个元素,直到找到一个好的,或使用Linq TakeWhile()方法。 (I suppose that your intent is not to use Linq, so I don't provide any further detail in this direction.) (我想你的意图不是使用Linq,所以我没有提供任何进一步的细节。)

        int a;
        Console.WriteLine("Enter size of Array:-");
        a = int.Parse(Console.ReadLine());
        int[] array = new int[a];
        Console.WriteLine("Enter the Elements of the Array:-");
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = int.Parse(Console.ReadLine());
        }
        Console.WriteLine("\nThe Elemets of the Array are:-");
        for (int j = 0; j < array.Length; j++)
        {
            Console.WriteLine(array[j]);
        }

Just try to break down what you've done and what you still need to do 试着打破你做过的事情以及你还需要做些什么

Create a program where: 创建一个程序,其中:

1) size of its array elements comes from user input (check) 1)其数组元素的大小来自用户输入(检查)
2) array elements come from user input (check) 2)数组元素来自用户输入(检查)
3) Prompt the user to search for a specific element (TODO) 3)提示用户搜索特定元素(TODO)
4) Display where it is (TODO) 4)显示它的位置(TODO)

From the code you've written so far I would think you have most of what you need to do #3 & #4 从你到目前为止编写的代码中我会认为你有大部分需要做的事情#3&#4

An if statement may come into play when finding the location of the element the user specifies. 当找到用户指定的元素的位置时,if语句可以起作用。

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

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