gpt4 book ai didi

C:可变增量不起作用,石头,布,剪刀

转载 作者:太空宇宙 更新时间:2023-11-04 07:57:30 27 4
gpt4 key购买 nike

我目前受困于我的 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
;
}

链接

如果问题出在其他地方,这里有一个指向整个程序的 pastebin 的链接:Link to pastebin

最佳答案

这些变量 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!

但是,请学习如何使用指针传递变量。避免使用全局变量。

关于C:可变增量不起作用,石头,布,剪刀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48995758/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com