C#-hdu-1000-A+B问题

再回归C#学习。

杭电的online judge支持C#,正好用来练习。

http://acm.hdu.edu.cn/showproblem.php?pid=1000

参考:https://blog.csdn.net/fcxxzux/article/details/54573392

输入:

有多行,每一行有两个整数a、b,中间用空格隔开。

输出:

每一行a+b的结果占一行。

用C#解决,要点如下:

1、从键盘读入一行。string s=Console.ReadLine();

2、判断读入结束的条件。if(s==null) break;

3、把字符串按照空格隔开。string[] a = s.Split(' ');

4、数据类型转换。int b = Convert.ToInt32(a[0]);

5、输出一行。Console.WriteLine(b );

完整代码如下:

using System;


namespace test
{
    class Program
    {
        
        static void Main(string[] args)
        {
            string s ;           
            while(true)
            {
                s = Console.ReadLine();
                if (s == null)
                    break;

                string[] a = s.Split(' ');
                
                int b, c;
                b = Convert.ToInt32(a[0]);
                c = Convert.ToInt32(a[1]);
                Console.WriteLine(b + c);

            }            

        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43917370/article/details/108062144