C 程序输出有问题
Having trouble with C program output
据我所知,不管任何其他问题,下面的程序都应该打印出标题和菜单选项,然后提示用户输入。
但是,它什么都不做,当我停止执行时,它会打印出菜单等,然后,由于它没有要求用户输入选项,它会重复打印 "This is not a valid option" 行。
*编辑:我已经完全删除了循环。我在程序中所拥有的只是打印标题、打印菜单、要求用户输入,在我终止之前,我仍然无法在控制台上看到任何东西。我的输入请求有问题吗?
EDIT2:绝对是 scanf,因为没有它一切正常。我 运行 带有附加功能的代码可以打印出存储在选项中的值,当我在要求用户输入之前没有将它设置为 0 时它告诉我 -1。该程序似乎是自动分配选项,而不是费心询问用户他们想要什么。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main ()
{
/*Print Title*/
printf("Maths Quiz Game \n");
printf("\n");
int i;
int rightCount = 0; //count of right answers
int wrongCount = 0; //count of wrong answers
int questions = 0; //user input for number of questions
int exit = 0; //store exit option
int option = 0; //menu option
while(exit == 0) //while loop that keeps program running until exit is chosen
{
/*Menu Options*/
printf("Please choose an option from the menu below. Enter the number of your choice. \n");
printf(" 1. Choose number of questions for this round. (Max = 5) \n");
printf(" 2. Start Quiz \n");
printf(" 3. Display total of right and wrong answers. (Only availanle after quiz) \n");
printf(" 4. Exit Game \n");
scanf("%d", &option); //taking user menu option
/*Error check for any input that is not a valid option. It continues until valid entry*/
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
scanf("%d", &option);
}
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
你输入的任何选项值都假设 1,while() 的第一个条件为假但其余为真所以进入循环并打印 "That is not a valid option. Please try again." 所以 替换 ||使用逻辑 And(&&).
while((option != 1) && (option != 2) && (option != 3) && (option != 4))
现在这里如果你输入正确的输入它不会显示"That is not a valid option. Please try again"
改一下怎么样
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
scanf("%d", &option);
}
到其他东西,比如
if ((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
// scanf("%d", &option); // This is probably not required
}
或
if ( option >= 1 && option <= 4)
{
printf("\n That is not a valid option. Please try again. \n");
// scanf("%d", &option);
}
因为你在外面使用无限循环,为什么你需要一个内部?如果选择了不可用的选项,您只需选中该选项并显示菜单。
之后您可以将所有逻辑放入此 if
中,每个选项对应一个选择。
更好地使用switch
以便更好地理解
/* After selecting an option */
switch (option)
{
case 1:
/* Do the operation according */
break;
case 2:
/* Do the operation according */
break;
case 3:
/* Do the operation according */
break;
case 4:
/* Do the operation according */
break;
default:
/* If none of the option selected */
printf ("Wrong input! \n")
break;
}
希望你明白了。
问题出在 printf 函数上,它在您输入以下选项后才打印出来,直到用户回答后才询问。 printf 解决后的简单刷新。
据我所知,不管任何其他问题,下面的程序都应该打印出标题和菜单选项,然后提示用户输入。
但是,它什么都不做,当我停止执行时,它会打印出菜单等,然后,由于它没有要求用户输入选项,它会重复打印 "This is not a valid option" 行。
*编辑:我已经完全删除了循环。我在程序中所拥有的只是打印标题、打印菜单、要求用户输入,在我终止之前,我仍然无法在控制台上看到任何东西。我的输入请求有问题吗?
EDIT2:绝对是 scanf,因为没有它一切正常。我 运行 带有附加功能的代码可以打印出存储在选项中的值,当我在要求用户输入之前没有将它设置为 0 时它告诉我 -1。该程序似乎是自动分配选项,而不是费心询问用户他们想要什么。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main ()
{
/*Print Title*/
printf("Maths Quiz Game \n");
printf("\n");
int i;
int rightCount = 0; //count of right answers
int wrongCount = 0; //count of wrong answers
int questions = 0; //user input for number of questions
int exit = 0; //store exit option
int option = 0; //menu option
while(exit == 0) //while loop that keeps program running until exit is chosen
{
/*Menu Options*/
printf("Please choose an option from the menu below. Enter the number of your choice. \n");
printf(" 1. Choose number of questions for this round. (Max = 5) \n");
printf(" 2. Start Quiz \n");
printf(" 3. Display total of right and wrong answers. (Only availanle after quiz) \n");
printf(" 4. Exit Game \n");
scanf("%d", &option); //taking user menu option
/*Error check for any input that is not a valid option. It continues until valid entry*/
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
scanf("%d", &option);
}
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
你输入的任何选项值都假设 1,while() 的第一个条件为假但其余为真所以进入循环并打印 "That is not a valid option. Please try again." 所以 替换 ||使用逻辑 And(&&).
while((option != 1) && (option != 2) && (option != 3) && (option != 4))
现在这里如果你输入正确的输入它不会显示"That is not a valid option. Please try again"
改一下怎么样
while((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
scanf("%d", &option);
}
到其他东西,比如
if ((option != 1) || (option != 2) || (option != 3) || (option != 4))
{
printf("\n That is not a valid option. Please try again. \n");
// scanf("%d", &option); // This is probably not required
}
或
if ( option >= 1 && option <= 4)
{
printf("\n That is not a valid option. Please try again. \n");
// scanf("%d", &option);
}
因为你在外面使用无限循环,为什么你需要一个内部?如果选择了不可用的选项,您只需选中该选项并显示菜单。
之后您可以将所有逻辑放入此 if
中,每个选项对应一个选择。
更好地使用switch
以便更好地理解
/* After selecting an option */
switch (option)
{
case 1:
/* Do the operation according */
break;
case 2:
/* Do the operation according */
break;
case 3:
/* Do the operation according */
break;
case 4:
/* Do the operation according */
break;
default:
/* If none of the option selected */
printf ("Wrong input! \n")
break;
}
希望你明白了。
问题出在 printf 函数上,它在您输入以下选项后才打印出来,直到用户回答后才询问。 printf 解决后的简单刷新。