本帖最后由 Silent_memory 于 2014-3-20 20:50 编辑
- 第一题
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
-
- NSString *num1 = @"342";
- NSString *num2 = @"17";
-
- int a = [num1 intValue]; //intValue函数将字符串转int
- int b = [num2 intValue];
- int c = a - b;
- //stringWithFormat 将输入的参数转字符串格式
- NSString *result = [NSString stringWithFormat:@"%d",c];
- NSLog(@"两个数的差 %@",result);
-
- }
- return 0;
- }
复制代码- 第二题
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
-
- NSString *str = @"www-it-heima-com";
- /*- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement
- 函数功能为将字符串中的指定字符替换
- */
- NSString *newStr = [str stringByReplacingOccurrencesOfString:@"-" withString:@"."];
- NSLog(@"%@",newStr);
-
- }
- return 0;
- }
复制代码- #import <Foundation/Foundation.h>
- int compareNum(int num1,int num2);
- int main(int argc, const char * argv[])
- {
-
- @autoreleasepool {
-
- int flag;
- int randomNum = arc4random() % 10; //随机生成0 ~ 10 之间的随机数
- do { //做第一次输出
- int x;
- NSLog(@"请输入一个数");
- scanf("%d",&x);
- flag = compareNum(x,randomNum); //判断输入数与随机数的大小关系
- } while (flag < 1);
-
- }
- return 0;
- }
- int compareNum(int num1,int num2)
- {
- if (num1 < num2) {
- NSLog(@"输入的数小了");
- return -1;
- }else if(num1 > num2) {
- NSLog(@"输入的数大了");
- return 0;
- }else{
- NSLog(@"Bing Go,你猜对了");
- return 1;
- }
- return 0;
- }
复制代码
|