求大侠帮忙用C语言,从键盘输入两个整数,要求求和然后输出和。

如题所述

代码如下:

using System;

using System.Collections.Generic;

namespace TestProject

{

class Program

{

static void Main(string[] args)

{

int a, b, sum = 0;

var readLine = "";

Console.WriteLine("程序功能:请输入两个数字计算其和(空格分割,回车结束),输入exit结束:");

//将输入的字符串放至readLine变量中,然后判断是否为退出条件 exit 

while ((readLine = Console.ReadLine()).ToLower() != "exit")

{

var arr = readLine.Split(new char[] { ' ' });

if (arr.Length != 2)

{

Console.WriteLine("输入不正确,请重新输入!");

continue;

}

//尝试转化输入的两个字符串是否为整数

if (!int.TryParse(arr[0], out a) || !int.TryParse(arr[1], out b))

{

Console.WriteLine("输入的字符串不是整数,请重新输入!");

continue;

}

sum = a + b;

Console.WriteLine("结果:{0} + {1} = {2}", a, b, sum);

a = b = sum = 0;

}

Console.WriteLine("程序结束,按回车结束。");

Console.ReadLine();

}

}

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-29
你这样的问题百度上估计得上万,为什么不搜索一下呢,还要花花钱提问。给你个简单的吧:
#include "stdio.h"
void main()
{
int a,b;
scanf("%d %d",&a,&b);
printf("%d",a+b);
}
输入的时候,两个数字之间加个空格。如果运行之后,窗口不能停下看结果,就在printf这一句的下面再加一句:getch(); 。
第2个回答  2013-03-29
#include <stdio.h>
int main()
{
int i,j,k;

printf("input two number : ");

scanf("%d %d",&i,&j);

k=i+j
;
printf("sum = %d \n",k);

return 0;
}
第3个回答  2013-03-29
#include <iostream>
#include <string>

using namespace std;

int main()
{
int a;
int b;

cin>>a>>b;
cout<<a + b<<endl;

system("pause");
return 1;
}
第4个回答  2013-03-29
#include "stdio.h"
void main()
{
int a,b,c;
scanf("%d %d",&a,&b);
c=a+b;
printf("%d",c);
}本回答被提问者采纳