c - 在石头,纸,剪刀中打破循环 - C
问题描述
#include <stdio.h>
#include <stdlib.h> //for random()
#include <time.h>
void RockPaperScissors(char usrVal);
main(){
char input;
int i;
for (i = 0; i < 3; ++i){
printf("\nEnter r, p, or s for rock paper scissors: ");
input = getchar();
RockPaperScissors(input);
}
printf("\n\n\nTHE GAME IS OVER\n\n");
}
void RockPaperScissors(char usrVal){
const int rock = 0;
const int paper = 1;
const int scissors = 2;
srand(time(NULL));
int comVal = rand() % 2 + 0;
int usrScore = 0;
int comScore = 0;
int tie = 0;
switch (usrVal){
case 'r':
if (comVal == paper)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
if (comVal == scissors)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
case 'p':
if (comVal == rock)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
if (comVal == scissors)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
case 's':
if (comVal == rock)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
if (comVal == scissors)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
default:
printf("\nTIE\n");
++tie;
printf("\ncomVal: %d usrVal: %c\n", comVal, usrVal);
}
printf("\nscore is user: %d computer: %d Tie: %d \n\n\n\n", usrScore, comScore, tie);
}
// ISSUES: need way to store score count.. need to fix rando()..sometimes printf() is skipped in switch statement
//Need to fix the for loop (it does not ask for input every time)
我已经在这个问题上停留了几个小时。我浏览了多个论坛,并没有发现任何可以真正解释我的问题的东西。程序将进入循环的一次迭代,然后跳过用户输入。但之后它会要求输入。
为石头剪刀布输入 r、p 或 s:r
得分是用户:0 计算机:1 领带:0
为石头剪刀布输入 r、p 或 s:TIE
comVal:0 usrVal:
得分是用户:0 计算机:0 领带:1
为石头剪刀布输入 r、p 或 s:r
得分是用户:0 计算机:1 领带:0
游戏结束了
解决方案
使 。input = getchar();
_ scanf("\n%c", &input);
您的问题是不受管理的\n
(新行)。\n
也被认为是一个字符。所以 enter你按的用于下一步getchar()
。
也使main()
( int main()
main 应该有一些返回类型通常是 int)和改进你的代码缩进。(如果您正在使用,请不要忘记添加)return 0;
int main()
修改后的代码:-
#include <stdio.h>
#include <stdlib.h> //for random()
#include <time.h>
void RockPaperScissors(char usrVal);
int main() // make it int main()
{
char input;
int i;
for (i = 0; i < 3; ++i)
{
printf("\nEnter r, p, or s for rock paper scissors: ");
scanf("\n%c", &input); // not input = getchar();
RockPaperScissors(input);
}
printf("\n\n\nTHE GAME IS OVER\n\n");
return 0;
}
void RockPaperScissors(char usrVal)
{
const int rock = 0;
const int paper = 1;
const int scissors = 2;
srand(time(NULL));
int comVal = rand() % 2 + 0;
int usrScore = 0;
int comScore = 0;
int tie = 0;
switch (usrVal)
{
case 'r':
if (comVal == paper)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
if (comVal == scissors)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
case 'p':
if (comVal == rock)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
if (comVal == scissors)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
case 's':
if (comVal == rock)
printf("\nYOU LOSE!!!\n");
++comScore;
break;
if (comVal == scissors)
printf("\nYOU WIN!!!\n");
++usrScore;
break;
default:
printf("\nTIE\n");
++tie;
printf("\ncomVal: %d usrVal: %c\n", comVal, usrVal);
}
printf("\nscore is user: %d computer: %d Tie: %d \n\n\n\n", usrScore, comScore, tie);
}
推荐阅读
- flutter - 如何限制 ListView 项目长度以防止错误
- apache-kafka - Kafka java消费者在轮询调用中只获取几条消息,有时没有,有人可以帮助增加轮询计数
- javascript - 如何在 React 功能组件中使用 Props 将 Redux 函数添加为参数
- selenium - 黄瓜找不到默认步骤定义类
- flutter - Flutter StreamBuilder和Python TCP Socket服务器无法连接
- c# - 直接从控制器运行 SQL Server 存储过程
- git - 可以将本地开发分支与另一个本地分支同步
- r - 将字符日期转换为R中的日期
- java - 找不到类路径中的 JKS 密钥库文件
- ruby-on-rails - 提交表单后更新 Ruby on rails 视图时出现问题