- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我目前受困于我的 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!