C 我能';无法正确执行if语句
我似乎无法在程序的存款和取款部分正确执行if语句。当数字为负数时,它将正确执行,但当数字为正数时,它将显示错误消息,然后结束循环并移动到取款C 我能';无法正确执行if语句,c,C,我似乎无法在程序的存款和取款部分正确执行if语句。当数字为负数时,它将正确执行,但当数字为正数时,它将显示错误消息,然后结束循环并移动到取款 #include <stdio.h> int main (void) { int deposits = 0, totaldeposits = 0, deposited = 0, totalwithdrawals = 0, withdrawals, d = 0, w = 0; float balance = 0, b;
#include <stdio.h>
int main (void)
{
int deposits = 0, totaldeposits = 0, deposited = 0, totalwithdrawals = 0, withdrawals, d = 0, w = 0;
float balance = 0, b;
printf ("Welcome to the computer banking system\n\n");
for ( b = 0; b <= balance; ++b )
{
printf ("Enter your current balance in dollars and cents:");
scanf ("%f", &b);
balance = balance + b;
if ( balance <= -1 )
{
printf ("***Beginning balance must be at least zero, please re-enter.\n\n");
balance = balance - b, --b;
}/*end of if statement*/
}/*end of for loop*/
for ( d = 0; d <= deposits; ++d )
{
printf ("\nEnter the number of deposits (0-5):");
scanf ("%i", &d);
deposits = deposits + d;
if ( deposits > 5 || deposits < 0 )
{
printf ("*** Invalid number of deposits, please re-enter.");
deposits = deposits - d;
}/*end of if statement for deposits*/
}/*end of for loop for deposits*/
for ( w <= 0; w <= withdrawals; ++w)
{
printf ("\n\nEnter the number of withdrawals (0-5):");
scanf ("%i", &w);
withdrawals = withdrawals + w, ++w;
if ( withdrawals > 5 || withdrawals < 0 )
{
printf ("***Invalid number of withdrawals, please re-enter.");
withdrawals = withdrawals - w, --w;
}/*end of withdrawals if*/
}/*end of withdrawals for loop*/
getchar;
return 0;
} /*End main*/
#包括
内部主(空)
{
整数存款=0,总存款=0,存款=0,总取款=0,取款,d=0,w=0;
浮动平衡=0,b;
printf(“欢迎使用计算机银行系统\n\n”);
对于(b=0;b您在for循环和获取输入中使用了相同的变量d。这导致了问题。请使用以下代码:-
int dep;
for ( d = 0; d <= deposits; ++d )
{
printf ("\nEnter the number of deposits (0-5):");
scanf ("%i", &dep);
deposits = deposits + dep;
if ( deposits > 5 || deposits < 0 )
{
printf ("*** Invalid number of deposits, please re-enter.");
deposits = deposits - dep;
}/*end of if statement for deposits*/
else break;
}/*end o
intdep;
对于(d=0;d 5 | |存款<0)
{
printf(“***存款数量无效,请重新输入。”);
存款=存款-dep;
}/*存款的if结单*/
否则就断了;
}/*结束o
您的程序逻辑似乎有错误。我会重写
for ( d = 0; d <= deposits; ++d )
{
printf ("\nEnter the number of deposits (0-5):");
scanf ("%i", &d);
deposits = deposits + d;
if ( deposits > 5 || deposits < 0 )
{
printf ("*** Invalid number of deposits, please re-enter.");
deposits = deposits - d;
}/*end of if statement for deposits*/
}/*end of for loop for deposits*/
(d=0;d 5 | |存款<0)的
{
printf(“***存款数量无效,请重新输入。”);
存款=存款-d;
}/*存款的if结单*/
}/*存款循环结束*/
作为
do{
printf(“\n输入存款数量(0-5):”;
scanf(“%i”和存款);
如果(存款<0 | |存款>5){
printf(“***存款数量无效,请重新输入。”);
}否则{
打破
}
}而(1),;
其他循环也可以这样做
我们的想法是至少要求输入一次,然后检查输入的值是否对您的案例有效,如果无效,请再次要求输入。如果是语句,会是哪个?非常感谢您的帮助!我真的非常感谢!非常感谢您的帮助!我真的非常感谢。我不理解do循环,但它们是正确的在这之后更清楚了。再次感谢!
do {
printf ("\nEnter the number of deposits (0-5):");
scanf ("%i", &deposits);
if (deposits < 0 || deposits > 5) {
printf ("*** Invalid number of deposits, please re-enter.");
} else {
break;
}
} while (1);