输入Nemerle

时间:2009-09-13 01:02:55

标签: c# nemerle

我想知道如何从表单控制台获取输入:

M 14 65 99 in nemerle。在C#中,我这样做:

            string[] input = System.Console.ReadLine().Split(' ');
            ch = System.Char.Parse(input[0]);
            a  = System.Int32.Parse(input[1]);
            d =  System.Int32.Parse(input[2]);
            m =  System.Int32.Parse(input[3]);

但这不适用于Nemerle。请建议我在Nemerle如何做。

2 个答案:

答案 0 :(得分:5)

class Test
{
  public static Main () : void
  {
    def input = System.Console.ReadLine ().Split (' ');
    def ch = System.Char.Parse (input[0]);
    def a  = System.Int32.Parse (input[1]);
    def d =  System.Int32.Parse (input[2]);
    def m =  System.Int32.Parse (input[3]);

    System.Console.WriteLine ("ch:{0} a:{1} d:{2} m:{3}",  ch, a, d, m);
  }
}

答案 1 :(得分:3)

您也可以使用IO宏:

using Nemerle.IO;
using System;

mutable ch, a, d, m;
try
{
    scanf("%c %d %d %d", ch, a, d, m);
    printf("%c %d %d %d\n", ch, a, d, m);
}
catch
{
    | _ is InvalidInput => Console.WriteLine("Invalid input")
}

请注意,与C ++不同,Nemerle版本的printf和scanf是安全的。如果传递正确类型的参数,它们将仅编译。在上面的示例中,甚至可以从使用中推断出正确的类型。