已经搞乱了一段时间,似乎可以解决它。有人看到我不喜欢的东西吗?嵌套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)
这些嵌套的字符给我的问题。
答案 0 :(得分:0)
注意逻辑比较运算符('==')和表示运算符('=')。 也尽量避免使用scanf(“%C”),尽可能使用string或int,或者使用_getch();
// 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("\n\nHey it's your turn!\n\n");
printf("choose rock,paper, or scissors\n");
printf("and see if you can be me young padawan : ");
// character input validation loop
do {
chOK = 1;
ch1=_getch();
switch(ch1) {
case'R':
case 'r':
ch1='R';
break;
case 'P':
case 'p':
ch1='P';
break;
case 'S':
case 's':
ch1='S';
break;
case 27:
break;
default :
chOK = 0;
}
} while (!chOK);
printf("%c\n",ch1);
// Get computer's move
ch2 = rand() % 3; // C code to get a rand #
ch2="RSP"[ch2];
// Check who won
printf("\n [ ");
if (ch1 == 'R'){
if( ch2 == 'R')
printf("Rock Rock Draw");
if( ch2 == 'P')
printf("Rock Paper Puter");
if( ch2 == 'S')
printf("Rock Scissors Human");
}
if (ch1 == 'P'){
if( ch2 == 'R')
printf("Paper Paper Draw");
if( ch2 == 'P')
printf("Paper Rock Human");
if( ch2 == 'S')
printf("Paper Scissors Puter");
}
if (ch1 == 'S'){
if( ch2 == 'R')
printf("Scissors Scissors Draw");
if( ch2 == 'P')
printf("Scissors Paper Human");
if( ch2 == 'S')
printf("Scissor Rock Puter");
}
printf(" ]\n");
} while (ch1 != 27);
printf("\nProgram terminated\n");
}