C中的变更计算程序

时间:2016-05-25 03:42:13

标签: c loops calculator

我刚刚开始学习编程,我正在尝试编写一个程序,该程序将显示任何给定数量的变化需要多少种货币面额。 我在日本学习,所以货币是日元,但我认为基本代码是普遍的。我在网上看过其他类似的节目,但是我的一些额外的功能可能是我的问题的原因,但我不确定。

首先,用户输入寄存器中是否有两千日元钞票。 (因为这些法案并不常见)。 然后输入到期总金额。然后输入支付的金额。然后计算变化和每种面额的多少,然后显示它。

但是,在您输入支付金额后,光标将转到下一行并无限期地坐在那里。我不知道是什么原因造成的。我唯一的猜测就是它在某个地方被卡住了。

有没有人看到问题? (*我将文本切换为英文)

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

int main(void)
{
    //入力
    int aru;

    printf("Are there 2-thousand yen bills in the register?\n 1.) Yes\n 2.) No\n "); //レジに2千円札が入ってますか?\n 1.) 入ってます\n 2.)入ってません
    scanf("%d", &aru);

    int total, paid;

    printf("Enter Total Price  ");//お会計を記入して下さい。 
    scanf("%d", &total);
    printf("Enter Amount Paid ");//お客さんのお支払った合計を記入してください。
    scanf("%d", &paid);

    //計算
    if (paid < total)
    {
        printf("Insufficiant amount paid\n");//お金を十分にもらいませんでした
    }
    if (paid > total)
    {
    int change = paid - total;
    int ichi = 0, go = 0, ju = 0, goju = 0;
    int hyaku = 0, gohyaku = 0, sen = 0, nisen = 0, gosen = 0;

    while (change > 5000)
    {
        change - 5000;
        gosen++;
    }
    while (change > 2000)
    {
        if (aru == 1)
        {
            change - 2000;
            nisen++;
        }
        else
        {
            nisen = 0; //skips calculating 2000 yen bills if answer was 'no'
        }
    }
    while (change > 1000)
    {
        change - 1000;
        sen++;
    }
    while (change > 500)
    {
        change - 500;
        gohyaku++;
    }
    while (change > 100)
    {
        change - 100;
        hyaku++;
    }
    while (change > 50)
    {
        change - 50;
        goju++;
    }
    while (change > 10)
    {
        change - 10;
        ju++;
    }
    while (change > 1)
    {
        change - 1;
        ichi++;
    }

    //出力
    printf(" %d \n", gosen);
    printf(" %d \n", nisen);
    printf(" %d \n", sen);
    printf(" %d \n", gohyaku);
    printf(" %d \n", hyaku);
    printf(" %d \n", goju);
    printf(" %d \n", ju);
    printf(" %d \n", go);
    printf(" %d \n", ichi);
}
    return 0;
} 

1 个答案:

答案 0 :(得分:2)

while

您可能需要int i = 0; while (i < gamePlays) { // If, else if, and else statements to make the game possible. // Here is an example of part of the code: if (cpuChoice.equals ("rock")) { if (userChoice.equals ("scissors")) { System.out.println("Rock beats scissors, computer wins."); i++; // increment only when there is result } else if (userChoice.equals ("paper")) { System.out.println("Paper beats rock, computer wins."); i++; // increment only when there is result } else { System.out.println("You both chose rock! It's a tie."); // don't increment if no result } // The rest would else if cpuChoice.equals "paper" and else. } 而不是while (change > 5000) //This is an infinite loop { change - 5000; //no change is made to change gosen++; } 这是您代码中的几个位置。 change -= 5000;相当于

change - 5000;