我无法让if语句正确执行

时间:2019-03-24 19:39:15

标签: 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;

        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*/

2 个答案:

答案 0 :(得分:0)

您在for循环中使用相同的变量d并接受输入,这导致了问题。使用以下代码:-

SELECT A.FCLTY_NO, A.CSR_MN, A.CP1KVINS,
LEAD(A.CP1KVINS, 1, 0) OVER (ORDER BY A.CP1KVINS) AS NEXT_CPIKVIN,
LEAD(A.CP1KVINS, 1, 0) OVER (ORDER BY A.CP1KVINS) - A.CP1KVINS AS 
DIFF_CP1KVIN
FROM USER9.WPO_VIN_VST_CLM_CY17_TO_DATE A,
     DRBA.ADA_DEALER_T                  E
WHERE A.FCLTY_NO = E.DEALER_NO
AND E.CHANNEL_ELIG_CD = 'N'
AND E.APPOINT_DT < '01JAN2018'
AND E.TERM_DT = '31DEC9999'
ORDER BY A.FCLTY_NO, A.CSR_MN

FCLTY_NO    CSR_MN  CP1KVINS    CP1KVINS_DIFF
01016       1/01/2017     1,060     0
01016       2/01/2017     1,000        -60
01016       3/01/2017     1,070         70
01016       4/01/2017     1,070         0
01016       5/01/2017     1,060        -10
01016       6/01/2017     1,070         10
01016       7/01/2017     1,060        -10
01016       8/01/2017     1,050        -10
01016       9/01/2017     1,060         10
01016       10/01/2017    1,040        -20

答案 1 :(得分:0)

似乎您的程序逻辑上有一些错误。我会重写

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*/

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);

对于其他循环也可以这样做。

这个想法是要求至少输入一次,并检查输入的值对您的情况是否有效,如果无效,请再次输入。