如何遍历从用户输入c#创建的数组

时间:2022-11-29 17:24:34

Well I'm trying to make a simple program that takes advantage of a for loop and adds user input to an array one at a time, this uses this

好吧,我正在尝试创建一个简单的程序,利用for循环并一次一个地添加用户输入,这使用这个

string []str = new string[10];
for (int i = 0; i < str.Length; i++)
{
     Console.WriteLine("Please enter a number: ");
     str[i] = Console.ReadLine();
}

But When i try and loop through the array with a foreach statement, i get an error stating that i can't implicitly convert string[] to type String; the foreach statement is this:

但是当我尝试使用foreach语句循环遍历数组时,我得到一个错误,指出我不能隐式地将string []转换为String类型; foreach声明是这样的:

int even=0; int odd=0;

int[] Arr=new string [] {str};

foreach (int i in Arr)
{
    if (i % 2 == 0)
    {
        even++;
    }
    else
    {
        odd++;
    }
}

And here is the full source,

这里是完整的来源,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = new string[10];
            for (int i = 0; i < str.Length; i++)
            {
                Console.WriteLine("Please enter a number: ");
                str[i] = Console.ReadLine();
            }
            int even = 0; int odd = 0;
            int[] Arr = new string[] { str };
            foreach (int i in Arr)
            {
                if (i % 2 == 0)
                {
                    even++;
                }
                else
                {
                    odd++;
                }
            }
            Console.WriteLine("There is " + even + " even numbers.");
            Console.WriteLine("There is " + odd + " odd numbers");
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}

2 个解决方案

#1


2  

Change your input code to save the user input directly in an array of integers instead of strings

更改输入代码以将用户输入直接保存在整数数组中而不是字符串中

    int i = 0;
    int[]values = new int[10];
    while(i < values.Length)
    {
        Console.WriteLine("Please enter a number: ");
        int result;
        string input = Console.ReadLine();
        if(Int32.TryParse(input, out result)
        {
            values[i] = result;
            i++;
        }
        else
        { 
            Console.WriteLine("Not a valid integer");
        }
    }

This will avoid the error when in this line int[] Arr=new string [] {str}; you try to initialize an array of integers from an array of strings and the compiler is not happy with it

这将避免在此行中的错误int [] Arr = new string [] {str};你尝试从一个字符串数组初始化一个整数数组,编译器不满意它

Apart from the obvious compilation error, using Int32.TryParse allows to check immediately if the user types something that is not an integer and you can deny the input

除了明显的编译错误,使用Int32.TryParse允许立即检查用户是否键入了不是整数的东西,你可以拒绝输入

#2


0  

In the line below you are trying to create an integer array from all your input. But actually this kind of syntax is not correct. Firstly you are trying to create an array of int out of array of string. This is not possible. Secondly a string array is created like new string[]{"str", "str"} but you are doing new string[]{str[]}. So to solve all these issues i recommend replace

在下面的行中,您尝试从所有输入创建一个整数数组。但实际上这种语法并不正确。首先,您尝试使用字符串数组创建一个int数组。这是不可能的。其次,创建一个字符串数组,就像new string [] {“str”,“str”},但是你正在做新的string [] {str []}。所以要解决所有这些问题,我建议更换

int[] Arr=new string [] {str};

with

int[] Arr = str.Select(s => int.Parse(s)).ToArray();

#1


2  

Change your input code to save the user input directly in an array of integers instead of strings

更改输入代码以将用户输入直接保存在整数数组中而不是字符串中

    int i = 0;
    int[]values = new int[10];
    while(i < values.Length)
    {
        Console.WriteLine("Please enter a number: ");
        int result;
        string input = Console.ReadLine();
        if(Int32.TryParse(input, out result)
        {
            values[i] = result;
            i++;
        }
        else
        { 
            Console.WriteLine("Not a valid integer");
        }
    }

This will avoid the error when in this line int[] Arr=new string [] {str}; you try to initialize an array of integers from an array of strings and the compiler is not happy with it

这将避免在此行中的错误int [] Arr = new string [] {str};你尝试从一个字符串数组初始化一个整数数组,编译器不满意它

Apart from the obvious compilation error, using Int32.TryParse allows to check immediately if the user types something that is not an integer and you can deny the input

除了明显的编译错误,使用Int32.TryParse允许立即检查用户是否键入了不是整数的东西,你可以拒绝输入

#2


0  

In the line below you are trying to create an integer array from all your input. But actually this kind of syntax is not correct. Firstly you are trying to create an array of int out of array of string. This is not possible. Secondly a string array is created like new string[]{"str", "str"} but you are doing new string[]{str[]}. So to solve all these issues i recommend replace

在下面的行中,您尝试从所有输入创建一个整数数组。但实际上这种语法并不正确。首先,您尝试使用字符串数组创建一个int数组。这是不可能的。其次,创建一个字符串数组,就像new string [] {“str”,“str”},但是你正在做新的string [] {str []}。所以要解决所有这些问题,我建议更换

int[] Arr=new string [] {str};

with

int[] Arr = str.Select(s => int.Parse(s)).ToArray();