[英]Why is my code returning only one function?
我是编程的新手(以C开头),并尝试通过构建计算器来练习函数。 但是,即使没有调用其If-Statement,它也只会返回相同的函数。 这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int result;
int multiplication(int num1, int num2){
result = num1 * num2;
return result;
};
int addition(int num1, int num2){
result = num1 + num2;
return result;
};
int substraction(int num1, int num2){
result = num1 - num2;
return result;
};
int main(){
int num1;
int num2;
char Math;
printf("Do you want to do a Multiplication or an Addition, or a Substraction: ");
scanf("%c", &Math);
printf("Now give me a Number: ");
scanf("%d",&num1);
printf("Now give me another Number: ");
scanf("%d",&num2);
if(Math = 'M' || 'm'){
printf("Your Mulitplication came out to %d", multiplication(num1,num2));
}
else if(Math = 'A' || 'a'){
printf("Your Addition came out to %d", addition(num1, num2));
}
else if(Math = 'S' || 's'){
printf("Your Substraction came out to %d", substraction(num1, num2));
}
else{
printf("Your Input was wrong");
};
return 0;
}
我真的很感激我能得到的每一个建议!
这里
if(Math = 'M' || 'm')
需要更改为
if((Math == 'M') || (Math == 'm'))
因为按原样, 'M' || 'm'
'M' || 'm'
变成1
,然后将其分配给Math
,并返回结果,这意味着采用if
。 进行此更改后,您实际上是将Math
与'M'
进行比较,如果不相等,则将其与'm'
。
同样,对于else if(Math = 'A' || 'a')
等。
注意 (=)与(==)不同
=是赋值运算符,它会在您将代码“ M”分配给Math的代码中赋值,逻辑运算符返回0或1。在这种情况下,它返回1,因为1和if的结果在内部返回了控制流。获得乘法作为输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.