【发布时间】: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