请问C#能否像C语言,scanf("%d %d",&a,&b),用户就能一行用空格隔开2数字,然后一起读入?

danbomingli_88 2012-09-09 07:50:35
请问C#能否像C语言,scanf("%d %d",&a,&b),用户就能一行用空格隔开2数字,然后一起读入?


这样用C语言的话,输入 1 2 就能使得 a=1 b=2

但C#中不知怎么实现。不想一行只能输入一个数字
...全文
3014 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
srhouyu 2013-01-18
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
本帖最后由 caozhy 于 2012-09-09 21:27:01 编辑 使用可变参数: C# code?1234567891011121314151617181920212223using System;using System.Collections.Generic;using System.Linq;using System.Te……
正则表达式是个办法
全栈极简 2012-09-09
  • 打赏
  • 举报
回复

class Program
{
static void Main(string[] args)
{
Console.WriteLine("请输入第一个数字:");
string num1 = Console.ReadLine();

Console.WriteLine("请输入第二个数字:");
string num2 = Console.ReadLine();

string temp = string.Format("{0} {1}", num1, num2);
Console.WriteLine(temp);
}
}
threenewbee 2012-09-09
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] input = Console.ReadLine().Split(new char[] {' ', ',', ';' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => int.Parse(x.Trim())).ToArray();
foreach (int i in input)
Console.WriteLine(i);
}
}
}
1, 2, 3, 4, 5
1
2
3
4
5
Press any key to continue . . .
threenewbee 2012-09-09
  • 打赏
  • 举报
回复
使用可变参数:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class Program
{
static Dictionary<string, string> MyScanf(string format, params string[] values)
{
var result = Regex.Match(Console.ReadLine(), format).Groups;
return values.Select((x, i) => new { key = x, value = result[i + 1].Value })
.ToDictionary(x => x.key, x => x.value);
}
static void Main(string[] args)
{
var input = MyScanf(@"(\d+) (\d+)", "a", "b");
Console.WriteLine("your input is a = {0}, b = {1}.", input["a"], input["b"]);
}
}
}


或者
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ConsoleApplication1
{
class RefType<T>
{
public T Value { get; set; }
public override string ToString()
{
return Value.ToString();
}
}

class Program
{
static void MyScanf(string format, params RefType<string>[] values)
{
var result = Regex.Match(Console.ReadLine(), format).Groups;
values.Select((x, i) => new { x = result[i + 1].Value, i }).ToList()
.ForEach(x => values[x.i].Value = x.x);
}
static void Main(string[] args)
{
RefType<string> a = new RefType<string>();
RefType<string> b = new RefType<string>();
MyScanf(@"(\d+) (\d+)", a, b);
Console.WriteLine("your input is a = {0}, b = {1}.", a, b);
}
}
}

1 2
your input is a = 1, b = 2.
Press any key to continue . . .
danbomingli_88 2012-09-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

Console.ReadLine 读入整行,然后按照你的规则分割。最简单的比如:string.Split 复杂点的用正则分割。
[/Quote]


那怎么把分离出来的比如10个数字,分别存在数组中,这段代码怎么写?有实例吗
机器人 2012-09-09
  • 打赏
  • 举报
回复
Console.ReadLine 读入整行,然后按照你的规则分割。最简单的比如:string.Split 复杂点的用正则分割。

111,108

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧