c - 为什么我的 C 程序会跳过 if 语句?
问题描述
我是一个新的编码员,不知道我在做什么,请帮忙!
代码正在读取并接受输入,直到它到达 scanf(" %c", &i); 然后它跳到 Amount 打印似乎忽略了我的 if 语句。我使用 scanF 有什么问题吗?
这是代码:
#include <stdio.h>
int main(){
printf("BANK ACCOUNT PROGRAM!\n--------------------\n");
char W,F,A,i,D;
int t=1;
double b,d;
while (t=1){
printf("Enter the old balance:\n");
scanf(" %lf", &b);
printf(" %lf", b);
if(b>=0)
{
printf("Enter the transactions now!\n Enter an F for the transaction type when you are finished.\n");
printf("Transaction Type (D=deposit, W=withdrawal, F=finished):\n");
scanf(" %c", &i);
if(i=F){
printf("Your ending balance is");
printf(" %lf", b);
printf("\n Program is ending!");
return 0;
}
if(i=W){
printf("Amount:");
scanf(" %f", &d);
b= b-d;
printf(" %f",b);}
}
if(b<0);
{
printf("The balance must be maintained above zero!\n");
}
}
return 0;
}
解决方案
因为你的比较是错误的
if(i=F){ // this is assignment, not comparison
应该
if(i=='F'){ // note also it's comparison to character 'F'
推荐阅读
- databricks - 将数据写入 Azure 数据块中的 Delta Lake 时出现问题(检测到不兼容的格式)
- angularjs - 表单 $dirty 在 typeahead 选择时为假
- typescript - 是否可以将 Lerna 用于在 api 边界上使用 rxjs(例如)的多个打字稿项目?
- python - 使用 TensorFlow 的对象检测 API
- javascript - 如何构建对 API 的异步调用?
- printing - 如何在python3中创建打印对话框
- swift - tableView 内的 collectionView 在滚动时返回到第一个索引
- javascript - 将构造函数中的默认值设置为未定义是一种好习惯吗?
- python - 如何通过标记现有数据框的内容来创建新的数据框?
- reactjs - Highcharts 系列标签