Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.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# 将字符串[]转换为int[],然后将int[]转换为单独的变量(x、y、z)_C#_Arrays_String_Methods_Integer - Fatal编程技术网

C# 将字符串[]转换为int[],然后将int[]转换为单独的变量(x、y、z)

C# 将字符串[]转换为int[],然后将int[]转换为单独的变量(x、y、z),c#,arrays,string,methods,integer,C#,Arrays,String,Methods,Integer,我是编程新手,决定采取步骤学习C#,在阅读某些教程、书籍和其他指南时,我遇到了StreamReader命令 由于我的学习方式一直是玩我觉得有趣的新命令,我决定通过命令挖掘我的方式,看看我能做些什么 这就引出了一个问题,我这里有我想要的代码 它从文件中获取行,将其转换为字符串数组,继续将其转换为int数组,然后我将数组的每个部分分类为一个变量,以便在代码的主要部分中使用 我想知道的是,有没有更干净的方法可以做到这一点? 当我包括第二个,口渴,等等。。。从文件中提取的行,我是否能够使用Turner方

我是编程新手,决定采取步骤学习C#,在阅读某些教程、书籍和其他指南时,我遇到了StreamReader命令

由于我的学习方式一直是玩我觉得有趣的新命令,我决定通过命令挖掘我的方式,看看我能做些什么

这就引出了一个问题,我这里有我想要的代码

它从文件中获取行,将其转换为字符串数组,继续将其转换为int数组,然后我将数组的每个部分分类为一个变量,以便在代码的主要部分中使用

我想知道的是,有没有更干净的方法可以做到这一点? 当我包括第二个,口渴,等等。。。从文件中提取的行,我是否能够使用Turner方法(?)重复此过程

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

namespace StreamReaderToIntArray
{
   class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            int y = 0;
            int z = 0;

            string[] stringSeperators = new string[] { " " };

            StreamReader myReader = new StreamReader("Values.txt");
            string line = myReader.ReadLine();
            var myArray = line.Split(stringSeperators, StringSplitOptions.RemoveEmptyEntries);
            myReader.Close();

            Turner(myArray, out x, out y, out z);
        }

        static private int Turner(string[] newArray, out int x, out int y, out int z)
        {
           int[] cArray = newArray.Select(int.Parse).ToArray();

           x = cArray[0];
           y = cArray[1];
           z = cArray[2];


            return x;
        }
    }
}

我看到的唯一问题是
int.Parse()
如果不喜欢输入,就会抛出异常。此外,您从不检查该行是否实际包含3个值。我不喜欢对三个值使用
out
,当它们可以组合成一个
struct Point{int x,y,z;}
并从函数返回时

因此,我建议使用
int.TryParse()
为解析创建一个数组,并返回一个与字符串数组大小相同的整数数组

{
    string line = myReader.ReadLine();
    var point = Point.Parse(line);
}

public struct Point
{
    readonly int x, y, z;  // Always make struct immutable
    public Point(int x,int y,int z)
    {
       this.x = x;
       this.y = y;
       this.z = z;
    }

    public int X { get { return x; } }
    public int Y { get { return y; } }
    public int Z { get { return z; } }

    public static Point Parse(string line)
    {
      var parts = line.Split(stringSeperators, StringSplitOptions.RemoveEmptyEntries);
      var array = parts.Select( (s)=> s.ParseInt() ).ToArray();
      if(array.Length>=3) 
      {
          return new Point(array[0],array[1],array[2]);
      }      
      throw new IndexOutOfRangeException();
    }
}

// Example extensions for parsing strings
public static class Extensions
{
    public int ParseInt(this string value)
    {
        int x = 0;
        int.TryParse(value, out x);
        return x;
    }
    public float ParseFloat(this string value)
    {
        float x = 0;
        float.TryParse(value, out x);
        return x;
    }
}

如果您确信文本文件包含一行,其中三个数字由一个或多个空格分隔,则此代码有效:

int[] values =
    File
        .ReadAllText("Values.txt")
        .Split(new [] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
        .Select(int.Parse)
        .ToArray();

int x = values[0];
int y = values[1];
int z = values[2];

在您的情况下,不要使用
StreamReader
尝试
File.ReadAllLines

但是使用这种方法,变量
x,y,z
将只保留最后一行的值。

考虑使用@ JA72的<代码>结构> /代码>保存值并创建值< <代码>列表>代码>或<代码>数组> <代码>值

为什么不直接从字符串数组中分配< <代码> x < /代码>、<代码> y>代码>代码> z >代码> >(NeWRAI[0)]?阅读您要学习的语言教程的前几页总是个好主意。我打赌它会说一些关于重复操作的事情…@ThorstenDittmar我不太清楚为什么我不直接分配它并在那里进行解析。问得好。@AlexeiLevenkov我已经读了一遍重复的操作,在这段代码中还没有包含它们。在多次让它做我想做的事情之前,我一直专注于让它先做我想做的事情。这意味着三天前的全新品牌。所以,像
Int.TryParse()
这样的语法对我来说是未知的,我现在将对此进行研究。
static void Main(string[] args)
{
    int x = 0;
    int y = 0;
    int z = 0;

    string[] stringSeperators = new string[] { " " };

    string[] lines = File.ReadAllLines("Values.txt");
    foreach(string line in lines)
    {
       string[] myArray = line.Split(stringSeperators,
                                     StringSplitOptions.RemoveEmptyEntries);
       Turner(myArray, out x, out y, out z); 
    }       
}