【发布时间】:2016-08-18 03:52:03
【问题描述】:
我试图将此字符串转换为数组,但我不断收到错误消息,提示我无法将 char[] 隐式类型转换为 int[]。代码如下:
using System;
using System.IO;
using System.Linq;
public class Readingfiles{
public static void larry(string[] args)
{
if (args == null || args.Length == 0) {
Console.WriteLine("Error: please specify the file to read!");
Console.ReadKey();
return;
}
try {
StreamReader src = new StreamReader(args[0]);
while(!src.EndOfStream)
{
string line = src.ReadToEnd();
int[] num = line.ToArray();
Console.WriteLine(num);
}
}
catch (Exception ex)
{
Console.WriteLine("Error while reading the file! " + ex.ToString());
}
Console.ReadKey();
}
static void Main(String[] args)
{
larry(args);
}
}
我知道它发生在 while 循环中,但我不知道为什么我不能这样做,还有其他方法吗? p.s. c# 新手。
编辑 我以前的方式是字符串后面没有行,它产生了这个:
207 554 171 542 677 91 227 492 611 904 246 896 919 223 763 235 534 89 1 760 301 678 474 414 975 52 774 809 595 426 253 643 781 581 948 318 511 118 953 758 876 579 436 829 752 780 211 381 417 342 217 414 925 786 674 427 254 876 31 910 759 925 326 381 660 64 87 504 46 730 471 745 473 789 103 966 760 585 292 432 333 914 488 140 864 391 596 629 765 693 189 136 382 85 201 70 977 828 511 896
但我喜欢这样做:
[207, 554, 171, 542, 677, 91、227、492、611、904、 246、896、919、223、763、 235、534、89、1、760、 301、678、474、414、975、 52、774、809、595、426、 253、643、781、581、948、 318、511、118、953、758、 876、579、436、829、752、 780、211、381、417、342、 217、414、925、786、674、 427、254、876、31、910、 759、925、326、381、660、 64, 87, 504 46, 730, 471、745、473、789、103、 966、760、585、292、432、 333、914、488、140、864、 391、596、629、765、693、 189、136、382、85、201、 70, 977, 828, 511, 896,]
这是一个文本文件,我在终端上使用 sublime text 和 mcs/mono。
【问题讨论】:
-
可能是因为字符串是字符数组而不是整数数组。
-
1) 请尝试更努力地格式化您的代码。很难阅读。 2) “Java 新手”与 C# 无关……
-
你的文件内容是什么?你能解释一下你的整数数组的内容是什么吗?
-
该死的我的意思是说 c# 的困意
-
@Fabjan 更好地查看代码。 src.ReadToEnd
标签: c# arrays while-loop compiler-errors implicit-conversion