gpt4 book ai didi

c - C 中的退出选项

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:40 26 4
gpt4 key购买 nike

我知道这是一段非常愚蠢的代码,它只是让我尝试了解事情...我认为我做得还不错,(除了没有正确缩进)

但我正在努力为用户提供使用“Q”字符退出程序的选项,但似乎无法做到正确?

有人能帮忙吗..

#include <stdio.h>
#define YEAR 2015

int main()
{
int yearBorn, age, ageLimit,years;
char quit = 'Q';

ageLimit=16;

do // The program will continue running until the user enters an age greater than 16 or presses the 'Q' character
// to quit the program
{
printf("\nPlease Enter The Year You Were Born: ");
scanf(" %d", &yearBorn);

age= YEAR-yearBorn;
years=ageLimit-age;

if (yearBorn==YEAR) //This IF statement will run if the user enters the current year!
{
printf("Please double check your entry as it seems ");
printf("that you havent been born yet?");

}
else if (age<=ageLimit) //If the users age is less than 16 program prints the following messages
{
printf("\nYou are too young to play the Lottery!\n");
printf("you have to wait %d year%s before you can play!\n\n",years,(years!=1) ? "s" : " ");

printf("You are %d\n",age);
}
}while (years>=1);

age = YEAR-yearBorn;
printf("You're old enough.. for heavens sake you are %d years old \n", age);

return 0;
}

最佳答案

只需声明一个类型为 charopt 变量并将其添加到 } while (years>1); 行之前:

printf ("Press %c and ENTER to quit, or just ENTER to repeat: ", quit);
do
opt = getchar();
while (opt!=quit && opt!='\n');
if (opt==quit)
break;

为避免总是执行最后一个 printf,将其包含在 if 语句中

if (opt!=quit)
{
age = YEAR-yearBorn;
printf("You're old enough.. for heavens sake you are %d years old \n", age);
}

关于c - C 中的退出选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29331264/

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