仍然是初學者和一年級學生。我正在嘗試創建一個簡單的問答游戲作為練習,但是當我嘗試創建一個計數器作為剩余機會時,每次用戶回答錯誤時它都會減少。問題是當生命計數器達到 0 時回圈不會退出,而是變為負值并且不會退出回圈。我仍然不知道我是否正確使用了while回圈。我希望得到一個簡單的答案。謝謝!
#include<stdio.h>
int main (void) {
//quiz variables
int gamestart,ingame=1;
int scr1,scr2,scr3,scr4,scr5;
int ans1,ans2,ans3,ans4,ans5;
char name[25];
int total=0;
//start of quiz
printf("--------Hello welcome to the quiz game!--------\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d",&gamestart);
//quiz code
int lives=3;
while (ingame==1 && lives>0 && lives<=3){
if (gamestart==1){
printf("Enter your first name: ");
scanf("%s",&name);
printf("Hello %s! Let's start the game...\n",name);
printf("First Question: What does “www” stand for in a website browser?\n");
printf("1. Whole Wide World 2. Whole Wide Web\n");
printf("3. World Wide Web 4. World Wide Whole\n");
printf("Enter the number of your answer: ");
scanf("%d",&ans1);
switch(ans1){
case 1:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
case 2:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
case 3:
printf("Correct Answer! You gain 5 points!");
scr1=5;
break;
default:
printf("Wrong Answer. You gain 0 points");
scr1=0;
break;
}
total = scr1;
if (scr1 ==0 )
lives--;
printf("\nLives left: %d",lives);
printf("\nYour total score this round is %d ",total);
printf("\n");
printf("\nNext Question...\n");
printf("What is \"cynophobia\"?\n");
printf("1. Fear of dogs 2. Fear of cats\n");
printf("3. Fear of cyanide 4. Fear of the color blue\n");
printf("Enter the number of your answer: ");
scanf("%d",&ans2);
switch(ans2){
case 1:
printf("Correct Answer! You gain 5 points!");
scr2=5;
break;
case 2:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
case 3:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
default:
printf("Wrong Answer. You gain 0 points");
scr2=0;
break;
}
total =scr2;
if (scr2==0)
lives--;
printf("\nLives left: %d",lives);
printf("\nYour total score this round is %d ",total);
ingame=0;
} else if (gamestart == 0) {
ingame=0;
}else{
printf("Invalid Keyword\n");
printf("----Press 1 to start and 0 to quit----\n");
scanf("%d",&gamestart);
}
printf("\n\n\nThank you!");
return 0;
}
uj5u.com熱心網友回復:
首先,我看不出您使用所有這些switch陳述句的原因,只需使用if. 還要格式化您的代碼,以便我們可以閱讀它。
這是我對您的代碼的看法:
#include <stdio.h>
int main(void)
{
// quiz variables
int gamestart, ingame = 1;
int total = 0;
int ans;
char name[25];
// start of quiz
printf("--------Hello welcome to the quiz game!--------\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d", &gamestart);
// quiz code
int lives = 3;
while (ingame == 1 && lives > 0)
{
if (gamestart == 1)
{
printf("Enter your first name: ");
scanf("%s", &name);
printf("Hello %s! Let's start the game...\n", name);
printf("First Question: What does “www” stand for in a website browser?\n");
printf("1. Whole Wide World 2. Whole Wide Web\n");
printf("3. World Wide Web 4. World Wide Whole\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 3)
{
printf("Correct Answer! you gain 5 points!");
total = 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("What is \"cynophobia\"?\n");
printf("1. Fear of dogs 2. Fear of cats\n");
printf("3. Fear of cyanide 4. Fear of the color blue\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 1)
{
printf("Correct Answer! you gain 5 points!");
total = 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("Who named the Pacific Ocean?\n");
printf("1. Ferdinand Magellan 2. Christopher Colombus\n");
printf("3. Miguel Lopez de Legazpi 4. Antonio Pigafetta\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 1)
{
printf("Correct Answer! you gain 5 points!");
total = 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\nNext Question...\n");
printf("What was the first soft drink in space?\n");
printf("1. Fanta 2. Pepsi\n");
printf("3. Coca-Cola 4. Dr. Pepper\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 3)
{
printf("Correct Answer! you gain 5 points!");
total = 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\nYour total score this round is %d ", total);
printf("\n");
printf("\n");
printf("Next Question...");
printf("Which country invented ice cream?\n");
printf("1. USA 2. China \n");
printf("3. Spain 4. Turkey\n");
printf("Enter the number of your answer: ");
scanf("%d", &ans);
if (ans == 2)
{
printf("Correct Answer! you gain 5 points!");
total = 5;
}
else
{
printf("Wrong Answer! You gain 0 points!");
lives--;
if (lives == 0)
{
printf("You lost");
break; // or do something else
}
}
printf("\nLives left: %d", lives);
printf("\n");
printf("\n");
printf("\nYour total score in this game is %d \n", total);
switch (total)
{
case 0:
printf("Too bad!");
break;
case 5:
printf("Try harder!");
break;
case 10:
printf("Fair enough");
break;
case 15:
printf("Pretty good!");
break;
default:
printf("Excellent! Genius!");
break;
}
ingame = 0;
}
else if (gamestart == 0)
{
ingame = 0;
}
else
{
printf("Invalid Keyword");
printf("\n");
printf("----Press 1 to start the game and 0 to quit----\n");
scanf("%d", &gamestart);
}
}
printf("\n");
printf("\n");
printf("\nThank you!");
return 0;
}
我沒有添加換行符。此外,當實時計數為 0 時,我確實中斷了。你可以用它做任何你想做的事情。
uj5u.com熱心網友回復:
據我了解,您的“計數器”是變數生命,在您的問題中,您說它可以變為負數。這是因為您忘記檢查是否生活 == 0,因此使其未使用。在每個問題之后,您應該檢查是否 living == 0 并添加一些細節(我將其留在這里以供您發揮創造力)。
編輯:雖然這取決于你的喜好,但我建議盡可能縮短代碼(這樣當它出現錯誤時它不會變得混亂),然后在它作業后添加細節。
uj5u.com熱心網友回復:
這很粗略,但它試圖演示如何撰寫更少的代碼。這個版本仍然有你原來的 5 個問答。它仍然“邊緣粗糙”,但我希望它為您提供一些見解,以撰寫更少但更有效的代碼......
#include <stdio.h>
int qa( int ans, char *str ) {
int guess = 0;
puts( str );
printf("Your answer: ");
scanf("%d", &guess);
printf("Correct answer is %d! ", ans);
printf("You gain %d points!\n\n", (guess == ans) * 5 );
return guess == ans;
}
int main() {
int res = 0, total = 0, lives = 3;
for( int qno = 1; qno <= 5 && lives > 0; qno ) {
if(qno == 1)
res = qa( 3,
"What does 'WWW' stand for in a website browser?\n"
"1. Whole Wide World 2. Whole Wide Web\n"
"3. World Wide Web 4. World Wide Whole\n" );
else if(qno == 2)
res = qa( 1,
"What is \"cynophobia\"?\n"
"1. Fear of dogs 2. Fear of cats\n"
"3. Fear of cyanide 4. Fear of the color blue\n" );
else if(qno == 3)
res = qa( 1,
"Who named the Pacific Ocean?\n"
"1. Ferdinand Magellan 2. Christopher Colombus\n"
"3. Miguel Lopez de Legazpi 4. Antonio Pigafetta\n" );
else if(qno == 4)
res = qa( 3,
"What was the first soft drink in space?\n"
"1. Fanta 2. Pepsi\n"
"3. Coca-Cola 4. Dr. Pepper\n" );
else if(qno == 5)
res = qa( 2,
"Which country invented ice cream?\n"
"1. USA 2. China \n"
"3. Spain 4. Turkey\n" );
if( res == 1 )
total = 5;
else
lives -= 1;
printf("Your score is %d with %d lives left\n\n", total, lives);
if(lives == 0)
puts("Lives gone. You lost.");
}
char *cmnt = "Excellent! Genius!";
switch (total) {
case 0: cmnt = "Too bad!"; break;
case 5: cmnt = "Try harder!"; break;
case 10: cmnt = "Fair enough"; break;
case 15: cmnt = "Pretty good!"; break;
}
puts(cmnt);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/512700.html
標籤:C柜台
上一篇:傳遞給if陳述句的函式
