0

好的,所以我对目标 C 是全新的,为了给自己一点练习,我做了一个小程序。这些程序假设计算出你有多少天。我有这个我无法弄清楚的错误。继承人的代码:

年龄.h

#import <Foundation/Foundation.h>

@interface Age : NSObject {
int age;
int age365;
int lyear;
}
-(void) howOld;


@end

年龄.m

#import "Age.h"

@implementation Age


-(void) howOld{
NSLog(@"How old are you currently?\n");
scanf("age");
NSLog(@"You entered %i", age);



}


@end

月.h

#import "Age.h"

@interface Month : Age{
int month;
}

-(void) whatMonth;

@end

月.m

#import "Month.h"

@implementation Month

-(void) whatMonth{

NSLog(@"What month is it? (Number)\n");
scanf("month");
NSLog(@"you entered %i", month);



}


@end

天.h

#import "Month.h"

@interface Day : Month{
int day;
}
-(void) whatDay;
-(void) printAge;
@end

天.m

#import "Day.h"

@implementation Day

-(void) whatDay{
NSLog(@"What day is it? (Number)");
scanf("day");
NSLog(@"You entered %i", day);
day = month*30+day+age365;

}

-(void) printAge{
age365 = age * 365;
lyear = age/4;
age = lyear + age365;
NSLog(@"You are %i days old", day);
}

@end

主程序

#import <Foundation/Foundation.h>
#import "Age.h"
#import "Month.h"
#import "Day.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {

    Age *a = [[Age alloc]init];

    [a howOld];


    Month *m = [[Month alloc]init];

    [m whatMonth];

    Day *d = [[Day alloc]init];

    [d whatDay];
    [d printAge];


}
return 0;
}

输出 *这是错误*

**Lets me type**(
2013-08-08 17:27:26.121 HMDAI[10989:303] How old are you currently?
3)

**Does by itself**(
2013-08-08 17:27:43.854 HMDAI[10989:303] You entered 0
2013-08-08 17:27:43.854 HMDAI[10989:303] What month is it? (Number)
2013-08-08 17:27:43.855 HMDAI[10989:303] you entered 0
2013-08-08 17:27:43.855 HMDAI[10989:303] What day is it? (Number)
2013-08-08 17:27:43.855 HMDAI[10989:303] You entered 0
2013-08-08 17:27:43.856 HMDAI[10989:303] You are 0 days old
)

我做错了什么?如果有帮助,我会使用 XCode。

4

1 回答 1

1

scanf表示从标准输入扫描格式化字符串。该函数采用格式字符串和一组指针来扫描内容。

您的代码失败了,因为您没有告诉scanf()它应该扫描一个数字。

维基百科的文章实际上信息量很大: http ://en.wikipedia.org/wiki/Scanf_format_string

(而且比我们这些大胡子赖以生存和死亡的典型手册页要少得多)。


请注意,如果您的目标是为了 Mac OS X 或 iOS 编程而学习 ObjC,那么您选择了一个不错但奇怪的起点。您最好从众多如何编写应用程序教程之一开始。

于 2013-08-09T00:46:50.290 回答