gpt4 book ai didi

C公式无法得到正确的值

转载 作者:行者123 更新时间:2023-11-30 20:35:41 25 4
gpt4 key购买 nike

我需要有关以下代码的公式的帮助:

#include <stdio.h>
void main()
{
int MAGNUM, AK47, Knife, Axe, total;
char choice, choice2, confirm;

MAGNUM = 75;
AK47 = 150;
Knife = 40;
Axe = 20;

printf("Welcome to weapon store.\n");
printf("Over here, we sell cheap weaponry you may be interested in.\n");
printf("Currently, this is what we have for sale:\n\n\n");
printf("(A) .44 MAGNUM\n");
printf("(B) AK-47\n");
printf("(C) Laredo Bowie knife\n");
printf("(D) Tomahawk axe\n\n\n");
printf("Pick one to buy, just type the letter in caps of the item and press enter.\n");
printf("ITEM SELECTED:");
scanf("%c", &choice); //First input//

if (choice == 'A')
{
printf("That will be $%d.\n", MAGNUM);
}

if (choice == 'B')
{
printf("That will be $%d.\n", AK47);
}
if (choice == 'C')
{
printf("That will be $%d.\n", Knife);
}
if (choice == 'D')
{
printf("That will be $%d.\n", Axe);
}
printf("Do you want to buy anything else?<Y//N>\n"); //Second input//
scanf(" %c", &choice2);

if (choice2 == 'Y')
{
printf("What else do you want to buy:"); //Third input//
scanf(" %c", &confirm);
if (confirm == 'A')
{
printf("That will be $%d.\n ", MAGNUM);
}
else if (confirm == 'B')
{
printf("That will be $%d.\n", AK47);
}
else if (confirm == 'C')
{
printf("That will be $%d.\n", Knife);
}
else if (confirm == 'D')
{
printf("That will be $%d.\n", Axe);
}

total = choice + confirm; //Need help with formula here. total equals first input + third input//

printf("Total cost is $%d. Thank you.\n", total);
}
else
{
printf("Thank you for shopping with us.\n");
}
}

调试时,它不会显示输入 1 和 3 加在一起时的正确值。

例如,如果我分别为输入 1 和 3 选择 D,然后再次选择 D,我总共应该得到 40 美元,但我却得到了不同的值 136 美元。

可能是我目前缺乏 C 知识,因为我才刚开始 2 天。这段代码只是我对 if else 语句等基础知识的测试,所以如果代码中的内容可能冒犯任何人,我深表歉意。

最佳答案

您添加的是字符值而不是价格。

68 是解释为整数的“D”。 68+68=136。

我会在您的 if 语句中添加价格,例如

if (choice == 'D')
{
printf("That will be $%d.\n", Axe);
total=total+Axe;
}

关于C公式无法得到正确的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38380317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com