验证输入是否为数字以及是否重复了数字?

时间:2019-01-03 16:49:16

标签: objective-c arrays loops

我知道这个游戏上有一些问题,但是我的还有更多说明,因此难度更大。我快完成了,我还有几件事要做。我的代码如下:

- (NSString *)output {

    NSMutableString *resultOutput = [[NSMutableString alloc] init];
    int secretNumber= arc4random_uniform(10);
    int numberChosen;
    int attempt=0;

    NSMutableArray<NSNumber*> *myArray = [[NSMutableArray alloc] init];

    printf("Guess a Number\n");
    printf("\n");
    scanf("%d", &numberChosen);

    while (numberChosen != secretNumber) {

        if (numberChosen < secretNumber) {
            printf("Too Low\n");
            printf("Guess Again\n");
            scanf("%d", &numberChosen);
            attempt++;
        }
        else if (numberChosen > secretNumber) {
            printf("Too high\n");
            printf("Guess Again\n");
            scanf("%d", &numberChosen);
            attempt++;
        }
        else if (numberChosen ==secretNumber) {
            attempt++;
            break;
        }

        [myArray addObject:[NSNumber numberWithInt:numberChosen]];
    }
    if (numberChosen == secretNumber) {
        NSLog(@"\nGood job, you guessed in %i tries", attempt);

    }

    return resultOutput;
}

我的输出应该是:

Guess a number:
> 12
Too Low!
Guess a number:
> 65
Too High!
Guess a number:
> 65
Already Guessed!
Guess a number:
> asdf
Not a number! Try again!
Guess a number:
> 42
You got it! You took 3 attempts!

我可以创建一个循环来验证您是更高还是更低,以及尝试了多少次。我现在坚持要验证输入是否为字母,如果是,则在控制台上显示该输入无效。在验证是否已输入数字并显示一条消息说该数字已被猜中时,我也遇到了麻烦。

我们将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:0)

使用scanf的返回值。

documentation

  

返回值

     

此函数返回分配的输入项目数。这可以是        如果匹配失败,则少于规定的数量,甚至为零。
       零表明,尽管有输入可用,但没有转化        被分配;通常是由于输入字符无效,        例如用于“%d”转换的字母字符。值EOF为        如果在任何转换(例如文件结束)之前发生输入失败,则返回        发生。如果转换后出现错误或文件结尾        开始后,成功完成的转化次数为        返回。

编辑:

如果使用返回值,则必须在输入字母的情况下清空缓冲区。

这是所有条件的实现。而不是使用数组来避免重复:

int secretNumber = arc4random_uniform(10);
int numberChosen = 0;
int returnValue = -1;
int attempt = 0;

NSMutableSet<NSNumber*> *set = [[NSMutableSet alloc] init];

printf("Guess a Number\n");
printf("\n");

while (numberChosen != secretNumber) {
    returnValue = scanf("%d", &numberChosen);
    attempt++;
    if (numberChosen == secretNumber) {
        break;
    }
    else if ([set containsObject:@(numberChosen)]) {
        printf("Already Guessed!\n");
    }
    else if (returnValue == 0) {
        printf("Not a number!\n");
        int c;
        while((c = getchar()) != '\n' && c != EOF);
    }
    else if (numberChosen < secretNumber) {
        printf("Too Low\n");
        [set addObject:@(numberChosen)];
    }
    else if (numberChosen > secretNumber) {
        printf("Too high\n");
        [set addObject:@(numberChosen)];
    }
    printf("Guess Again\n");
    printf("\n");

}
NSLog(@"\nGood job, you guessed in %i tries", attempt);

答案 1 :(得分:0)

在可能的情况下,使用Objective-C类型,以便您可以利用诸如数字格式化程序之类的内置类来解析输入,并利用NSArray来记录先前的猜测。使用方法来组织代码,以使游戏算法清晰明了。

@property(strong,nonatomic) NSMutableArray *priorGuesses;

- (NSNumber *)promptForNumber {
    char cstring[256];

    NSLog(@"Guess a Number\n");
    scanf("%s", cstring);         // note that input exceeding 256 chars will write past this buffer

    NSString *string = [NSString stringWithCString:cstring encoding:NSUTF8StringEncoding];
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    return [formatter numberFromString:string];
}

- (BOOL)isUnique:(NSNumber *)number {
    if ([self.priorGuesses containsObject:number]) return NO;
    [self.priorGuesses addObject:number];
    return YES;
}

- (void)game {
    BOOL gameOn = YES;
    self.priorGuesses = [NSMutableArray array];

    NSInteger secretNumber= arc4random_uniform(10);
    NSNumber *guess = @(-1);

    while (gameOn) {
        guess = [self promptForNumber];
        if (guess && [self isUnique:guess]) {
            NSInteger guessInt = [guess intValue];
            if (guessInt == secretNumber) {
                // notice that there's no need to count attempts in a separate var
                NSLog(@"Good job, you guessed in %ld tries", self.priorGuesses.count);
                gameOn = NO;
            } else if (guessInt > secretNumber) {
                NSLog(@"Too high");
            } else {  // by implication, guessInt < secretNumber
                NSLog(@"Too low");
            }
        } else {
            NSLog((guess)? @"Already guessed\n" : @"Not a number\n");
        }
    }
}