C:可变增量不起作用,摇滚,纸张,剪刀

我目前坚持使用我的RPS程序,因为它无法正确存储用户丢失或与计算机绑定的次数。 例如,当我运行程序并输入“q”退出时,我得到以下输出:

输入R,P,S或Q(退出)

q

你赢了0次,而电脑打败了你1900022269次。 你们两人并列3次。

谢谢你来玩!

请注意,我已经玩了几个游戏而不是跑步和退出,并且还获得了“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的链接,以防问题出现在其他地方: 链接到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; 。 他们必须在函数调用中存活下来。 有很多解决方案。 你可以使用powershell全局声明w, l, t; (不优雅)或者你通过w, l, t; 作为参数。

 int declareWin (int one, int two, int *w, int *l, int* t) { //.. (*t)++; //... (*l)++; //... (*w)++; //... } 

编辑:

我查看了你的程序。 您已经在考虑全局变量:

 #include  #include  #include  //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! 

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

您需要在使用变量之前初始化变量。 如果不初始化它们,编译器不一定将它们设置为0,因此它们几乎可以具有任何值。