Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在';已从用户输入c创建#_C#_Arrays_String_Loops_Foreach - Fatal编程技术网

C# 如何在';已从用户输入c创建#

C# 如何在';已从用户输入c创建#,c#,arrays,string,loops,foreach,C#,Arrays,String,Loops,Foreach,我试图制作一个简单的程序,利用for循环,一次一个地向数组中添加用户输入,这个程序使用 string []str = new string[10]; for (int i = 0; i < str.Length; i++) { Console.WriteLine("Please enter a number: "); str[i] = Console.ReadLine(); } 这是完整的资料来源 using System; using System.Collecti

我试图制作一个简单的程序,利用for循环,一次一个地向数组中添加用户输入,这个程序使用

string []str = new string[10];
for (int i = 0; i < str.Length; i++)
{
     Console.WriteLine("Please enter a number: ");
     str[i] = Console.ReadLine();
}
这是完整的资料来源

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();
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串[]str=新字符串[10];
对于(int i=0;i
更改输入代码,将用户输入直接保存在整数数组中,而不是字符串中

    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");
        }
    }
inti=0;
int[]值=新的int[10];
而(i
这将避免在此行
int[]Arr=newstring[]{str};
中尝试从字符串数组初始化整数数组,但编译器不满意时出现错误


除了明显的编译错误外,使用允许立即检查用户是否键入了非整数的内容,并且您可以拒绝下面一行中的输入。您正在尝试从所有输入创建整数数组。但实际上,这种语法不正确。首先,您尝试从中创建int数组字符串数组。这是不可能的。第二个字符串数组是这样创建的
newstring[]{“str”,“str”}
但您正在执行
newstring[]{str[]}
。因此,要解决所有这些问题,我建议替换

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


因为您创建了一个整数数组,并尝试用字符串填充/初始化它。请检查第二个代码框中的第二行代码。小挑剔:str本身是一个字符串[]。因此,他实际上尝试将字符串[]作为元素添加到字符串[]…旋转头…:)很好,但您已经接受了另一行代码,因此这对您来说不是很好
int[] Arr=new string [] {str};
int[] Arr = str.Select(s => int.Parse(s)).ToArray();