Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.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_C#_.net - Fatal编程技术网

C# 拆分字符串,然后解析为int

C# 拆分字符串,然后解析为int,c#,.net,C#,.net,这是我正在做的练习: 编写一个以abcd格式(如2011)输入四位数字的程序,并执行以下操作: •计算数字之和(在我们的示例中为2+0+1+1=4) •在控制台上按相反顺序打印号码:dcba(在我们的示例1102中) •将最后一位数字置于第一位:dabc(在我们的示例1201中) •交换第二位和第三位数字:acbd(在我们的示例2101中) 这是我的代码: using System; using System.Collections.Generic; using System.Linq; usi

这是我正在做的练习:

编写一个以abcd格式(如2011)输入四位数字的程序,并执行以下操作:

•计算数字之和(在我们的示例中为2+0+1+1=4)

•在控制台上按相反顺序打印号码:dcba(在我们的示例1102中)

•将最后一位数字置于第一位:dabc(在我们的示例1201中)

•交换第二位和第三位数字:acbd(在我们的示例2101中)

这是我的代码:

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            string FourDigitNum = Console.ReadLine();
            string[] digits = FourDigitNum.Split();
            int firstDigit = int.Parse(digits[0]);
            int secondDigit = int.Parse(digits[1]);
            int thirdDigit = int.Parse(digits[2]);
            int fourthDigit = int.Parse(digits[3]);
            int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
            string reversed = digits[3] + digits[2] + digits[1] + digits[0];
            string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0];
            string exchanged = digits[0] + digits[2] + digits[1] + digits[3];
            Console.WriteLine("The Sum is: {0}", sum);
            Console.WriteLine("The Reversed number is: {0}", reversed);
            Console.WriteLine("The Last Digit is First: {0}", lastCharFirst);
            Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged);
        }
    }
}
当我使用1100作为输入时,我得到的错误是:

未处理的异常:System.IndexOutOfRangeException:索引为 在数组的边界之外。在 控制台应用程序6.Program.Main(字符串[]args)位于 c:\Users\User1\Documents s\Visual Studio 2013\Projects\ConsoleApplication6\Program.cs:第16行

编辑:非常感谢,我误解了拆分的方式();工作。这是我最后的工作代码:

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            string digits = Console.ReadLine();
            int firstDigit = (int)Char.GetNumericValue(digits[0]);
            int secondDigit = (int)Char.GetNumericValue(digits[1]);
            int thirdDigit = (int)Char.GetNumericValue(digits[2]);
            int fourthDigit = (int)Char.GetNumericValue(digits[3]);
            int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
            Console.WriteLine("The Sum is: {0}", sum);
            Console.WriteLine("The Reversed number is: {3}{2}{1}{0}", firstDigit, secondDigit, thirdDigit, fourthDigit);
            Console.WriteLine("The Last Digit is First: {3}{0}{1}{2}", firstDigit, secondDigit, thirdDigit, fourthDigit);
            Console.WriteLine("The Second and Third Digit Exchanged: {0}{2}{1}{3}", firstDigit, secondDigit, thirdDigit, fourthDigit);
        }
    }
}

不需要
Split
,您假定通过执行
Split()
将每个字符拆分为字符串数组的元素。它不是,没有任何参数的
Split()
将在空白字符上拆分,并且由于在字符串
1102
中没有任何空白字符,因此将返回单个元素的数组。因此有例外

使用字符串作为字符数组,您可以访问每个索引,并通过连接字符创建一个反向字符串。还要学习调试和单步执行代码,这将帮助您排除代码故障

作为提示,请使用:

int firstDigit = (int) Char.GetNumericValue(FourDigitNum[0]);
问题是一致的

string[] digits = FourDigitNum.Split();
假设您有1100作为输入。你认为这会得到[1,1,0,0],但它会给你[1100]

你应该把它改成

string digits = Console.ReadLine();

将使用空格作为分隔符拆分字符串。

您正在对输入的字符串长度进行硬编码,但执行此操作。split()只在数组中创建一个项目,而不是四个

您可以将字符串拆分为一个字符数组。ToCharArray()可以创建一个数组,其中每个数字都是数组中自己的项。

当您这样做时

string[] digits = FourDigitNum.Split();
它只返回一个仅包含一项的数组。拆分没有按预期进行,这就是为什么在尝试执行拆分时,您得到的
索引超出了数组的界限

int secondDigit = int.Parse(digits[1]);

您误解了这里的字符串用法。字符串的核心也是字符的集合。您可以通过索引直接访问这些字符

您的代码应该如下所示:

    static void Main(string[] args)
    {
        string digits = Console.ReadLine();
        int firstDigit = Convert.ToInt32(digits[0]);
        int secondDigit = Convert.ToInt32(digits[1]);
        int thirdDigit = Convert.ToInt32(digits[2]);
        int fourthDigit = Convert.ToInt32(digits[3]);
        int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
        string reversed = digits[3] + digits[2] + digits[1] + digits[0];
        string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0];
        string exchanged = digits[0] + digits[2] + digits[1] + digits[3];
        Console.WriteLine("The Sum is: {0}", sum);
        Console.WriteLine("The Reversed number is: {0}", reversed);
        Console.WriteLine("The Last Digit is First: {0}", lastCharFirst);
        Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged);
    }

您的FourDigitNum.Split()没有按您认为的那样执行。不是将每个数字分割成一个数组,而是得到一个数组,该数组有一个元素,即四位数

改变

string[] digits = FourDigitNum.Split();

确保你有

using System.Linq; 
使用
stations使用另一个
,其余代码可以保持不变

结果:

2011
The Sum is: 4
The Reversed number is: 1102
The Last Digit is First: 1202
The Second and Third Digit Exchanged: 2101
“IndexOutOfRangeException”表示您试图访问超出范围的集合元素。在这种情况下,索引
1
超出数组
数字的范围

您可以使用调试器检查数组中的内容。方便的是,当您在VisualStudio中遇到未捕获的异常时,它通常会为您创建一个断点

*“Locals”选项卡当前处于选中状态,但如果要编写自己的表达式,可以选择“Watch”标记

如您所见,
digits
数组只包含一个元素,
digits[0]
,它包含您的整个输入

至于如何实际获取字符串数字列表,有很多方法,这里有一个简单的方法

string[] digits = FourDigitNum.ToCharArray().Select(c => c.ToString()).ToArray();

现在,如果你是为学校做这件事,你的老师可能会怀疑你使用了
Select
语句,但我会让你考虑另一种方法作为练习。

没有定义的字符可以分割。您可以按如下方式遍历字符串中的每个字符:

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

namespace ConsoleApplication6
{
class Program
{
    static void Main(string[] args)
    {
        string FourDigitNum = Console.ReadLine();
        List<string> digitsList = new List<string>();
        foreach (char c in FourDigitNum)
        {
            digitsList.Add(c.ToString());
        }
        string[] digits = digitsList.ToArray();
        int firstDigit = int.Parse(digits[0]);
        int secondDigit = int.Parse(digits[1]);
        int thirdDigit = int.Parse(digits[2]);
        int fourthDigit = int.Parse(digits[3]);
        int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
        string reversed = digits[3] + digits[2] + digits[1] + digits[0];
        string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0];
        string exchanged = digits[0] + digits[2] + digits[1] + digits[3];
        Console.WriteLine("The Sum is: {0}", sum);
        Console.WriteLine("The Reversed number is: {0}", reversed);
        Console.WriteLine("The Last Digit is First: {0}", lastCharFirst);
        Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged);

    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序6
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串FourDigitNum=Console.ReadLine();
List digitsList=新列表();
foreach(FourDigitNum中的字符c)
{
添加(c.ToString());
}
string[]digits=digitsList.ToArray();
int firstDigit=int.Parse(数字[0]);
int secondDigit=int.Parse(数字[1]);
int thirdDigit=int.Parse(数字[2]);
int-fourthDigit=int.Parse(数字[3]);
整数和=第一位数+第二位数+第三位数+第四位数;
字符串反转=数字[3]+数字[2]+数字[1]+数字[0];
字符串lastCharFirst=数字[3]+数字[0]+数字[1]+数字[0];
交换的字符串=数字[0]+数字[2]+数字[1]+数字[3];
WriteLine(“总和是:{0}”,总和);
Console.WriteLine(“反转的数字为:{0}”,反转);
WriteLine(“最后一个数字是First:{0}”,lastCharFirst);
WriteLine(“交换的第二个和第三个数字:{0}”,交换);
}
}
}


我还没有在机器上检查过,但我认为它应该能工作。试着执行一次。

那么您做了什么调试?提示:将断点放在
int firstDigit=…
并查看
数字
…您的FourDigitNum.Split()没有执行您认为会执行的操作。我使用的测试输入是11000。您的
lastCharFirst
中的最后一个值也不正确。应该是
数字[2]
…只是说;)下一步,这个问题显示了理解发生了什么的努力,提供了错误和预期结果。我认为在这种情况下投反对票是不公平的。不完全正确。Split()实际上会拆分为一个数组。但是,在使用
1102
作为输入的情况下,它只会给出
数字[0]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication6
{
class Program
{
    static void Main(string[] args)
    {
        string FourDigitNum = Console.ReadLine();
        List<string> digitsList = new List<string>();
        foreach (char c in FourDigitNum)
        {
            digitsList.Add(c.ToString());
        }
        string[] digits = digitsList.ToArray();
        int firstDigit = int.Parse(digits[0]);
        int secondDigit = int.Parse(digits[1]);
        int thirdDigit = int.Parse(digits[2]);
        int fourthDigit = int.Parse(digits[3]);
        int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
        string reversed = digits[3] + digits[2] + digits[1] + digits[0];
        string lastCharFirst = digits[3] + digits[0] + digits[1] + digits[0];
        string exchanged = digits[0] + digits[2] + digits[1] + digits[3];
        Console.WriteLine("The Sum is: {0}", sum);
        Console.WriteLine("The Reversed number is: {0}", reversed);
        Console.WriteLine("The Last Digit is First: {0}", lastCharFirst);
        Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged);

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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            string FourDigitNum = Console.ReadLine();
            int firstDigit = int.Parse(FourDigitNum.SubString(0,1));
            int secondDigit = int.Parse(FourDigitNum.SubString(1,1));
            int thirdDigit = int.Parse(FourDigitNum.SubString(2,1));
            int fourthDigit = int.Parse(FourDigitNum.SubString(3,1));
            int sum = firstDigit + secondDigit + thirdDigit + fourthDigit;
            string reversed = fourthDigit.ToString()  + thirdDigit.ToString()  + secondDigit.ToString()  + firstDigit.ToString();
            string lastCharFirst = fourthDigit.ToString() + firstDigit.ToString() + secondDigit.ToString() + firstDigit.ToString();
            string exchanged = firstDigit.ToString() + thirdDigit.ToString() + secondDigit.ToString() + fourthDigit.ToString();
            Console.WriteLine("The Sum is: {0}", sum);
            Console.WriteLine("The Reversed number is: {0}", reversed);
            Console.WriteLine("The Last Digit is First: {0}", lastCharFirst);
            Console.WriteLine("The Second and Third Digit Exchanged: {0}", exchanged);
        }
    }
}