这个功能没有检查ifs的nums ... 我需要做什么? tnx用于帮助!
//Includes
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//Defines
#define FIVE 5
#define SIX 6
#define TEN 10
#define FIFHTY 15
#define TWENTY 20
#define GEN_NUM 5309
#define GEN_MIN 1234
int main() {
char c = ' ';
int hits = 0, miss = 0;
int counter = 0;
int round = TWENTY;
int flag = 0;
int input = 0;
int gen = 0;
int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
int inum1 = 0, inum2 = 0, inum3 = 0, inum4 = 0;
do {
do {
printf("generating...\n");
gen = rand() % GEN_NUM + GEN_MIN;
printf("gen %d\n", gen);
num1 = gen % TEN;
num2 = gen % (TEN*TEN) / TEN;
num3 = gen % (TEN*TEN*TEN) / (TEN*TEN);
num4 = gen / (TEN*TEN*TEN);
} while (num1 < 0 || num1 > SIX || num1 == 0 ||
num2 < 0 || num2 > SIX || num2 == 0 ||
num3 < 0 || num3 > SIX || num3 == 0 ||
num4 < 0 || num4 > SIX || num4 == 0 ||
num1 == num2 || num1 == num3 || num1 == num4 ||
num2 == num3 || num2 == num4 || num3 == num4);
do {
do {
printf("Write your guess (only 1-6, no ENTER is needed) [%d guesses left]\n", round);
num4 = getch();
putch(num4);
num3 = getch();
putch(num3);
num2 = getch();
putch(num2);
num1 = getch();
putch(num1);
num1 -= '0';
num2 -= '0';
num3 -= '0';
num4 -= '0';
printf("\nnum1 %d\n", num1);
printf("num2 %d\n", num2);
printf("num3 %d\n", num3);
printf("num4 %d\n", num4);
//if ill use scanf it will work fine...
/* scanf("%4d", &input);
printf ("\ninput: %d\n", input);
inum1 = input % TEN;
printf ("%d\n", inum1);
inum2 = input % (TEN*TEN) / TEN;
printf ("%d\n", inum2);
inum3 = input % (TEN*TEN*TEN) / (TEN*TEN);
printf ("%d\n", inum3);
inum4 = input / (TEN*TEN*TEN);
printf ("%d\n", inum4); */ //
if (num1 < 0 || num1 > SIX || num1 == 0 ||
num2 < 0 || num2 > SIX || num2 == 0 ||
num3 < 0 || num3 > SIX || num3 == 0 ||
num4 < 0 || num4 > SIX || num4 == 0 ||
num1 == num2 || num1 == num3 || num1 == num4 ||
num2 == num3 || num2 == num4 || num3 == num4)
{
printf("Only 1-6 are allowed, try again!");
flag == 1;
}
else
{
flag = 0;
}
} while (flag == 1);
hits = 0;
miss = 0;
if (inum1 == num1)
{
hits++;
}
else if(inum1 == num2 || inum1 == num3 || inum1 == num4)
{
miss++;
}
if (inum2 == num2)
{
hits++;
}
else if(inum2 == num1 || inum2 == num3 || inum2 == num4)
{
miss++;
}
if (inum3 == num3)
{
hits++;
}
else if (inum3 == num1 || inum3 == num2 || inum3 == num4)
{
miss++;
}
if (inum4 == num4)
{
hits++;
}
else if (inum4 == num1 || inum4 == num2 || inum4 == num3)
{
miss++;
}
if (hits == 4)
{
printf("4 HITS YOU WON!!!\n");
flag = 0;
}
else if (round == 0)
{
printf("OOOOHHHH!!! Pancratius won and bought all of Hanukkah's gifts.\nNothing left for you...\n");
printf("The secret password was %d\n", gen);
flag = 0;
}
else
{
printf("you got\t %d HITS\t %d MISSES\n", hits, miss);
counter++;
round--;
flag = 1;
}
} while(1 == flag);
printf("Would you like to play again? (y/n):");
c = getchar();
} while (c == 'y');
}
generating...
gen 1284
generating...
gen 3507
generating...
gen 1490
generating...
gen 1519
generating...
gen 2546
Write your guess (only 1-6, no ENTER is needed) [20 guesses left]
2465
num1 5
num2 6
num3 4
num4 2
you got 0 HITS 4 MISSES
Write your guess (only 1-6, no ENTER is needed) [19 guesses left]
generating...
gen 1284
generating...
gen 3507
generating...
gen 1490
generating...
gen 1519
generating...
gen 2546
Write your guess (only 1-6, no ENTER is needed) [20 guesses left]
2465
num1 5
num2 6
num3 4
num4 2
you got 0 HITS 0 MISSES // it should be 0 HITS 4 MISSES
Write your guess (only 1-6, no ENTER is needed) [19 guesses left]
答案 0 :(得分:0)
变量round
的定义无处可见。它应该被初始化为您获得用户输入的循环之前的最大尝试次数。
当您阅读用户输入时,您应该将其存储到变量inum1
,inum2
...而不是num1
,num2
等。
此外,您将来电调用getch()
并调用getchar()
。我不确定这种行为是否符合你的期望。
请注意,我们必须重新格式化您的代码才能理解它。学习正确格式化代码。避免长行,使用空格而不是标签一致地缩进。