问题描述
我目前对RPS程序感到困惑,因为它无法正确存储用户丢失或与计算机建立联系的次数.例如,当我运行程序并输入"q"时,退出,我得到以下输出:
I am currently stuck with my RPS program in that it will not properly store the number of times the user loses, or ties with the computer. For instance, when I run the program and enter "q" to quit I get the following output:
q
您赢了0次,而计算机击败了您1900022269次.你们都绑了3次.
You won 0 times, while the computer beat you 1900022269 times. You both tied 3 times.
感谢您的参与!
请注意,我玩了几场游戏,而不是跑步和退出游戏,并且我对"l"的评价也很差.和"t".
Note that I have played several games instead of running and quitting and have also gotten bad values for "l" and "t".
虽然我定义了变量"w,l,t",但这似乎可行.作为全局变量;但是,有没有一种方法可以处理那些在clarifyWin函数范围内的变量?
It seems to work though if I define the variables "w, l, t" as global variables; however, is there a way for this to work with those variables being within the scope of the declareWin function?
代码
int declareWin (int one, int two) {
int w, l, t;
if (one == 1 && two == 1) {
printf("You chose Rock, Computer chose Rock. Rock does not beat Rock.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 1 && two == 2) {
printf("You chose Rock, Computer chose Paper. Paper covers Rock.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 1 && two == 3) {
printf("You chose Rock, Computer chose Scissors. Rock smashes Scissors.\n");
printf("You win!\n");
w++;
}
else if (one == 2 && two == 1) {
printf("You chose Paper, Computer chose Rock. Paper covers Rock.\n");
printf("You win!\n");
w++;
}
else if (one == 2 && two == 2) {
printf("You chose Paper, Computer chose Paper. Paper does not beat Paper.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 2 && two == 3) {
printf("You chose Paper, Computer chose Scissors. Scissors cuts Paper.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 3 && two == 1) {
printf("You chose Scissors, Computer chose Rock. Rock smashes Scissors.\n");
printf("Computer wins!\n");
l++;
}
else if (one == 3 && two == 2) {
printf("You chose Scissors, Computer chose Paper. Scissors cuts Paper.\n");
printf("You win!\n");
w++;
}
else if (one == 3 && two == 3) {
printf("You chose Scissors, Computer chose Scissors. Scissors does not beat Scissors.\n");
printf("It is a tie!\n");
t++;
}
else if (one == 0) {
printf("You won %d times, while the computer beat you %d times. You both tied %d times.\n", w, l, t);
printf("Thank you for playing!");
}
else
;
}
链接
如果问题出在其他地方,这里是指向整个程序的粘贴框的链接:链接到粘贴框
Here is a link to a pastebin of the entire program in case the problem is somewhere else: Link to pastebin
推荐答案
这些变量w, l, t;
将具有从堆栈中获取的随机值:
These variables w, l, t;
will have random values taken from stack:
int w, l, t;
//...
t++; // random number increased
//...
l++; // random number increased
//...
w++; // random number increased
//
printf("You won %d times, while the computer beat you %d times. You both tied %d times.\n", w, l, t);
要更正您的程序,您必须记住w, l, t;
.他们必须幸免于函数调用.有很多解决方案.您可以使用w, l, t;
的蛮力全局声明(不美观),也可以将w, l, t;
用作参数.
To correct your program you have to remember w, l, t;
. They have to survive the function call.There are many solution. You can use brute force global declaration of w, l, t;
(not elegant) or you pass w, l, t;
as a parameters.
int declareWin (int one, int two, int *w, int *l, int* t)
{
//..
(*t)++;
//...
(*l)++;
//...
(*w)++;
//...
}
我看了你的程序.您已经在考虑全局变量:
I looked at your program. You were already thinking about global variables:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//int w, l, t;
作为快速尝试,我从上面的行中删除了//
,并注释了
As a quick try, I removed //
from the line above, commented out the declaration of w, l, t
in
//declareWin Function
int declareWin (int one, int two) {
//int w, l, t;
编译并玩游戏:
r
p
s
s
r
q
Enter R, P, S, or Q (for quit)
You chose Rock, Computer chose Rock. Rock does not beat Rock.
It is a tie!
Enter R, P, S, or Q (for quit)
You chose Paper, Computer chose Paper. Paper does not beat Paper.
It is a tie!
Enter R, P, S, or Q (for quit)
You chose Scissors, Computer chose Paper. Scissors cuts Paper.
You win!
Enter R, P, S, or Q (for quit)
You chose Scissors, Computer chose Paper. Scissors cuts Paper.
You win!
Enter R, P, S, or Q (for quit)
You chose Rock, Computer chose Rock. Rock does not beat Rock.
It is a tie!
Enter R, P, S, or Q (for quit)
You won 2 times, while the computer beat you 0 times. You both tied 3 times.
Thank you for playing!
但是,请学习如何使用指针传递变量.避免使用全局变量.
However, please learn how to pass variables using pointers. Avoid global variables.
这篇关于C:可变增量不起作用,石头,纸张,剪刀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!