gpt4 book ai didi

c - 我的代码有什么问题吗? C

转载 作者:行者123 更新时间:2023-11-30 19:09:24 25 4
gpt4 key购买 nike

我尝试制作一个问答游戏。由于某种原因,在第一个答案之后,整个代码会自行打印,而当时只应该打印一行。

int main()

{

    char answer;
int score = 16;


printf("Hello and welcome to Chen's trivia!\n");
printf("Press ENTER to continue!\n");
getchar();

printf("Ready? Let's begin!\n");
printf("Press ENTER to continue!\n");
getchar();


printf(" Who is the president of the united states?\n");
printf("Barack Obama, Donald Trump, George w. Bush\n");
scanf(" %c", &answer);
if(answer == 'Trump' || 'trump'){
printf("You are correct\n");
score*=2;
printf("your score is: %d \n", score);
}else{
score = (score / 2);
printf("Wrong answer!\n");
printf("score: %d \n", score);
}


printf("What superhero shoots web out of his arms?\n");
printf("A.Batman, B.Spiderman, C.Captain America, D.Superman\n");
scanf(" %c", &answer);
if(answer == 'B' || 'b'){
printf("That's right, Hero!\n");
score*=2;
printf("Youre score is: %d \n", score);
}else{
score = (score / 2);
printf("sorry, wrong answer!\n");
printf("your score is! %d\n", score);
}

printf("Who is the the main character in 'The Matrix'? \n");
scanf(" %c", &answer);
if(answer == 'neo' || 'NEO'){
score*=2;
printf("That's right!\n");
printf("Your score is %d\n", score);
}else{
score = (score / 2);
printf("Sorry, Wrong answer!\n");
}

printf("What is the capital of Israel?");
scanf(" %c", &answer);
if(answer== ('jerusalem') || ('Jerusalem') ){
score*=2;
printf("That's right!");
printf("Your score is:%d", score);
}else{
score = (score / 2);
printf("Sorry, wrong answer!");
printf("Your score is now:%d", score);
}


return 0;

}

有什么想法吗? :(顺便说一句,我的代码中出现此错误: block

警告:字符常量对于其类型来说太长警告:多字符字符常量 wmultichar

总共收到 6 条警告。

最佳答案

char answer;
/* ... */
scanf(" %c", &answer);

if(answer == 'Trump' || 'trump'){ /* et cetera */

您混淆了字符和字符串。字符串是一个数组人物。 %c 是读取单个字符的scanf代码;为了您将使用 %s 的字符串。字 rune 字用单引号编写,例如 'a'。字符串文字使用双引号:"A string"。您可以使用== 运算符来比较字符,但要比较字符串,您应该使用 strcmp() 函数。

您对 || 运算符的使用并不符合您的预期。您需要编写两个单独的测试:

if ( (answer == 'A') || (answer == 'a')) { /* etc */

关于c - 我的代码有什么问题吗? C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43284880/

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