【问题标题】:Why is the sum of two Integers in this case not working?为什么在这种情况下两个整数的总和不起作用?
【发布时间】:2021-02-19 08:47:18
【问题描述】:

我是 C 编程新手,遇到一个简单的问题。下面是代码...

#include <stdio.h>

/* Write a C Program that accepts two integers from the 
    user and calculate the sum of the two intergers. Go to the editor!
*/

int main() 
{
    
    int firstInteger, secondInteger;
    int sum;
    
    printf("Input two Integers: \n");
    scanf("%d%d", &firstInteger, &secondInteger);
    
    sum = firstInteger + secondInteger;
     printf("%d", sum);

    return 0;
}

在 GCC 编译器上运行我的代码后,我没有得到我期望的结果!

C:\XXXX\XXXX\XXXX\XXXX\XXXX>gcc 2sumOfTwoIntegers.c

C:\XXXX\XXXX\XXXX\XXXX\XXXX>a
Input two Integers:
25, 38
25

为什么我没有得到我输入的整数的总和?

【问题讨论】:

  • 这就是你检查scanf()的返回值的原因!
  • 程序的赋值是接受来自用户的两个整数还是接受一个整数、一个逗号和一个整数?

标签: c scanf conversion-specifier


【解决方案1】:

您正在输入逗号。只放一个空格 像那样 25 38

【讨论】:

    【解决方案2】:

    你有两种方法可以解决这个问题-

    1. 删除输入的两个整数之间的“,”。只需输入:25 38。
    2. 如果您想在输入整数之间添加一个“,”,那么您需要在代码中进行更改。在 scanf 行你应该写:
    scanf("%d,%d", &firstInteger, &secondInteger);
    

    [注意:您在 %d 中提供的内容将在输入部分跳过。就像你想在两个变量中取两个整数小时和分钟一样。然后你可以写:

    scanf("%d:%d", &hour, &minute);
    

    因此,如果您在输入部分写入 - 10:30,则小时变量取 10,分钟变量取 30。 ]

    【讨论】:

      【解决方案3】:

      您希望用户输入两个整数,在它们之间使用逗号。用户可以输入两个整数,例如

      25,38
      

      25, 38
      

      25 , 38
      

      25 ,38
      

      所有这些输入都是有效的。您需要正确处理输入。

      您需要跳过两个数字之间的逗号。

      还要考虑到两个整数之和可能太大而无法存储在 int 类型的对象中。

      这是一个执行该任务的演示程序。

      #include <stdio.h>
      
      int main(void) 
      {
          int firstInteger, secondInteger;
      
          printf( "Input two Integers (first , second): " );
          
          if ( scanf( "%d %*c %d", &firstInteger, &secondInteger ) == 2 )
          {
              long long int sum = ( long long int )firstInteger + secondInteger;
              
              printf( "The sum of the integers is %lld\n", sum );
          }
          else
          {
              puts( "Incorect input." );
          }
          
          return 0;
      }
      

      它的输出可能看起来像

      Input two Integers (first , second): 25 , 38
      The sum of the integers is 63
      

      另一种方法是读取变量中的逗号并检查用户是否确实键入了逗号。在这种情况下,程序可能看起来像

      #include <stdio.h>
      
      int main(void) 
      {
          int firstInteger, secondInteger;
          char c;
          
          printf( "Input two Integers (first , second): " );
          
          if ( scanf( "%d %c %d", &firstInteger, &c, &secondInteger ) == 3 && c == ',' )
          {
              long long int sum = ( long long int )firstInteger + secondInteger;
              
              printf( "The sum of the integers is %lld\n", sum );
          }
          else
          {
              puts( "Incorect input." );
          }
          
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        scanf() 默认情况下不要在输入中跳过, 并停在那里,因为它不能被解释为整数。

        , 添加到这样的格式中以使scanf() 读取并删除,

            scanf("%d,%d", &firstInteger, &secondInteger);
        

        【讨论】:

          猜你喜欢
          • 2017-11-26
          • 2010-12-29
          • 1970-01-01
          • 2021-08-05
          • 2011-11-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多