为什么条件没有在单一陈述上停止? [英] Why if condition is not stopping on single statement?

查看:79
本文介绍了为什么条件没有在单一陈述上停止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到错误,该程序将执行所有操作,即Add,Sub等,但我只想执行单个操作。

我怎么做?



为什么scanf(%c,& op)在程序中的任何地方都没有工作,除了第一名?





I got error that this program will execute for all operation i.e Add,Sub etc but i want to execute it only for single operation.
How i can do it?
and
Why scanf("%c",&op) is not working anywhere in program except first place?


#include<stdio.h>
int add(int x,int y)
{
    int z;
    z=x+y;
    return z;
}
int multi(int x,int y)
{
    int z;
    z=x*y;
    return z;
}
int divi(int x,int y)
{

    int z;
    z=x/y;
    return z;
}

int sub(int x,int y)
{

    int z;
    z=x-y;
    return z;
}
main()
{
    int z;
    int firstv,secv;
    char op;
    printf("Please enter operator\n");
    scanf("%c",&op);
    printf("Please enter first value to calculation\n");
    scanf("%d",&firstv);
    printf("Please enetr second number\n");
    scanf("%d",&secv);
        if (op="+")
        z= add(firstv,secv);
        printf("The addtion of %d and %d is %d\n",firstv,secv,z);
        if (op="-")
        z=sub(firstv,secv);
        printf("The substration of %d and %d is %d\n",firstv,secv,z);

        if (op="*")
        z= multi(firstv,secv);
        printf("the multiplication of %d and %d is %d\n",firstv,secv,z);

        if(op="/")
        z=divi(firstv,secv);
        printf("The division of %d and %d is %d",firstv,secv,z);

        getch();
}

推荐答案

我不知道你的scanf问题。

使用Visual Studio 2010,我试过这个并且它有效:

I don''t know about your scanf issue.
With Visual Studio 2010, I tried this and it worked:
#include<conio.h>
int main()
{
    int z;
    int firstv,secv;
    char op;
    printf("Please enter operator\n");
    scanf("%c",&op);
    printf("Please enter first value to calculation\n");
    scanf("%d",&firstv);
    printf("Please enetr second number\n");
    scanf("%d",&secv);
    if (op=='+')
    {
        z= add(firstv,secv);
        printf("The addtion of %d and %d is %d\n",firstv,secv,z);
    }
    if (op=='-')
    {
        z=sub(firstv,secv);
        printf("The substration of %d and %d is %d\n",firstv,secv,z);
    }
    if (op=='*')
    {
        z= multi(firstv,secv);
        printf("the multiplication of %d and %d is %d\n",firstv,secv,z);
    }
    if(op=='/')
    {
        z=divi(firstv,secv);
        printf("The division of %d and %d is %d",firstv,secv,z);
}
    _getch();
}



我修复了赋值/比较问题并将比较改为char而不是字符串(编译器在比较中不会用字符串编译它) 。

试试看看会发生什么。


I fixed the assignment/comparison problem and changed the comparisons to be char not string (the compiler wouldn''t compile it with strings in comparisons).
Try this and see what happens.


第一眼看,你忘记了 {}



For the first look, you forgot about { and }

if (condition statement)
{
//code block to execute if condition is true
//line 2
}
else
{
//code block to execute if condition is false
//line 2
}





你不需要 {} 如果执行sin gle line



更多关于:如果 [ ^ ]。



You don''t need { and } if you execute single line

More about: if[^].


这篇关于为什么条件没有在单一陈述上停止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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