我正在試着製作一個計算器。一切工作正常,但現在我想添加一個小數點,我不知道如何。你能幫忙嗎?謝謝。爲我的計算器添加小數點
我想這
- (IBAction) buttondecimal {
SelectNumber = SelectNumber * 10;
SelectNumber = SelectNumber + @".";
Screen.text = [NSString stringWithFormat: @"%0.f", SelectNumber]; }
,但我得到一個錯誤:無效的操作數爲二進制表達式( '雙' 和 '的NSString *')
這裏是我的代碼:
.h
#import <UIKit/UIKit.h>
int Method;
double SelectNumber;
double RunningTotal;
@interface KalkulackaViewController : UIViewController
{
IBOutlet UILabel *deglabel;
IBOutlet UIButton *degbutton;
IBOutlet UITextField *Screen;
}
-(IBAction)Number1:(id)sender;
-(IBAction)Nasobeni:(id)sender;
-(IBAction)Deleni:(id)sender;
-(IBAction)Minus:(id)sender;
-(IBAction)Plus:(id)sender;
-(IBAction)Rovnase:(id)sender;
-(IBAction)AllClear:(id)sender;
@end
.M
#import "KalkulackaViewController.h"
@interface KalkulackaViewController()
@end
@implementation KalkulackaViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10,10)];
Screen.rightView = paddingView;
Screen.rightViewMode = UITextFieldViewModeAlways;
Screen.leftView = paddingView;
Screen.leftViewMode = UITextFieldViewModeAlways;
}
-(IBAction)Number1:(id)sender
{
SelectNumber = SelectNumber * 10;
SelectNumber = SelectNumber + 1;
Screen.text = [NSString stringWithFormat:@"%0.f", SelectNumber];
}
- (IBAction) buttondecimal
{
}
THX對您有所幫助,但如果我使用的號碼添加您的代碼(例如Screen.text = [Screen.text stringByAppendingString:@ 「1」]; ),計算器停止工作,因爲它與SelectNumber一起工作:/ – Alien 2014-10-18 18:56:09
那麼問題是您需要改變您創建計算器的方式,而不是使用「SelectNumber」。所有3個答案都說。 – rebello95 2014-10-18 18:57:03
那麼,我會按照你的意見;-) – Alien 2014-10-18 19:16:30