【问题标题】:C coding interestingC 编码有趣
【发布时间】:2021-12-19 05:56:15
【问题描述】:
#include <stdio.h>

int main (void) //starting entry of integer indicate not associated with any data types argument
{
    int value1, value2, calculate = 0; //integer values
    
    printf ("\nProgram should perform the following:"); 
    printf("\n"); 
    
    printf ("\nHave the user enter two different integer numbers to be divided by the program "); 
    {

                printf ("\nIf the division of both numbers");
                printf("\n"); 
                printf ("\nresult with the remainder of zero,"); 
        
                printf("\n"); 
    
                printf ("\nby the second number.");
    
                printf("\n"); 
    
                printf ("Example: thirty-five divided by seven will have a remainder of zero"); 
    
                            printf ("\n"); 
    }
    
    printf ("Please enter the first integer number>>"); 
    scanf  ("%i", &value1);
    
    printf ("Please enter the second integer>> ");  
    scanf  ("%i", &value2); 
       
    if (value1 % value2 == 0 ) {
      
       printf ("\n%i is evenly divisible by %i\n", value1, value2);
       calculate = value1 / value2;
       printf ("%i / %i = %i\n", value1, value2, calculate);
       
       
    else
       printf ("\n%i is not evenly divisible by %i\n", value1, value2);
    

return 0;

}

我编写的这段代码工作正常。但是我似乎找不到使用哪个 else 或 else if 语句来使代码显示错误:如果用户输入的第二个数字为零,则程序中允许为零。

【问题讨论】:

  • 这不是C#,请去掉C#标签。另外,您能否改进代码格式(提示:使用编辑器中的{} 按钮)?
  • 你想要的错误信息不正确。仅允许输入的第一个数字为零 (value1)。 x/0 未定义,即使对于 x==0
  • 请发布实际可编译的代码。这个区块在哪里关闭? if (value1 % value2 == 0 ) {

标签: c windows printf


【解决方案1】:

不要在printf 之后使用括号,它不会改变任何内容,并且在scanfprintf 中使用%d 表示整数。并在 else 之前关闭 if 括号。

【讨论】:

    【解决方案2】:

    如果你想为用户引入这样的消息,你必须在用户提供输入后执行条件来检查数字。可以这样做:

    #include <stdio.h>
    
    int main(void)  //starting entry of integer indicate not associated with any data types argument
    {
        int value1, value2, calculate = 0;  //integer values
        printf("\nProgram should perform the following:");
        printf("\n");
        printf("\nHave the user enter two different integer numbers to be divided by the program ");
        printf("\nIf the division of both numbers");
        printf("\n");
        printf("\nresult with the remainder of zero,");
        printf("\n");
        printf("\nby the second number.");
        printf("\n");
        printf("Example: thirty-five divided by seven will have a remainder of zero");
        printf("\n");
        printf("Please enter the first integer number>>");
        scanf("%i", &value1);
        printf("Please enter the second integer>> ");
        scanf("%i", &value2);
    
        if (value1 == 0 && value2 != 0)
        {
            printf("\nA zero is allowed in the program if the second number that the user enters is zero");
            return 0;
        }
        else
        {
            if (value1 % value2 == 0)
            {
                printf("\n%i is evenly divisible by %i\n", value1, value2);
                calculate = value1 / value2;
                printf("%i / %i = %i\n", value1, value2, calculate);
            }
            else
            {
                printf("\n%i is not evenly divisible by %i\n", value1, value2);
            }
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-05
      • 2016-02-19
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多