C中的If/Else语句不起作用
我正在为班级做一个基础的计算机科学项目,我被困在这个问题上了。我有Java方面的经验,但我刚刚开始学习C语言。它似乎只是跳过了我的if/else语句,好像它不在那里,有人能帮我理解吗C中的If/Else语句不起作用,c,if-statement,C,If Statement,我正在为班级做一个基础的计算机科学项目,我被困在这个问题上了。我有Java方面的经验,但我刚刚开始学习C语言。它似乎只是跳过了我的if/else语句,好像它不在那里,有人能帮我理解吗 #include <stdio.h> int main() { //Systems Programming: Project3 - Justus Milhon //Requests User Input printf("Enter temparature
#include <stdio.h>
int main()
{
//Systems Programming: Project3 - Justus Milhon
//Requests User Input
printf("Enter temparature in Farenheit (int up to 3 digits): ");
//Names and scans in Farenheit value
float fare;
scanf("%f", &fare);
//Declares and calculates Celcius value
float celc;
celc = 5.0 / 9.0 * ( fare - 32 );
//Determines approptiate description (if/else system)
char desc[50] = "if statement is not running :( ";
if(fare == -40){
char desc[50] = "Ouch! Cold either way!!";
}
else if(fare == 32){
char desc[50] = "Freezing point of water";
}
else if(fare == 70){
char desc[50] = "Room temperature";
}
else if(fare == 99){
char desc[50] = "Average body temperature";
}
else if(fare == 212){
char desc[50] = "Boiling point of water";
}
else{
char desc[50] = "final else stetement is being used :(";
}
//Prints output
printf("Farenheit Celsius Description\n----------
---------- ----------\n%.0f %.3f
%s\n", fare, celc, desc);
return 0;
}
这:
打开一个新的作用域,其中包含一个名为desc的新局部变量,该变量会将周围作用域中的变量隐藏起来。此变量初始化为字符串,然后在范围退出时丢弃。父作用域中同名的变量永远不会被触及
你需要:
#include <string.h>
这使用标准strcpy函数将新字符串写入父范围中的单个desc变量
另外,您也从来没有看过celc,这在计算后看起来很混乱,对浮点进行精确比较是一个坏主意。您的代码只是用相同的名称定义了不同的数组,并且范围仅限于定义它们的块。您应该使用指针并将其指定给适当的字符串:
#include <stdio.h>
int main(void) {
//Systems Programming: Project3 - Justus Milhon
//Requests User Input
printf("Enter temperature in Farenheit (int up to 3 digits): ");
//Names and scans in Farenheit value
float fare;
scanf("%f", &fare);
//Declares and calculates Celcius value
float celc;
celc = 5.0 * (fare - 32) / 9.0;
//Determines appropriate description (if/else system)
const char *char = "if statement is not running :( ";
if (fare == -40) {
desc = "Ouch! Cold either way!!";
} else
if (fare == 32) {
desc = "Freezing point of water";
} else
if (fare == 70) {
desc = "Room temperature";
} else
if (fare == 99) {
desc = "Average body temperature";
} else
if (fare == 212) {
desc = "Boiling point of water";
} else {
desc = "final else statement is being used :(";
}
//Prints output
printf("Farenheit Celsius Description\n"
"--------- ------- -----------\n"
"%9.0f %7.3f %s\n",
fare, celc, desc);
return 0;
}
但是请注意,浮点运算不是以绝对精度执行的:5.0/9.0是一个近似值,因此计算到celc中的值可能与积分结果略有不同。像我这样更改操作数的顺序可能会提高某些计算结果的准确性 这不是比较浮动的方式。提示:当你是一门语言的初学者时:语言永远不会被打破。坏东西最有可能是您在代码中加入的隐式假设。在你的例子中:假设你可以像处理整数一样处理浮点数。{char desc[50]=哎哟!不管哪种方式都很冷!!;}:这个desc是这个块中的局部范围变量。你已经有了答案,只是一个提示:不要标题[一些基本语言功能..]不起作用。它正在工作,而不是你的代码。有了这样一个标题,你只会吸引反对票。你能不能也涵盖我认为的第二个问题:浮动和==?也就是说,change==->Celc是最终输出的一部分。我更改了它们以符合您的建议,现在我得到了内置函数“strcpy”的错误不兼容隐式声明:@Yunnosch问题的一部分,该函数必须包含==vs@JustusMilhon。
if(fare == -40){
strcpy(desc, "Ouch! Cold either way!!");
}
#include <stdio.h>
int main(void) {
//Systems Programming: Project3 - Justus Milhon
//Requests User Input
printf("Enter temperature in Farenheit (int up to 3 digits): ");
//Names and scans in Farenheit value
float fare;
scanf("%f", &fare);
//Declares and calculates Celcius value
float celc;
celc = 5.0 * (fare - 32) / 9.0;
//Determines appropriate description (if/else system)
const char *char = "if statement is not running :( ";
if (fare == -40) {
desc = "Ouch! Cold either way!!";
} else
if (fare == 32) {
desc = "Freezing point of water";
} else
if (fare == 70) {
desc = "Room temperature";
} else
if (fare == 99) {
desc = "Average body temperature";
} else
if (fare == 212) {
desc = "Boiling point of water";
} else {
desc = "final else statement is being used :(";
}
//Prints output
printf("Farenheit Celsius Description\n"
"--------- ------- -----------\n"
"%9.0f %7.3f %s\n",
fare, celc, desc);
return 0;
}