读书人

非常经典的A+B problem怎么读取两个数

发布时间: 2012-04-14 17:14:21 作者: rapoo

非常经典的A+B problem如何读取两个数?
题目中要求两个数之间可以用任意个空格、回车来分隔,多组数据。
请问应该如何读?比如C里scanf就行了,C++用cin,难不成在C#中还得一个个Console.Read()再判断不成?

[解决办法]

探讨

这两个我都用过,可是如果用spilt那么两个数必须在同一行。否则如果一行只有一个数,用readline读取,再试图spilt会错误。
C#没有类似Java里的Scanner类吗?

[解决办法]
C# code
int pos = 0;int[] array = new int[5];Console.WriteLine("please input 5 numbers:");while (pos < 5){    string[] s = Console.ReadLine().Split(' ');    for (int i = pos; i < pos + s.GetLength(0) && pos < 5; i++, pos++)        array[i] = Convert.ToInt32(s[i - pos]);}Console.Write("\r\nyour entered: " + array[0]);for (int i = 1; i < 5; i++){    Console.Write(", " + array[i];}Console.WriteLine(); 

读书人网 >C#

热点推荐