#include <stdio.h>
#include <stdlib.h>
void main()
{
char choice = 'y';
while(choice != 'n')
{
printf("Enter choice(y/n):- \n");
scanf("%c", choice);
if(choice == 'y')
{
printf("Choice = y");
}
if (choice == 'n')
{
printf("Choice = n");
exit(1);
}
}
}
字符串
实际输出:
Enter choice(y/n):-
y
(Program execution terminated)
型
预期输出:
Enter choice(y/n):-
y
Enter choice(y/n):-
y
Enter choice(y/n):-
n
(Program execution terminated)
型
我想询问用户选择,直到他/她不按“n”。这是我的问题
2条答案
按热度按时间mhd8tkvw1#
代码中的问题是
scanf
。使用scanf
阅读字符时,需要使用格式说明符%c
,并使用&
运算符提供存储字符的变量的地址:字符串
6ie5vjzr2#
字符串