好吧,我正在尝试制作一个简单的程序,该程序利用 for 循环并一次将用户输入添加到数组中,这使用了
string []str = new string[10];
for (int i = 0; i < str.Length; i++)
{
Console.WriteLine("Please enter a number: ");
str[i] = Console.ReadLine();
}
但是当我尝试使用 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++;
}
}
这是完整的来源,
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();
}
}
}