如果 y==0 如何让 C 接受
How to make C accept if y==0
#include <stdio.h>
int main() {
int x,y,add,subtraction,multiplication,division;
printf("Write x:");
scanf("%d",&x);
printf("Write y:");
scanf("%d",&y);
add = x+y;
printf("Add=%d\n",add);
subtraction = x-y;
printf("subtraction = %d\n",subtraction);
multiplication = x*y;
printf("multiplication = %d\n",multiplication);
division = x/y;
if (y == 0)
printf("Operation not permitted");
else
printf("division = %d\n",division);
return 0;
}
所以
当我打印 y=0 时什么也没有发生
无“不允许操作”或除法的回答
我该怎么办?
在 else
中进行除法。像这样:
#include <stdio.h>
int main()
{
int x,y,sum,roz,iloczyn,iloraz;
printf("Wprowadz x:");
scanf("%d",&x);
printf("Wprowadz y:");
scanf("%d",&y);
sum = x+y;
printf("Suma=%d\n",sum);
roz = x-y;
printf("roznica = %d\n",roz);
iloczyn = x*y;
printf("iloczyn = %d\n",iloczyn);
if (y == 0)
{
printf("Operation not permitted");
exit(0);
}
else
{
iloraz = x/y;
printf("iloraz = %d\n",iloraz);
return 0;
}
}
只需放置此语句
division = x/y;
在 if 语句中,如
if (y == 0)
{
printf("Operation not permitted");
}
else
{
division = x/y;
printf("division = %d\n",division);
}
return 0;
#include <stdio.h>
int main() {
int x,y,add,subtraction,multiplication,division;
printf("Write x:");
scanf("%d",&x);
printf("Write y:");
scanf("%d",&y);
add = x+y;
printf("Add=%d\n",add);
subtraction = x-y;
printf("subtraction = %d\n",subtraction);
multiplication = x*y;
printf("multiplication = %d\n",multiplication);
division = x/y;
if (y == 0)
printf("Operation not permitted");
else
printf("division = %d\n",division);
return 0;
}
所以 当我打印 y=0 时什么也没有发生 无“不允许操作”或除法的回答 我该怎么办?
在 else
中进行除法。像这样:
#include <stdio.h>
int main()
{
int x,y,sum,roz,iloczyn,iloraz;
printf("Wprowadz x:");
scanf("%d",&x);
printf("Wprowadz y:");
scanf("%d",&y);
sum = x+y;
printf("Suma=%d\n",sum);
roz = x-y;
printf("roznica = %d\n",roz);
iloczyn = x*y;
printf("iloczyn = %d\n",iloczyn);
if (y == 0)
{
printf("Operation not permitted");
exit(0);
}
else
{
iloraz = x/y;
printf("iloraz = %d\n",iloraz);
return 0;
}
}
只需放置此语句
division = x/y;
在 if 语句中,如
if (y == 0)
{
printf("Operation not permitted");
}
else
{
division = x/y;
printf("division = %d\n",division);
}
return 0;