#include <stdio.h>
#include <windows.h>
#define java2 "5"
#define java3 "five"
#define java4 "Five"
#define java5 "FIVE"
main(void)
{
printf("Few question to cheer you up \n\n\n");
printf("How many class are there in Java\n");
scanf("%s",class);
if(class==java2 || class==java3 || class==java4 || class==java5 ){
goto Okay;
}
else{
printf("incorrect answer \n\n");
}
system("pause");
exit(0);
Okay:
{
printf("Welcome");
system("pause");
}
return 0;
}
当我正确回答问题时,它将打印错误答案并退出 如果我改用它;
if(class=5 || class=five || class=Five || class==FIVE ){
goto Okay;
}
它将打印编译器错误&#34; [错误]五未声明(首次使用此功能)&#34;
请帮帮我,我上周刚开始写C语言。
答案 0 :(得分:3)
您已定义
#define java5 "FIVE"
换句话说,您已将其定义为字符串。但是为了比较字符串,您需要使用strcmp()
而不是==
,例如
if ( (strcmp(class, java5) == 0) || (strcmp(class, java4) == 0) ||... ){
....
}
答案 1 :(得分:2)
==
符号不会比较字符串。使用:
if (strcmp(class, java3)==0 || ...
答案 2 :(得分:2)
您的代码存在一些错误:从不使用goto
;不要使用C
或C++
个关键字作为变量名称,例如class
;无需在同一时间使用exit(0)
和return 0
#include <stdio.h>
#include <string.h>
#define java2 "5"
#define java3 "five"
#define java4 "Five"
#define java5 "FIVE"
int main()
{
char input[10];
printf("Few question to cheer you up \n\n\n");
printf("How many class are there in Java\n");
scanf("%s", input);
if ( (strcmp(input, java2) == 0) || (strcmp(input, java3) == 0) || (strcmp(input, java4) == 0) || (strcmp(input, java5) == 0) )
{
printf("Welcome. \n\n");
}
else
{
printf("Incorrect answer! \n\n");
}
system("pause");
return 0;
}
希望它有所帮助。
答案 3 :(得分:0)
而不是将charecter数组与==使用strcmp函数进行比较 也不要使用goto关键字,这是一个糟糕的编码习惯,而不是使用标志Like int flag = 0; 如果(条件) { 标志= 1; } 如果(标志== 1) { }