生成一个骰子游戏-C编程
我正在关注youtube上的一个教程,当时正在做一个骰子生成器。 它基本上打印出3个骰子的结果,并将结果相加。 之后,用户将查看总和,并基于总和,用户将猜测下一个滚动是更高、更低还是相同 下面是我的代码,假设,当我键入“yes”时,它应该在if语句中执行代码。然而,它直接转到了else声明。有人能告诉我怎么了吗生成一个骰子游戏-C编程,c,dice,C,Dice,我正在关注youtube上的一个教程,当时正在做一个骰子生成器。 它基本上打印出3个骰子的结果,并将结果相加。 之后,用户将查看总和,并基于总和,用户将猜测下一个滚动是更高、更低还是相同 下面是我的代码,假设,当我键入“yes”时,它应该在if语句中执行代码。然而,它直接转到了else声明。有人能告诉我怎么了吗 int answer; int guess; int diceRoll4 = 0; printf("Would you like to guess your next dice? Y/N
int answer;
int guess;
int diceRoll4 = 0;
printf("Would you like to guess your next dice? Y/N \n");
scanf(" %c", &answer);
if (answer == 'yes' ){
printf("What is your guess?\n");
printf("please key in your number \n");
scanf(" %d", &guess);
if (guess > diceRoll4 ){
printf(" You got it wrong, too high!");
}
else if (guess < diceRoll4){
printf(" You got it wrong, too low!");
}
else {
printf("You got it right");
}
}
else{
printf("Thanks for playing");
}
int答案;
智力猜测;
int=4=0;
printf(“您想猜下一个骰子吗?是/否\N”);
scanf(“%c”和“应答”);
如果(回答=‘是’){
printf(“您的猜测是什么?\n”);
printf(“请输入您的号码”);
scanf(“%d”,猜测(&guess);
如果(猜测>4){
printf(“你搞错了,太高了!”);
}
else if(猜测<4){
printf(“你搞错了,太低了!”);
}
否则{
printf(“你做对了”);
}
}
否则{
printf(“感谢您的参与”);
}
测试您必须使用的相等性。如果返回值为0
,则表示它们相等
if (strcmp(answer, "yes") == 0) {
// ...
} else {
// ...
}
注意事项:
answer==“yes”
测试指针的相等性,而不是值。这就是为什么只在else
中输入的原因answer
是int
您必须更改为数组
char answer[15]
%c
,而您必须使用%s
scanf("%s", answer);
'yes'
改为的“yes”
,它是一个字符数组,末尾是\0
“是”
是一个多字节字符,其行为由实现定义
您可能需要读取并比较单个字符
:
if (answer == 'y' ){
或者读取整个字符串并比较:
char answer[128];
scanf("%s", answer);
if ( strcmp(answer,"yes") == 0 ){
...
}
请注意,我更改了
answer
的类型,并使用%s
读取字符串。首先,answer
应该是char
的数组,以便保存字符串。改变
int answer;
到
第二,由于您希望扫描字符串而不是字符,请更改
scanf(" %c", &answer);
到
9将最多扫描9个字符(+1表示末尾的NUL终止符),从而防止。我已经删除了
&
,因为%s
需要字符*
,而&answer
将提供字符(*)[10]
。数组的名称被转换为指向其第一个元素char*
的指针,这正是%s
所期望的。因此,上述scanf
相当于
scanf("%9s", &answer[0]);
第三,使用==
比较两个字符串比较指针,而不是指针中的实际内容。改用string.h
中的strcmp
。当它的两个参数包含相同的内容时,它返回0。改变
if (answer == 'yes' ){
到
双引号用于表示以NUL结尾的字符串(
char*
),这正是strcmp所期望的,而单引号,如在代码中一样,是一种多字符文字,其值由实现定义。如果不想读取字符串,但是只有一个字符
,用户可以回答Y
或N
,您应该更改int-answer代码>至字符应答代码>。然后,您可以继续使用原始的scanf()
-调用。你仍然需要改变
if (answer == 'yes')
到
如果您希望用户键入y
或y
,您可以从ctype.h
中使用touper()
,并将If
-条件更改为If(toupper(answer)='y')
此行:
如果(回答=‘是’){
有几个问题
1) the definition of 'answer' is 'int' but the scanf is inputting a single character
2) answer could be compared with 'y' or 'n' but not to a array of char.
3) since the scanf only input a single char
and you/the user input 'yes',
only the first character was consumed,
so the 'es' are still in the input buffer
4) note the the single character could be anything, except white space.
the leading space in the format string would consume any white space.
so the user could input say 'y' or 'Y'
these are different characters
however, using the toupper() macro from ctypes.h
would mean only a 'Y' would need to be compared
5) if you decide to read a string,
then 'answer' needs to be a character array,
say: char answer[10];
and the scanf needs to have a max length modifier
on the associated "%s" input/conversion parameter
so as to avoid the user overflowing the input buffer
and the comparison would be via the strcmp() function
6) always check the returned value (not the parameter value)
from scanf to assure the operation was successful
7) diceRoll4 and guess can never be a negative number
so the variable definitions should be unsigned
and the associated scanf() for guess should use
something like "%u"
8) on the printf() format strings, always end them with '\n'
so the sting will be immediately displayed to the user,
otherwise, they will only be displayed
when a input statement is executed or the program exits
除其他事项外,scanf(…%c..
读取单个字符..发布的代码既不编译,也不是完整的程序。发布有运行时问题的代码时,请。发布干净编译并显示问题的代码。
if (strcmp(answer, "yes") == 0){
if (answer == 'yes')
if (answer == 'Y')
1) the definition of 'answer' is 'int' but the scanf is inputting a single character
2) answer could be compared with 'y' or 'n' but not to a array of char.
3) since the scanf only input a single char
and you/the user input 'yes',
only the first character was consumed,
so the 'es' are still in the input buffer
4) note the the single character could be anything, except white space.
the leading space in the format string would consume any white space.
so the user could input say 'y' or 'Y'
these are different characters
however, using the toupper() macro from ctypes.h
would mean only a 'Y' would need to be compared
5) if you decide to read a string,
then 'answer' needs to be a character array,
say: char answer[10];
and the scanf needs to have a max length modifier
on the associated "%s" input/conversion parameter
so as to avoid the user overflowing the input buffer
and the comparison would be via the strcmp() function
6) always check the returned value (not the parameter value)
from scanf to assure the operation was successful
7) diceRoll4 and guess can never be a negative number
so the variable definitions should be unsigned
and the associated scanf() for guess should use
something like "%u"
8) on the printf() format strings, always end them with '\n'
so the sting will be immediately displayed to the user,
otherwise, they will only be displayed
when a input statement is executed or the program exits