查询用户以继续在C中打印
我正在编写一个从0开始打印的程序。查询用户以继续在C中打印,c,C,我正在编写一个从0开始打印的程序。 它最多打印15个,并向用户询问y/n问题 如果y该程序将打印下一个15 如果n程序停止 我写的程序不起作用 帮助解决这个问题 int main() { int i=0,k=1; char ans; while(k=1) { i++; printf("\n%d",i); if(i%15==0) { printf("\nDo you want to continue?(y/n): "); sc
它最多打印15个,并向用户询问y/n问题
- 如果y该程序将打印下一个15李>
- 如果n程序停止李>
int main()
{
int i=0,k=1;
char ans;
while(k=1)
{
i++;
printf("\n%d",i);
if(i%15==0)
{
printf("\nDo you want to continue?(y/n): ");
scanf("%c",ans);
ans = toupper(ans);
if(ans=='Y') {
continue;
}
else if(ans=='N') {
k=0;
}
}
}
}
----------------------------------编辑-------------------------------------
将代码更改为@Programmer400。还有15-->3。现在我的电脑打印
1
2
3
Do you want to continue?(y/n): y
4
5
6
Do you want to continue?(y/n):
7
8
9
Do you want to continue?(y/n): y
首先它打印到3,然后请求。在Y之后,它一直打印到6并询问,然后在没有任何输入的情况下打印到9并询问。请注意第二个问题中缺少的y。我尝试更改代码,使其不会生成警告,现在它似乎“起作用”(但可能更正确)
#包括
#包括
#包括
int main()
{
int i=0,k=1;;
字符c;
字符ans[]=“”;
而(k==1)
{
i++;
printf(“\n%d”,i);
如果(i%15==0)
{
printf(“\n是否继续?(是/否):”;
scanf(“%c”,ans);
ans[0]=(char)toupper(ans[0]);
if(ans[0]=='Y'){
继续;
}
else if(ans[0]='N'){
k=0;
}
}
}
}
我尝试更改您的代码,使其不会生成警告,现在它似乎“起作用”(但可能更正确)
#包括
#包括
#包括
int main()
{
int i=0,k=1;;
字符c;
字符ans[]=“”;
而(k==1)
{
i++;
printf(“\n%d”,i);
如果(i%15==0)
{
printf(“\n是否继续?(是/否):”;
scanf(“%c”,ans);
ans[0]=(char)toupper(ans[0]);
if(ans[0]=='Y'){
继续;
}
else if(ans[0]='N'){
k=0;
}
}
}
}
我在下面提供了一个工作的C程序,它执行您在问题中指定的任务
我一直在努力忠实于您在原始代码示例中使用的函数,而且我也只注意添加(而不是删除代码)
在注释中,我解释了我添加的代码行,这些代码行不在您的原始代码示例中
#include <stdio.h>
int main(void)
{
int i = 0, k = 1;
char user_input;
char ans;
while(k == 1)
{
i++;
printf("%d\n", i);
if (i % 15 == 0)
{
printf("Do you want to continue? (y/n/Y/N): ");
scanf(" %c",&user_input); // Keep the whitespace in front of the %c format specifier -- it's important!
getchar(); // Consume the newline character left in the buffer by scanf()
// Check if user input is already capitalized
if (user_input >= 65 && user_input <= 90)
// If it is, keep it capitalized
ans = user_input;
else
// If it isn't, capitalize it
ans = toupper(user_input);
if (ans=='Y')
{
// Allow the loop to continue
continue;
}
else if (ans == 'N')
{
// Inform the user that execution is ending
printf("Exiting loop... ending program.\n");
// Consider removing 'k' entirely, just use a 'break' statement
k = 0;
}
else
{
// Inform the user that the input was not recognized (if not y/n/Y/N...)
printf("User input not recognized... please provide input again.\n");
// Decrement 'i' so that the user is forced to provide input again...
i--;
// Allow the loop to continue
continue;
}
}
}
}
#包括
内部主(空)
{
int i=0,k=1;
字符用户输入;
查尔安斯;
而(k==1)
{
i++;
printf(“%d\n”,i);
如果(i%15==0)
{
printf(“您想继续吗?(是/否/是/否):”;
scanf(“%c”,&user_input);//在%c格式说明符前面保留空格——这很重要!
getchar();//使用scanf()在缓冲区中留下的换行符
//检查用户输入是否已大写
如果(user_input>=65&&user_input我在下面提供了一个工作的C程序,它执行您在问题中指定的任务
我一直在努力忠实于您在原始代码示例中使用的函数,而且我也只注意添加(而不是删除代码)
在注释中,我解释了我添加的代码行,这些代码行不在您的原始代码示例中
#include <stdio.h>
int main(void)
{
int i = 0, k = 1;
char user_input;
char ans;
while(k == 1)
{
i++;
printf("%d\n", i);
if (i % 15 == 0)
{
printf("Do you want to continue? (y/n/Y/N): ");
scanf(" %c",&user_input); // Keep the whitespace in front of the %c format specifier -- it's important!
getchar(); // Consume the newline character left in the buffer by scanf()
// Check if user input is already capitalized
if (user_input >= 65 && user_input <= 90)
// If it is, keep it capitalized
ans = user_input;
else
// If it isn't, capitalize it
ans = toupper(user_input);
if (ans=='Y')
{
// Allow the loop to continue
continue;
}
else if (ans == 'N')
{
// Inform the user that execution is ending
printf("Exiting loop... ending program.\n");
// Consider removing 'k' entirely, just use a 'break' statement
k = 0;
}
else
{
// Inform the user that the input was not recognized (if not y/n/Y/N...)
printf("User input not recognized... please provide input again.\n");
// Decrement 'i' so that the user is forced to provide input again...
i--;
// Allow the loop to continue
continue;
}
}
}
}
#包括
内部主(空)
{
int i=0,k=1;
字符用户输入;
查尔安斯;
而(k==1)
{
i++;
printf(“%d\n”,i);
如果(i%15==0)
{
printf(“您想继续吗?(是/否/是/否):”;
scanf(“%c”,&user_input);//在%c格式说明符前面保留空格——这很重要!
getchar();//使用scanf()在缓冲区中留下的换行符
//检查用户输入是否已大写
如果(用户输入>=65和用户输入while(k=1)
while(k==1)
和scanf(%c),ans)
scanf(%c),ans)
i++;printf(\n%d,i)
printf(\n%d,i++)
?while
i++;printf(“\n%d”,i);
-->printf(\n%d,i++);
?