C:可变增量不起作用,石头,布,剪刀
C: Variable Increment not working, Rock, Paper, Scissors
我目前受困于我的 RPS 程序,因为它无法正确存储用户丢失的次数或与计算机的关联。例如,当我 运行 程序并输入“q”退出时,我得到以下输出:
Enter R, P, S, or Q (for quit)
q
You won 0 times, while the computer beat you 1900022269 times. You both tied 3 times.
Thank you for playing!
请注意,我玩了几场比赛而不是 运行宁和退出,并且“l”和“t”的值也很糟糕。
如果我将变量“w、l、t”定义为全局变量,它似乎可以工作;但是,有没有办法让它与 declareWin 函数范围内的那些变量一起工作?
代码
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
;
}
Link
这里是一个 link 到整个程序的 pastebin,以防问题出在其他地方:Link to pastebin
您需要在使用变量之前对其进行初始化。如果你不初始化它们,编译器不一定会将它们设置为 0,因此它们几乎可以是任何值。
这些变量 w, l, t;
将从堆栈中获取随机值:
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;
作为参数传递。
int declareWin (int one, int two, int *w, int *l, int* t)
{
//..
(*t)++;
//...
(*l)++;
//...
(*w)++;
//...
}
编辑:
我看了你的程序。您已经在考虑全局变量:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//int w, l, t;
作为快速尝试,我从上面的行中删除了 //
,并在
中注释掉了 w, l, t
的声明
//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!
但是,请学习如何使用指针传递变量。避免使用全局变量。
我目前受困于我的 RPS 程序,因为它无法正确存储用户丢失的次数或与计算机的关联。例如,当我 运行 程序并输入“q”退出时,我得到以下输出:
Enter R, P, S, or Q (for quit)
q
You won 0 times, while the computer beat you 1900022269 times. You both tied 3 times.
Thank you for playing!
请注意,我玩了几场比赛而不是 运行宁和退出,并且“l”和“t”的值也很糟糕。
如果我将变量“w、l、t”定义为全局变量,它似乎可以工作;但是,有没有办法让它与 declareWin 函数范围内的那些变量一起工作?
代码
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
;
}
Link
这里是一个 link 到整个程序的 pastebin,以防问题出在其他地方:Link to pastebin
您需要在使用变量之前对其进行初始化。如果你不初始化它们,编译器不一定会将它们设置为 0,因此它们几乎可以是任何值。
这些变量 w, l, t;
将从堆栈中获取随机值:
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;
作为参数传递。
int declareWin (int one, int two, int *w, int *l, int* t)
{
//..
(*t)++;
//...
(*l)++;
//...
(*w)++;
//...
}
编辑:
我看了你的程序。您已经在考虑全局变量:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//int w, l, t;
作为快速尝试,我从上面的行中删除了 //
,并在
w, l, t
的声明
//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!
但是,请学习如何使用指针传递变量。避免使用全局变量。