有关C#的问题。我是初学者,想做一个程序,在控制台上输入一个int数字后,将其每个数字拆分。

来源:百度知道 编辑:UC知道 时间:2024/06/07 18:38:34
比如5487 拆分后是5 4 8 7 。 15487 拆分后是 1 5 4 8 7.
不要用投机取巧的方法。

不限制位数,比如3位数 4位数 5位数都要

不要有限制。 请给出具体的C#程序代码。
请把输入的数字定义为int型 string型有取巧的嫌疑……

using System;
using System.Collections.Generic;

namespace splitTest
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("Hello World!");
string strIN = "";
strIN = Console.ReadLine();
foreach(char s in strIN)
{
Console.Write(s + ",");
}
Console.ReadKey();
}
}
}

using System.Collections.Generic;

public List<int> SplitInt(int input)
{
List<int> result;
while(input)
{
int i = input%10;
result.Append(i);
input = input/10;
}
return result;
}

result[0]、[1]……就是你要的分开的数字。

string strIN = "";
strIN = Console.ReadLine();
foreach(char s in strIN)
{
int number = Convert .ToInt32(s);

Console.Write(number);

}
Console.ReadKey();