もう一度プレイしたいかどうかをユーザーに尋ねる最終段階に到達するまで、私のコードが正常に動作する理由を誰か教えてもらえますか? 何らかの理由で、プログラムはこのコード行を無視しているようです。私はプログラミングが初めてで、Objective-C を独学しようとしているので、優しくしてください。このプログラムは初心者向けで、乱数を生成し、ユーザーに推測してもらい、もう一度プレイするかどうか尋ねます。ありがとうございました。
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int randomNumber = arc4random_uniform(100); // This is a random number generator that gens a num betw 0 and 100
int userNumber; // This is the number that the user picks intially
int attempts = 0; // This is the number of attempts the user makes during each game
int games = 0; // This is the number of games the user has played
char play = 'n'; // This is whether the user wants to play again, intially set to 'y'
scanf("%c", &play);
while (play == 'y') {
NSLog(@"Random number is: %d", randomNumber);
NSLog(@"Enter a number between 0 and 100");
scanf("%d", &userNumber);
games++; // Increment the number of games the user has played to 1
if (userNumber == randomNumber) {
attempts++;
NSLog(@"Congratulations. You guessed correctly!");
}
attempts++;
while (userNumber != randomNumber) {
if (userNumber < randomNumber) { // Guess is too low
attempts++; // attempt is incremented
NSLog(@"Too low. Try again!"); // User tries again
scanf("%d", &userNumber);
}
if (userNumber > randomNumber) { // Guess is too high
attempts++; // attempt is incremented
NSLog(@"Too high. Try again!"); // User tries again
scanf("%d", &userNumber);
}
}
NSLog(@"Congratulations. You guessed correctly!");
NSLog(@"It took you %d attempts to guess correctly", attempts);
NSLog(@"Do you want to play again?");
scanf("%c", &play); // --------- Here is where things to wrong ---------
} // while play is yes
} // autoreleasepool
return 0;
} // main