编写程序以查找输入整数是负数还是零或正数。 [英] Write a program to find whether an input integer is negative or zero or positive.

查看:155
本文介绍了编写程序以查找输入整数是负数还是零或正数。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编写一个程序来查找输入整数是负数还是零或正数。



我尝试过:



write a program to find whether an input integer is negative or zero or positive.

What I have tried:

#include <stdio.h>
void main()
{
    int a = 0; // equal (=) added, Afzaal Ahmad Zeeshan
    printf("enter an integer\n");
    scanf("%d",&a);
    if(a>0)
    printf("the number is +ve");
    else if(a<o)
    printf("the number is negative");
    else
    printf("the number is zero");
}

推荐答案

您的代码存在严重问题,您编写的代码相信C编译器会像,它是好。

There are serious problems with your code, you wrote the code believing C compiler would be like, "it's ok".
#include <stdio.h>

void main() { 
   int a,0; 
   printf("enter an integer\n"); 
   scanf("%d",&a); 
   if(a>0) 
       printf("the number is +ve"); 
   else if(a<o)> 
       printf("the number is negative");
   else 
       printf("the number is zero"); 
}</stdio.h>



有很多问题,这会导致程序无法编译。我将逐一开始,首先,


There are many problems, and that would cause the program to not compile. I will start one by one, first of all,

int a = 0; 



是唯一可接受的表格。你不能写它, int a,0 。它不像 mov eax,0x0 的汇编指令。等等



最后, 0!= o 。在您自己的常规日使用中,您可以将0发音为O。但在一个程序中,它们有不同的含义。一个是整数值,另一个是变量的(可能)名称,否则是非法字符。因此,将其更改为,


Is the only acceptable form. You cannot write it like, int a, 0. It is not like assembly directives of, mov eax, 0x0. etc.

Finally, 0 != o. In your own regular day usage, you may pronounce "0" as, "O". But in a program, they have different meanings. One is an integer value, other is a (possible) name of a variable, otherwise illegal character. So, change that to,

else if(a<0)



然后编译它。如果编译器不将警告视为错误,那么它将起作用。


Then compile this. If compiler doesn't consider warnings as errors, then it would work.

#include <stdio.h>

int main() { 
   int a = 0; 
   printf("enter an integer\n"); 
   scanf("%d", &a); 

   if(a > 0)  {
       printf("the number is +ve"); 
   } else if(a < 0) {
       printf("the number is negative");
   } else {
       printf("the number is zero"); 
   }
   return 0;
}
</stdio.h>



这样可行。此外,请记住编写具有良好可读性因素的正确代码。否则,读者实际上无法理解发生了什么。


This would work. Besides, remember to write proper code with a good readability factor. Otherwise, readers cannot actually understand what is going on.


这篇关于编写程序以查找输入整数是负数还是零或正数。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆