// // main.m // 猜數 // #import#import Guess.h int main(int argc, const char * argv[]) { Guess * number = [[Guess alloc]init]; NSLog(@請輸入您所猜的數字); int guessNumber; scanf(%d,&guessNumber); [number guessNumber:guessNumber]; //NSLog(@%d,number->_guessNumber); return 0; }
#import@interface Guess : NSObject { @public int _guessNumber; } -(void)guessNumber:(int)number; @end
#import Guess.h @implementation Guess -(id)init { self = [super init]; if (self != nil) { _guessNumber = 100; } return self; } -(void)guessNumber:(int)number { //NSLog(@請輸入您所猜的數字); if(number==_guessNumber) { NSLog(@您猜對了); } else if(number > _guessNumber) { NSLog(@您猜的大了); } else { NSLog(@您猜的小了); } } @end