gpt4 book ai didi

c - If 语句问题,不显示正确的输出

转载 作者:太空宇宙 更新时间:2023-11-04 04:30:29 24 4
gpt4 key购买 nike

我正在尝试制作一个程序,让用户可以从他们想要转换的测量列表中进行选择。
到目前为止,我的代码是:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int sv, SourceUnit, du, mb, kb, by;
printf("--------------------------------------------------0x0 Menu:------ ---------------------------------\n");
printf(" Please Select from the following options (enter number exactly when prompted for source units!\n");
printf("1: Kilo -> Mega\n2: Mega -> Kilo\n10. Bits -> Bytes\n ");
printf("--------------------------------------------------------------------------------------------------\n");

printf("Enter source value: ");
scanf("%d", &sv);

printf("Enter source unit: ");
scanf("%s", &SourceUnit);

if (SourceUnit == 1) //convert kilobytes to megabytes
{

mb = (sv / 1024);
printf("%d Kb == %d Mb\n", sv, mb);
}

else if (SourceUnit == 2) //convert megabytes to kilobytes
{
kb = (sv / 1024);
printf("%d Mb == %d Kb\n", sv, kb);
}

else if (SourceUnit == 10) //bits to bytes
{
by = (sv / 8);
printf("%d Bits == %d Bytes\n", sv, by);
}

else
{
printf("Please Choose from the menu options to convert!\n");
}

return(0);
}

它在 GCC 上编译良好。我得到的输出是:

---------------------------------------0x0 Menu:----------------------------
1. Kilo -> Mega
2. Mega -> Kilo
3. Bits -> Bytes
----------------------------------------------------------------------------
Enter Source Value: (Example I will type in 30 for the number to be converted) 30
Enter Source Unit: (Example will be 1 to convert from Kilo to Mega) 1
Please Choose from the menu options to convert!

出于某种原因,当我不希望它出现时,我的 else 语句出现了......是什么导致了这个错误?

此外,我还遇到了一些问题,我需要将 deka 转换为 dibi..具体如何完成?我知道 deka 是 10 人一组,dibi 是 16 人一组,但这就是我能读到的关于它们的所有信息..

最佳答案

您的 scanf 有错误的格式说明符。它应该是“%d”。这导致了问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int sv, SourceUnit, du, mb, kb, by;
printf("--------------------------------------------------0x0 Menu:------ ---------------------------------\n");
printf(" Please Select from the following options (enter number exactly when prompted for source units!\n");
printf("1: Kilo -> Mega\n2: Mega -> Kilo\n10. Bits -> Bytes\n ");
printf("--------------------------------------------------------------------------------------------------\n");

printf("Enter source value: ");
scanf("%d", &sv);

printf("Enter source unit: ");
scanf("%d", &SourceUnit); //This should be %d

if (SourceUnit == 1) //convert kilobytes to megabytes
{

mb = (sv / 1024);
printf("%d Kb == %d Mb\n", sv, mb);
}

else if (SourceUnit == 2) //convert megabytes to kilobytes
{
kb = (sv / 1024);
printf("%d Mb == %d Kb\n", sv, kb);
}

else if (SourceUnit == 10) //bits to bytes
{
by = (sv / 8);
printf("%d Bits == %d Bytes\n", sv, by);
}

else
{
printf("Please Choose from the menu options to convert!\n");
}

return(0);
}

关于c - If 语句问题,不显示正确的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36829385/

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