无情的bug制造者 2019-11-04 22:25 采纳率: 0%
浏览 1205
已采纳

c#怎么一次输入多个数字,类似于c语言的scanf

public static double Test(double x)
{
return x*x*Math.PI;
}
public static int Test(int i,int j)
{
return i * j;
}
public static double Test(double i, double j, double k)
{
return ((i + j) * k) / 2;
}
static void Main(string[] args)
{
double x = Convert.ToInt32(Console.ReadLine());
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int j = Convert.ToInt32(Console.ReadLine());
int k = Convert.ToInt32(Console.ReadLine());
int l = Convert.ToInt32(Console.ReadLine());
Test(x);
Test(a,b);
Test(j, k,l);
Console.WriteLine("圆的面积为"+Test(x));
Console.WriteLine("矩形的面积为"+Test(a, b));
Console.WriteLine("梯形的面积为"+Test(j, k, l));
}
上面的是我写的代码,但是每次输入都要写一行,影响观看,有没有像scanf这样的函数一次输入多个数字的函数

  • 写回答

5条回答 默认 最新

  • threenewbee 2019-11-05 23:04
    关注

    https://stackoverflow.com/questions/4223917/c-sharp-equivalent-of-c-sscanf

    将你的程序的属性中允许不安全代码

    public static unsafe void Main(string[] args)
    {
        int a, b,c;
        string str = "10 12 100"; //这里换成string str = Console.ReadLine();
        sscanf(str, ' ', &a, &b, &c);
        Console.WriteLine("{0} {1} {2}", a, b, c);
        Console.Read();
    }
    
    public static unsafe void sscanf(string str, char seperator, params int*[] targets)
    {
        var parts = str.Split(seperator);
        if (parts.Length != targets.Length) throw new ArgumentException();
        for (int i = 0; i < parts.Length; i++)
        {
            *targets[i] = int.Parse(parts[i]);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?