【发布时间】:2015-11-08 08:56:05
【问题描述】:
已经搞砸了一段时间,似乎可以处理它。有人看到我看不到的东西吗? Char 在嵌套 if 语句的最底部给了我主要问题 谢谢!
// This is a mix of psuedocode and actual C code
#include "stdio.h"
#include "conio.h"
#include "stdlib.h" // rand is in here
#include "time.h" // contains time structure
#pragma warning (disable : 4996) // turn off stdio function warnings
int main(void)
{
time_t t; // a time object for rand seed
// seed random number generator
srand((unsigned)time(&t));
// MAIN PROCESSION LOOP
char ch1;
char ch2;
int get;
int chOK = 0;
do {
// Get human's move
printf("Hey it's your turn!\n choose rock,");
printf(" paper, or scissors and see if you can be me young padawan");
// character input validation loop
do {
scanf("%c", &ch1);
switch(ch1)
{
case'R':
break;
case 'r':
break;
case 'P':
break;
case 'p':
break;
case 'S':
break;
case 's':
break;
case 27:
chOK = 1;
break;
default :
chOK = 0;
break;
}
} while (chOK = 1);
// Convert input to uppercase
//
if (ch1 > 'Z') ch1 -= 32;
// Get computer's move
ch2 = rand() % 3; // C code to get a rand #
switch (ch2)
{
case 0:
ch2 = 'R';
break;
case 1:
ch2 = 'S';
break;
case 3:
ch2 = 'P';
break;
}
// Check who won
if (ch1 == 'R' && ch2 == 'R')
printf("Rock Rock Draw");
if (ch1 = 'R' && ch2 = 'P')
printf("Rock Paper Puter");
if (ch1 = 'R' && ch2 = 'S')
printf("Rock Scissors Human");
if (ch1 = 'S' && ch2 == 'P')
printf("Paper Paper Draw");
if (ch1 = 'P' && ch2 = 'R')
printf("Paper Rock Human");
if (ch1 = 'P' && ch2 = 'S')
printf("Paper Scissors Puter");
if (ch1 = 'S' && ch2 == 'S')
printf("Scissors Scissors Draw");
if (ch1 = 'S' && ch2 = 'P')
printf("Scissors Paper Human");
if (ch1 = 'S' && ch2 = 'R')
printf("Scissor Rock Puter");
} while (ch1 != 27)
这些嵌套的 if 字符给我带来了问题。
【问题讨论】:
-
您应该使用
==来比较chars,而不是=。一个等号就是赋值。 -
耶啊啊啊!!!!!!非常感谢!
-
scanf("%c", &ch1);-->scanf(" %c", &ch1);消耗左边的换行符\n字符。
标签: c error-handling