C 为什么我需要按'\n';三次让它运行?
嗨,有人知道为什么这个代码让我在运行前按三次enter键吗?我需要它,以便在第一个'\n'运行学校作业 下面是代码:非常感谢C 为什么我需要按'\n';三次让它运行?,c,loops,C,Loops,嗨,有人知道为什么这个代码让我在运行前按三次enter键吗?我需要它,以便在第一个'\n'运行学校作业 下面是代码:非常感谢 int main() { char L1=0, L2=0, L3=0 ; int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ; printf("give me some letters\n"); while ((L1!='\n')&&(L2!='\n'
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
printf("give me some letters\n");
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
{
L1=getchar();
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d",
countLA, countLC, countLL, countLH, count_hya);
return 0;
}
编辑将最后几行更改为:
if (L1=='H')
{
countLH++;
L2=getchar();
L3=getchar();
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
非常感谢你的帮助 当您使用时:
while ((L1!='\n')||(L2!='\n')||(L3!='\n'))
应该是
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
使用以下代码
#define SCANF_CHK(L) \
scanf("%c", &L); \
if(L=='\n') continue;
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
printf("give me some letters\n");
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
{
SCANF_CHK(L1);
SCANF_CHK(L2);
SCANF_CHK(L3);
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d\n",
countLA, countLC, countLL, countLH, count_hya);
return 0;
}
当您使用时:
while ((L1!='\n')||(L2!='\n')||(L3!='\n'))
应该是
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
使用以下代码
#define SCANF_CHK(L) \
scanf("%c", &L); \
if(L=='\n') continue;
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
printf("give me some letters\n");
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
{
SCANF_CHK(L1);
SCANF_CHK(L2);
SCANF_CHK(L3);
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d\n",
countLA, countLC, countLL, countLH, count_hya);
return 0;
}
当您使用时:
while ((L1!='\n')||(L2!='\n')||(L3!='\n'))
应该是
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
使用以下代码
#define SCANF_CHK(L) \
scanf("%c", &L); \
if(L=='\n') continue;
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
printf("give me some letters\n");
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
{
SCANF_CHK(L1);
SCANF_CHK(L2);
SCANF_CHK(L3);
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d\n",
countLA, countLC, countLL, countLH, count_hya);
return 0;
}
当您使用时:
while ((L1!='\n')||(L2!='\n')||(L3!='\n'))
应该是
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
使用以下代码
#define SCANF_CHK(L) \
scanf("%c", &L); \
if(L=='\n') continue;
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
printf("give me some letters\n");
while ((L1!='\n')&&(L2!='\n')&&(L3!='\n'))
{
SCANF_CHK(L1);
SCANF_CHK(L2);
SCANF_CHK(L3);
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d\n",
countLA, countLC, countLL, countLH, count_hya);
return 0;
}
这一行:
scanf("%c%c%c", &L1, &L2, &L3);
将读取3个字符。如果按enter键一次,它将读取一个字符,并等待更多字符
实际上,如果输入,您可能需要按enter键最多5次
1<enter>
<enter>
<enter>
<enter>
<enter>
1
一种解决方案是逐字符读取字符(然后可以使用
getchar()
而不是scanf(“%c”);
)。另一种解决方案是一次读取整行内容(可能使用fgets()
)
因为这是学校作业,所以我不打算为您编写解决方案:)这一行:
scanf("%c%c%c", &L1, &L2, &L3);
将读取3个字符。如果按enter键一次,它将读取一个字符,并等待更多字符
实际上,如果输入,您可能需要按enter键最多5次
1<enter>
<enter>
<enter>
<enter>
<enter>
1
一种解决方案是逐字符读取字符(然后可以使用
getchar()
而不是scanf(“%c”);
)。另一种解决方案是一次读取整行内容(可能使用fgets()
)
因为这是学校作业,所以我不打算为您编写解决方案:)这一行:
scanf("%c%c%c", &L1, &L2, &L3);
将读取3个字符。如果按enter键一次,它将读取一个字符,并等待更多字符
实际上,如果输入,您可能需要按enter键最多5次
1<enter>
<enter>
<enter>
<enter>
<enter>
1
一种解决方案是逐字符读取字符(然后可以使用
getchar()
而不是scanf(“%c”);
)。另一种解决方案是一次读取整行内容(可能使用fgets()
)
因为这是学校作业,所以我不打算为您编写解决方案:)这一行:
scanf("%c%c%c", &L1, &L2, &L3);
将读取3个字符。如果按enter键一次,它将读取一个字符,并等待更多字符
实际上,如果输入,您可能需要按enter键最多5次
1<enter>
<enter>
<enter>
<enter>
<enter>
1
一种解决方案是逐字符读取字符(然后可以使用
getchar()
而不是scanf(“%c”);
)。另一种解决方案是一次读取整行内容(可能使用fgets()
)
因为这是课业,所以我不打算为您编写解决方案:)您必须按三次/n键才能停止程序,而不是启动程序。这是暂时的情况。三个输入(l1、l2、l3)必须为/n才能离开while循环。您必须按三次/n键才能停止程序,而不是启动程序。这是暂时的情况。三个输入(l1、l2、l3)必须为/n才能离开while循环。您必须按三次/n键才能停止程序,而不是启动程序。这是暂时的情况。三个输入(l1、l2、l3)必须为/n才能离开while循环。您必须按三次/n键才能停止程序,而不是启动程序。这是暂时的情况。三个输入(l1、l2、l3)必须为/n才能留下while循环。以下是一个可行的解决方案:
#include<stdio.h>
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
int flag=1;
printf("give me some letters\n");
while (flag==1)
{
if(flag==1)
{scanf("%c",&L1);
if( L1=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L2);
if( L2=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L3);
if( L3=='\n')
flag=0;
}
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d",
countLA, countLC, countLL, countLH, count_hya);
return 0;
#包括
int main()
{
字符L1=0,L2=0,L3=0;
int count_hya=0,countLA=0,countLC=0,countLH=0,countLL=0;
int标志=1;
printf(“给我一些字母”\n);
while(标志==1)
{
如果(标志==1)
{scanf(“%c”和&L1);
如果(L1='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L2);
如果(L2=='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L3);
如果(L3=='\n')
flag=0;
}
如果(L1='A')
countLA++;
如果(L1='C')
countLC++;
如果(L1='L')
countLL++;
如果(L1='H')
countLH++;
如果(L1='H'&&L2='Y'&&L3='A')
count_hya++;
}
printf(“开头的字数:A=%d C=%d L=%d H=%d Hydra=%d”,
countLA,countLC,countLL,countLH,count_hya);
返回0;
}
在上面的代码中,仅当前一个字符与“\n”不同时,您才会读取以下字符。这样,您就不需要按“\n”三次 以下是一个有效的解决方案:
#include<stdio.h>
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
int flag=1;
printf("give me some letters\n");
while (flag==1)
{
if(flag==1)
{scanf("%c",&L1);
if( L1=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L2);
if( L2=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L3);
if( L3=='\n')
flag=0;
}
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d",
countLA, countLC, countLL, countLH, count_hya);
return 0;
#包括
int main()
{
字符L1=0,L2=0,L3=0;
int count_hya=0,countLA=0,countLC=0,countLH=0,countLL=0;
int标志=1;
printf(“给我一些字母”\n);
while(标志==1)
{
如果(标志==1)
{scanf(“%c”和&L1);
如果(L1='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L2);
如果(L2=='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L3);
如果(L3=='\n')
flag=0;
}
如果(L1='A')
countLA++;
如果(L1='C')
countLC++;
如果(L1='L')
countLL++;
如果(L1='H')
countLH++;
如果(L1='H'&&L2='Y'&&L3='A')
count_hya++;
}
printf(“开头的字数:A=%d C=%d L=%d H=%d Hydra=%d”,
countLA,countLC,countLL,countLH,count_hya);
返回0;
}
在上面的代码中,仅当前一个字符与“\n”不同时,您才会读取以下字符。这样,您就不需要按“\n”三次 以下是一个有效的解决方案:
#include<stdio.h>
int main()
{
char L1=0, L2=0, L3=0 ;
int count_hya=0, countLA=0, countLC=0, countLH=0, countLL=0 ;
int flag=1;
printf("give me some letters\n");
while (flag==1)
{
if(flag==1)
{scanf("%c",&L1);
if( L1=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L2);
if( L2=='\n')
flag=0;
}
if(flag==1)
{scanf("%c",&L3);
if( L3=='\n')
flag=0;
}
if (L1=='A')
countLA++;
if (L1=='C')
countLC++;
if (L1=='L')
countLL++;
if (L1=='H')
countLH++;
if (L1=='H'&&L2=='Y'&&L3=='A')
count_hya++;
}
printf("number of words begning with: A=%d C=%d L=%d H=%d Hydra=%d",
countLA, countLC, countLL, countLH, count_hya);
return 0;
#包括
int main()
{
字符L1=0,L2=0,L3=0;
int count_hya=0,countLA=0,countLC=0,countLH=0,countLL=0;
int标志=1;
printf(“给我一些字母”\n);
while(标志==1)
{
如果(标志==1)
{scanf(“%c”和&L1);
如果(L1='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L2);
如果(L2=='\n')
flag=0;
}
如果(标志==1)
{scanf(“%c”和&L3);
如果(L3=='\n')
flag=0;
}
如果(L1='A')
countLA++;
如果(L1='C')
countLC++;
如果(L1='L')
countLL++;
如果(L1='H')
计数