用空格分隔用户输入数字

时间:2016-09-22 19:57:11

标签: c#

我需要将用户输入的数字(最多五位数)与中间的三个空格分开。因此,如果用户输入数字12345,则控制台打印出来," 1 2 3 4 5"。

我只是让控制台打印出数字:

database.yml

4 个答案:

答案 0 :(得分:1)

static void Main()
{
    Console.Write("Please enter in a five digit number: ");
    Console.WriteLine(string.Join(" ", Console.ReadLine().ToCharArray()));
    Console.Read();
}

答案 1 :(得分:0)

Hımmm,看起来像是一个家庭作业;)

Console.WriteLine("Please enter in a five digit number: ");
string userInput = Console.ReadLine();

for (int i = 0; i < userInput.Length; i++) {
 char[] ip = userInput.ToCharArray();
 Console.Write(ip[i] + "   ");
}
Console.ReadKey();

Console.WriteLine("Please enter in a five digit number: ");
string userInput = Console.ReadLine();

foreach(char c in userInput) {
 Console.Write(c + "   ");
}
Console.Read();

答案 2 :(得分:0)

        Console.WriteLine("Please enter in a five digit number: ");
        string num = Console.ReadLine();
        string result = "";
        foreach (char n in num)
        {
            result += n + "   ";
        }
        Console.Write(result);
        Console.ReadKey();

答案 3 :(得分:-1)

这应该可以帮到你:)

class MainClass
{
    Console.WriteLine ("Please enter in a five digit number: ")
    string answer = string.Empty;
    int userInput = 0;

    for(int i = 0, i < 5, i++)
    {
        userInput = Console.ReadLine();

        // Append 3 spaces to each input provided by the user
        answer = string.Format("{0}{1}{3}",answer, userInput, "   ");
    }

    Console.WriteLine(answer)
}