1

我是 Objective C 的新手,我需要在 5 次循环内完成一个基本的数学应用程序。用户需要说明将执行什么操作(+、-、*、/),程序将生成两个随机数来进行数学运算。然后用户将进行数学运算,程序会将他的输入与正确答案进行比较。最后,用户将根据正确或错误答案收到分数和自定义消息。我有点卡住了。首先,我做了一个生成两个随机数并运行良好的程序。但是,当我添加其余代码时,我收到一条警告消息,指出实现不完整。我也无法看到案例中的 else 结构发生了什么,因为我收到了每个错误都表明“预期表达式”的错误。很感谢任何形式的帮助。这是我的代码:

#import <Foundation/Foundation.h>
@interface myMath : NSObject  
  //methods     
  -(int) getRandomNumber;
  -(void) add: (double) result;
  -(void) subtract: (double) result;
  -(void) multiply: (double) result;
  -(void) divide: (double) result;
@end  
@implementation myMath
  //returns a random number between 1 and 100     
  -(int) getRandomNumber{         
  return (arc4random()%(100-1))+1;     
 }
@end  
@interface Calculator : NSObject   
   @property double setaccumulator, accumulator; //Synt all methods.    
@end    
@implementation Calculator{     
    double accumulator;  
  }  
  -(void) setAccumulator: (double) value;
  {     accumulator = value;  }  
  -(void) clear  
  {      accumulator = 0;  }  
  -(double) accumulator  
  {     return accumulator;  }  
  -(void) add: (double) value  
  {     accumulator += value;  }  
  -(void) subtract: (double) value  
  {     accumulator -= value;  }  
  -(void) multiply: (double) value  
  {     accumulator *= value;  }  
  -(void) divide: (double) value  
  {     accumulator /= value;  }  
@end  
int main(int argc, const char * argv[]) {     

@autoreleasepool {         
  myMath *myMathStuff;         
  myMathStuff = [[myMath alloc] init];         

  int rnum1 = [myMathStuff getRandomNumber];         
  int rnum2 = [myMathStuff getRandomNumber];          
  double result;          
  char operator;               

  Calculator *myCalculator = [[Calculator alloc]init];          
  int n, right, wrong, cont;          
  n = 0, right, wrong, cont = 0;          

  NSLog(@"The random numbers are %i and %i", rnum1, rnum2);
  while (n <= 5){                    
    NSLog(@"What operation do you want to perform? (+ . - . * . / .");
      scanf(" %c", &operator);
      [myCalculator setAccumulator:(double) rnum1];

      switch (operator) {                                
         case '+':                  
           [myCalculator add: rnum2];                  
           NSLog(@"Please type the addition result:      ");
           scanf("%lf", &result);                  

           if (result == rnum1)                     
             right =+1;                      
             NSLog(@"Congratulations, you did it right!");
              else                       
                wrong =+1;                      
                NSLog(@"Sorry, the addition result is: %.2f", [myCalculator accumulator]);
           break;
         case '-':                  
           [myCalculator subtract: rnum2]
              NSLog(@"Please type the subtraction:      ");
              scanf("%lf", &result);                  
              if (result == rnum1)                      
                right =+1;                  
                   NSLog(@"Congratulations, you did it right!");                  
              else                      
                wrong =+1,                      
                NSLog(@"Sorry, the subtraction result is: %.2f", [myCalculator accumulator]);                  
             break;                                
          case '*':                  
             [myCalculator multiply: rnum2];                  
             NSLog(@"Please type the multiplication result:      ");
             scanf("%lf", &result);                  
             if (result == rnum1)                   
               right =+1;                  
               NSLog(@"Congratulations, you did it right!");                  
             else                      
               wrong =+1,                      
               NSLog(@"Sorry, the multiplication result is: %.2f", [myCalculator accumulator]);                  
             break;              
             case '/':                  
               [myCalculator divide: rnum2];
              NSLog(@"Please type the division result:      "); 
              scanf("%lf", &result);                  
              if (result == rnum1)                      
                right =+1;                  
                NSLog(@"Congratulations, you did it right!");                  
              else                      
                wrong =+1,                      
                NSLog(@"Sorry, the division result is: %.2f", [myCalculator accumulator]);                  
              break;              
          default:                  
         break;          
        }          
   ++n;             
 }        
 NSLog(@"You were right %i", right);      
 NSLog(@"You were wrong  %i", wrong);      
 if (right == 4)              
   NSLog(@"Excellent you have a perfect 100 percent score");    
 else if (right == 3)          
   NSLog(@"Good job you have a 80 percent score");      
 else if (right == 2)       
   NSLog (@"Well, you have a 60 percent score");      
 else if (right ==1)          
   NSLog(@"Sorry, you have received a 20 percent score");  
 }      
 return 0;  
}

我对 myMath 实现进行了一些更改,我还更改了“else”出现的行,如下所示。我仍然得到预期的表达。如果其他人可以看到我没有看到的内容,我将不胜感激您的意见。我更改的部分代码: switch (operator) { case '+':

                [myMathStuff add: rnum2];                    
                NSLog(@"Please type the addition result:      ");                    
                scanf("%lf", &result);                    
                if (result == rnum1)

                    right =+1;
                    NSLog (@"Congratulations, you did it right!");

                else {

                    wrong =+1;
                    NSLog (@"Sorry, the addition result is: %.2f", [myMathStuff accumulator]);
                }

                break;

我能够找到解决方案。

4

1 回答 1

2

你得到implementation is incomplete是因为在你的MyMath类中你说你在类级别有 5 个方法,而实际上只有一个(GetRandomNumber)被实现。

您需要完成合同 - 即 - 编写出其他四种方法将要执行的操作(加、减、乘、除)。

于 2013-10-19T04:11:21.497 回答