【问题标题】:C Programming Rock-Paper-Scissors Not Working [duplicate]C编程石头剪刀布不工作[重复]
【发布时间】:2018-10-17 11:57:13
【问题描述】:

我遇到了这个 C 编程活动的问题。我是 C 编程的新手,真的很想向你们寻求帮助。所以请多多包涵。

那么问题来了……

当我运行程序时,我输入我对玩家二的选择,程序会自动停止并关闭。它甚至不会让我在关闭之前按“ENTER”。我尝试了所有其他方法并在网上寻找解决方案,但无济于事。我只能为此使用条件语句 If-Else。我真的很感激任何建议。提前致谢。

#include<stdio.h>
#include<conio.h>

void main(){
clrscr();
char one, two;

printf("\t\t\t      Rock-Paper-Scissors\n");
printf("Choose from the following:\n");
printf("r for Rock\np for Paper\ns for Scissors\n\n");
printf("Player One enter your choice: ");
scanf("%c",&one);
printf("Player Two enter your choice: ");
scanf("%c",&two);
printf("\n");


//rock over scissors
if (one == 'r' && two == 's'){
    printf("Rock Destroys Scissors\n");
    printf("Player One Wins!");
}
else if (two == 'r' && one == 's'){
    printf("Rock Destroys Scissors\n");
    printf("Player Two Wins!");
}
//paper over rock
else if (one == 'p' && two == 'r'){
    printf("Paper Covers Rock\n");
    printf("Player One Wins!");
}
else if (two == 'p' && one == 'r'){
    printf("Paper Covers Rock\n");
    printf("Player Two Wins!");
}
//scissors over paper
else if (one == 's' && two == 'p'){
    printf("Scissors Cut Paper\n");
    printf("Player One Wins!");
}
else if (two == 's' && one == 'p'){
    printf("Scissors Cut Paper\n");
    printf("Player Two Wins!");
}
else
    printf("It's a Tie!");
getch();
}

【问题讨论】:

  • 这是开始学习如何使用调试器的绝佳时机!
  • 尝试scanf(" ") /* consume whitespace */;(“格式字符串”包含一个空格)消耗输入缓冲区中的可选空格。

标签: c


【解决方案1】:

只需将scanf("%c",&amp;one); 更改为scanf(" %c",&amp;one); 并将scanf("%c",&amp;two); 更改为      scanf(" %c",&amp;two);

scanf function seems to be skipped in c

【讨论】:

  • 非常感谢@suvojit_007。正如我所说,我是 C 的新手,不知道如何处理它,所以非常感谢您的回答。
猜你喜欢
  • 1970-01-01
  • 2017-11-27
  • 2021-01-10
  • 2017-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-16
  • 1970-01-01
相关资源
最近更新 更多