if语句不能立即工作端程序 [英] if statement not working ends program immediately
本文介绍了if语句不能立即工作端程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
林作出一个气象预报员code,但是当我问温度就结束程序。这里的code。
的#include<&stdio.h中GT;
INT主要(无效)
{
int类型的;
INT B:
INT℃;
INT D组;
的printf(你叫什么名字\\ n吗?);
scanf函数(%S,和b);
的printf(嗨%S \\ n,和b);
睡眠(1);
的printf(IM自己的天气预报员\\ n);
睡眠(1);
的printf(不是很大\\ n!);
睡眠(1);
的printf(请输入一个数字在\\ n);
睡眠(1);
scanf函数(%d个,&安培; A);
//检查出你写的数
如果(一个3;)
{
的printf(它Beatiful日\\ n);
的printf(%C \\ N,2);
}
如果(一个→3)
{
的printf(:(\\ n);
}
如果(A == 3)
{
的printf(它下雪\\ n!);
的printf(%C \\ N,3);
睡眠(5);
的printf(你想知道的温度图\\ n吗?);
scanf函数(%S,和C);
}
如果(C =='是') 的printf(20F \\ n); 返回0;}
我觉得对,如果搞乱(C =='是')结果
如何将我使其打印出来的20架F?
谢谢你帮我出了新的code是正下方随意使用它时,你想,但给予信贷给我。
的#include<&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&;&string.h中GT;
INT主要(无效)
{
诠释A,B;
炭C [5];
CHAR D [30];
的printf(你叫什么名字\\ n吗?);
scanf函数(%S,D);
的printf(嗨%S \\ n,D);
睡眠(1);
的printf(IM自己的天气预报员\\ n);
睡眠(1);
的printf(不是很大\\ n!);
睡眠(1);
的printf(请输入一个数字在\\ n);
scanf函数(%d个,&安培; A);
//检查出你写的数
如果(一个3;)
{
的printf(它Beatiful日\\ n);
的printf(%C \\ N,2);
}
如果(一个→3)
{
的printf(它阴天);
的printf(:(\\ n);
}
如果(A == 3)
{
的printf(它下雪\\ n!);
的printf(%C \\ N,3);
睡眠(5);
的printf(你想知道的温度图\\ n吗?);
scanf函数(%S,C);
} 如果(STRCMP(是,C)== 0)
的printf(20F \\ n); 返回0;}
解决方案
的问题在于,你读的字符串与此行一个int:
scanf函数(%S,和C);
试试这个,其中%d个
是显示输入的是int类型的。
scanf函数(%d个,和C);
Im making a weather man code but when i ask for the temperature it ends the program. here's the code.
#include <stdio.h>
int main(void)
{
int a;
int b;
int c;
int d;
printf("What Is Your Name?\n");
scanf("%s",&b);
printf("Hi %s\n", &b);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
sleep(1);
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s", &c);
}
if(c=='yes')
printf("20F\n");
return 0;
}
I think its messing up on if(c=='yes')
how would i make it to print out the 20 F?
[EDIT]Thanks for helping me out the new code is right below feel free to use it when you want but give credit to me.
#include <stdio.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
int a,b;
char c[5];
char d[30];
printf("What Is Your Name?\n");
scanf("%s",d);
printf("Hi %s\n", d);
sleep(1);
printf("Im Your Own Weather Man\n");
sleep(1);
printf("Isn't That Great!\n");
sleep(1);
printf("Please Type A Number In\n");
scanf("%d",&a);
//checking out the number you wrote
if(a<3)
{
printf("Its A Beatiful Day\n");
printf("%c\n",2);
}
if(a>3)
{
printf("Its A Cloudy Day");
printf(":(\n");
}
if(a==3)
{
printf("Its Snowing!\n");
printf("%c\n",3);
sleep(5);
printf("Would you like to know the temprature?\n");
scanf("%s",c);
}
if(strcmp("yes", c) == 0)
printf("20F\n");
return 0;
}
解决方案
The problem lies in that you're reading a string into an int with this line:
scanf("%s", &c);
Try this instead, where the %d
is showing that the input is of type int.
scanf("%d", &c);
这篇关于if语句不能立即工作端程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文