Adding Value in variable (If statement) - ios

I'm trying to figure this out, I have a counting app and want it to increase by 45. Assume the user clicks + 5 times, they can only subtract it -5 for it to equal 0.
Here is my if statement but it doesn't work, it goes into the negatives.
Can anyone help? It's not coming to me. It's the (if count >= 0)
-(IBAction)upButton45:(id)sender {
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
-(IBAction)downButton45:(id)sender {
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
if (countNumber45 <= 0) {
countNumber45 = 0;
xCount1 = 0;
x45Label.text = #"";
}
if (xCount1 >= 0) {
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}

-(IBAction)upButton45:(id)sender
{
if(xCount + 1 <= 45) //Your max allowed tap
{
xCount1 +=1;
countNumber45 +=45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight += 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}
-(IBAction)downButton45:(id)sender
{
if(xCount - 1 >= 0)
{
xCount1 -= 1;
countNumber45 -= 45;
x45Label.text = [NSString stringWithFormat:#"45x%i", xCount1];
totalWeight -= 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", totalWeight];
}
}

-(IBAction)upButton45:(id)sender
{
//Assuming that the lable always hold value and have "0" as start value, and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
currentValue = currentValue + 45;
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}
-(IBAction)downButton45:(id)sender
{
//Assuming that the lable always hold a int value and number is at first position
NSArray *stringList = [TotalWeightLabel.text componentsSeparatedByString:#" "];
int currentValue = [(NSString *)stringList[0] intValue];
if (currentValue != 0) {
currentValue = currentValue - 45;
}
TotalWeightLabel.text = [NSString stringWithFormat:#"%d LBS", currentValue];
}

Related

Create array of negative numbers in Objective C

I have created an array of positive, negative and zero numbers but it treats all the elements in the array as positive numbers. Here in code, the positiveCount is 6. How to put negative numbers in an array in Objective-C?
NSInteger positiveCount = 0;
NSInteger zeroCount = 0;
NSInteger negativeCount = 0;
NSArray *arr = [NSArray arrayWithObjects:#-4,#-3,#-9,#0,#4,#1, nil];
for (NSInteger i = 0; i < arr.count; i++){
NSLog(#"%d",arr[i]);
if (arr[i] > 0)
{
positiveCount += 1;
} else if (arr[i] < 0){
negativeCount += 1;
} else {
zeroCount += 1;
}
}
NSLog(#"%d",positiveCount);
The elements in your array are not numbers, they are NSNumber instances, that is, pointers. Pointers are always positive:
for (NSNumber* number in arr) {
NSInteger intValue = number.integerValue;
NSLog(#"%d", intValue);
if (intValue > 0) {
positiveCount += 1;
} else if (intValue < 0) {
negativeCount += 1;
} else {
zeroCount += 1;
}
}
One more way for the solution using enumerateObjectsUsingBlock,
__block NSInteger positiveCount = 0;
__block NSInteger zeroCount = 0;
__block NSInteger negativeCount = 0;
NSArray *arr = [NSArray arrayWithObjects:#-4,#-3,#-9,#0,#4,#1, nil];
[arr enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
NSInteger value = ((NSNumber *)obj).integerValue;
if (value > 0) {
positiveCount += 1;
} else if (value < 0) {
negativeCount += 1;
} else {
zeroCount += 1;
}
}];
NSLog(#"%ld",(long)positiveCount);

How to compare two numbers and pick the smallest to proceed with execution?

Let say number = 0 and otherNumber = 10. The loop will execute. But what if number = 10 and otherNumber = 0. I want the compiler to pick the smallest number and use it to proceed.
The code I have so far is here:
- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
if (number <= otherNumber) {
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (NSInteger i= number; i <= otherNumber; i++) {
[indices appendFormat:#"%d", i];
}
} else {
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (NSInteger i= otherNumber; i <= number; i++) {
[indices appendFormat:#"%d", i];
}
}
return #"%#", indices;
}
A simple fix is to move the declaration of indices outside the conditional:
- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
if (number <= otherNumber) {
for (NSInteger i= number; i <= otherNumber; i++) {
[indices appendFormat:#"%d", i];
}
} else {
for (NSInteger i= otherNumber; i <= number; i++) {
[indices appendFormat:#"%d", i];
}
}
return indices;
}
You could further unify your like this:
- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
NSInteger from, to;
if (number <= otherNumber) {
from = number;
to = otherNumber;
} else {
from = otherNumber;
to = number;
}
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (NSInteger i= from; i <= tp; i++) {
[indices appendFormat:#"%d", i];
}
return indices;
}
One option would be:
- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
NSInteger minNum = MIN(number, otherNumber);
NSInteger maxNum = MAX(number, otherNumber);
NSMutableString *indices = [NSMutableString stringWithCapacity:maxNum - minNum + 1];
for (NSInteger i = minNum; i <= maxNum; i++) {
[indices appendFormat:#"%d", i];
}
return indices; // or [indices copy];
}
Pick one of the variables to always be considered the larger variable, and then do a simple swap-test before the function body proper.
- (NSString *) stringWithNumbersBetweenNumber:(NSInteger)number andOtherNumber: (NSInteger)otherNumber {
if (number < otherNumber) {
NSInteger temp = number;
number = otherNumber;
otherNumber = temp;
}
NSMutableString *indices = [NSMutableString stringWithCapacity:1];
for (NSInteger i= otherNumber; i <= number; i++) {
[indices appendFormat:#"%d", i];
}
return #"%#", indices;
}
(Apologies for any code errors, Objective-C isn't my primary language)

How to reset countdown timer in spritekit

How to reset countdown timer when the score is divisible by 10?
Here is the score method
-(void)setScore:(int)score
{
_score = score;
scoreLabel.text = [NSString stringWithFormat:#"Score: %d", score];
}
the update method that counts down time
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if (startGamePlay) {
startTime = currentTime;
startGamePlay = NO;
}
int countDownInt = 10.0 - (int)(currentTime - startTime);
timeLabel.text = [NSString stringWithFormat:#"%i", countDownInt];
if (self.score % 10 == 0) {
//reset countDownInt here
}
}

Pick a random number but not the number 3

What would you add to make it so it still picks a random number but not the number 3?
- (IBAction)Button3 {
{
int randomviews = rand() % 6;
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}
Here's another way to do it with just a single call to rand(). Since you're excluding one number from your range, request a smaller range of numbers and then replace any generated 3's with the top number of the previous range:
- (IBAction)Button3 {
{
int randomviews = rand() % 5;
if (randomviews == 3) {
randomviews = 5
}
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}
- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3);
Label1.text = [NSString stringWithFormat:#"%i", randomviews];
}
If you would like to exclude zero as well:
- (IBAction)Button3 {
int randomviews;
do {
randomviews = rand() % 6;
}
while (randomviews == 3 || randomviews == 0);
Label1.text = [NSString stringWithFormat:#"%i", randomviews];

I want the loop of numbers in pyramid pattern by edit form code below

I want to change the following code
NSString* loop = #"";
for (int i = 1; i <= 5; i++)
{
loop = [loop stringByAppendingString:#"0"];
NSLog(#" %#", loop);
}
for (int i = 10; i >= 1; i--)
{
loop = [loop substringFromIndex:1];
NSLog(#" &#", loop);
}
to be result in the pattern of pyramid below. What should I do?
1
101
21012
3210123
432101234
and
0
01
012
0123
01234
012345
01234
0123
012
01
0
For second pattern you can use code:
NSString *loop=#"";
for (int i = 0; i <= 10; i++) {
if (i > 5) {
loop = [loop substringToIndex:[loop length] - 1];
}
else {
loop = [loop stringByAppendingString:[NSString stringWithFormat:#"%i", i]];
}
NSLog (#" %#" , loop);
}
For first
NSString *loop = #"";
NSString *spaces = #" ";
for (int i = 1; i <= 5; i++)
{
if (i==1) {
loop = [loop stringByAppendingString:[NSString stringWithFormat:#"%#%i", [spaces substringFromIndex:i], i - 1]];
} else {
loop = [loop substringFromIndex:6 - i];
loop = [[NSString stringWithFormat:#"%#%i",[spaces substringFromIndex:i], i - 1] stringByAppendingString:[loop stringByAppendingString:[NSString stringWithFormat:#"%i", i - 1]]];
}
NSLog(#" %#", loop);
}

Resources