I have this code:
if ((total == (total1 && total2 && total3)))
{
[scrollview.contentOffset = CGPointMake (0,0)];
}
here is what it's something like on button action:
if (sender.tag == 1)
{
total1 = 10;
}
if (sender.tag == 2)
{
total2 = 20;
}
if (sender.tag == 3)
{
total3 = 30;
}
I am trying to go back to the start page of the scroll view if the user clicked the three correct buttons (similar to a password key).
Does the logical operator && work well in Objective-C, and did I use it right?
if ((total == (total1 && total2 && total3)))
You cannot do that. You have to explicitly compare each separately.
if ((total == total1) && (total == total2) && (total == total3)))
But that leaves the question of how total can be equal to all the three simultaneously though.
In your code:
if ((total == (total1 && total2 && total3)))
{
[scrollview.contentOffset = CGPointMake (0,0)];
}
When the if expression is evaluated, (total1 && total2 && total3) is evaluated first. And that can be either YES or NO (true or false if you prefer), or (0 or 1).
So your code is equivalent to the following:
BOOL allVariablesAreNotZero = total1 && total2 && total3;
if (total == allVariablesAreNotZero)
{
[scrollview.contentOffset = CGPointMake (0,0)];
}
Edit after the question was better explained
Make your buttons perform the following action when pressed:
- (void)buttonClicked:(id)sender
{
UIButton *button = (UIButton *)sender;
buttonsCombination = buttonsCombination | (1 << button.tag);
}
Where buttonsCombination is an NSUInteger. Then use the following test to see if the buttons that were pressed are the correct ones (I am doing this with three buttons, but you guess the idea)
NSUInteger correctCombination = (1 << button1) | (1 << button2) | (1 << button3)
if (buttonsCombination == correctCombination) {
// The combination is correct
} else {
// The combination is incorrect
}
buttonsCombination = 0;
Finally, note that this works because there are enough bits in a NSUInteger for 30 buttons.
Here I used bitwise operators | and <<.
What your current code is essentially saying is "if total is 'true' and total1, total2, and total3 are also all nonzero or if total is zero and total1, total2, and total3 are also all zero, then do something".
The && you have there is doing a logical/boolean comparison. It treats its arguments as being either true or false, and returns true if both arguments evaluate to true and false in any other case. The == compares the value of total with the true or false value that was obtained from your && expressions. That is probably not what you want here.
It seems like probably what you want to be saying is "if total is equal to the sum of total1, total2, and total3, then do something". Assuming this is the case, you would do:
if (total == (total1 + total2 + total3)) {
[scrollview.contentOffset = CGPointMake (0,0)];
}
Trying to determine what you mean by your comment on two other answers "i tried it but it executes the codes when i started to run the app" maybe this is what you're trying to achieve:
/* all in your button handler */
switch(sender.tag)
{
case 1:
total1 = 10;
break;
case 2:
total2 = 20;
break;
case 3:
total3 = 30;
break;
default:
break; // other buttons are ignored
}
// check it latest click means the total is now correct
if((total1 + total2 + total3) == total)
{
[scrollview.contentOffset = CGPointMake (0,0)];
}
So you update any totalX's effected by the button click and then check the condition to reset the scrolling.
Related
I have tried many thing involving this, >=, >==, =>, ==>.i can not find one that works. hey all return either primary expression needed or expected initializer before '>'. I am creating a IR receiver latch switch and thus have to create parameters for the code because the receiver is not constant in all conditions. Full code below. Any suggestions to fix the code please reply and don't DM me. Thank you.
code:
int LEDState = 0;
int LEDPin = 8;
int dt = 100;
int recieverOld ==> 500 and recieverOld ==< 2000;
int recieverNew;
int recieverPin = 12;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(LEDPin, OUTPUT);
pinMode(recieverPin, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
recieverNew = digitalRead(recieverPin);
if((recieverOld >== 0 && recieverOld <== 10) && (recieverNew >== 500 && recieverNew <== 2000) {
if(LEDState == 0) {
digitalWrite(LEDPin, HIGH);
LEDState = 1;
}
}
recieverOld = recieverNew;
delay(dt);
}
error:
expected initializer before '==' token
if one = used line 4 and related, return error expected primary-expression before '>' token
if > before = line 4 and related, return error expected initializer before '>=' token
Any solutions or suggestions welcome.
TL;DR
Operators that do no exist, and that you should NOT use:
==>, ==<, >==, <==
Operators that works and you can use them:
>= - MORE THAN OR EQUAL, compare operator, for example X >= 5
<= - LESS THAN OR EQUAL, compare operator, for example X <= 5
> - MORE THAN, compare operator, for example X > 5
< - LESS THAN, compare operator, for example X < 5
== - compare operator, when you want to compare values of the variables if they have the same value, for example X == 5, Y == X, 10 == 7
=== - equality operator, similar to compare operator ==, but aditionally checks the type of a variable. for example X === Y, '10' === 10
= - assign operator, when you want to assign something to the variable, for example X = 5
<> OR != - NOT EQUAL, compare operator, for example X != 5, Y <> 10
!== - similar to != or <>, but also checks the type of a value. For example 10 !== '10', and will return opposite result of the equality operator ===
I've created a TTT game. However I'm having trouble with the AI taking spots. How do I avoid this from happening so that in the chooseCellRow:Col method it only returns a random spot that has not already been selected by either player?
- (void) chooseCellRow:(NSInteger)row Col:(NSInteger)col
{
//pick random spot for computer's turn
row = random()%2;
col = random()%2;
if (row == 0 && col == 0) {
[but00 setTitle:#"X" forState:0];
}
else if (row == 0 && col == 1) {
[but01 setTitle:#"X" forState:0];
}
else if (row == 0 && col == 2) {
[but02 setTitle:#"X" forState:0];
}
else if (row == 1 && col == 0) {
[but10 setTitle:#"X" forState:0];
}
else if (row == 1 && col == 1) {
[but11 setTitle:#"X" forState:0];
}
else if (row == 1 && col == 2) {
[but12 setTitle:#"X" forState:0];
}
else if (row == 2 && col == 0) {
[but20 setTitle:#"X" forState:0];
}
else if (row == 2 && col == 1) {
[but21 setTitle:#"X" forState:0];
}
else if (row == 2 && col == 2) {
[but22 setTitle:#"X" forState:0];
}
}
Cheating is a moral concept, one which doesn't apply to the computer since it's doing exactly what you've told it to do. However, you could prevent this by simply choosing again if the cell is already occupied, pseudo-code:
row = random() % 3;
col = random() % 3;
while cell[row][col] != empty: # Add your REAL detection code here.
row = random() % 3;
col = random() % 3;
You'll also notice that the above code stops you from cheating as well. Shame on you for only allowing the computer to choose four of the nine possibilities :-)
Applying % 2 to a number will give you 0 or 1, you need to use % 3 to allow for 2 as well.
Based on your comments that you have a cell array which contains the character occupying that cell, the code to continuse until you find a blank cell would be along the lines of:
do {
row = random() % 3;
col = random() % 3;
} while (cell[row][col] != ' ');
One easy way would be to keep a list of unused positions and choose from that list. When either player moves, remove the position they choose from the list.
Another, even simpler but less efficient way is to have it choose any position, but try again if the spot it picks is already occupied.
Guys what am I doing wrong?
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
}
else if (numberstring.intValue >16 && <=40){
rankLabel.text = #"A2";
}
I get an error on the "<=40" ..
You missed off a variable reference:
if (numberstring.intValue <=15) {
rankLabel.text = #"A1";
} // vv here vv
else if (numberstring.intValue >16 && numberstring.intValue <= 40){
rankLabel.text = #"A2";
}
As an optional extra, it looks like numberstring is an NSString object, which you are repeatedly converting to an integer in order to test various ranges. That operation is quite expensive, so you are better off doing the conversion once:
int value = [numberstring intValue];
if (value <=15) {
rankLabel.text = #"A1";
}
else if (value >16 && value <= 40){
rankLabel.text = #"A2";
}
Also note that the intValue method is not a property so I would avoid using the Objective-C 2.0 dot syntax to access it and use the normal method calling mechanism.
The && operator links two clauses together. However, each clause is independent, so each one has to be syntactically correct on its own if the other was removed. If you apply this rule to your condition, you can see that "<=40" is not syntactically correct on its own. Thus you need to reference the value being compared, as follows:
if (numberstring.intValue > 16 &&
numberstring.intValue <= 40) // this is syntactically correct on its own
I am using a NSStepper along with a NSTextField. The user can either set the value using the text field or can use the NSStepper to change the value. I will quote my question using the example below:
Suppose the current value of my stepper is 4 and increment value of the stepper is 2:
After I click the UP arrow on the NSStepper the value becomes:
Now Suppose the current value would have been 4.5 i.e.:
After using the UP arrow the value becomes:
What I require is that when the current value is 4.5, after using the UP arrow, the value becomes 6 instead of 6.5
Any ideas to accomplish this are highly appreciated!
What I require is that when the current value is 4.5, after using the
UP arrow, the value becomes 6 instead of 6.5
Hard to tell exactly what you are asking but taking a guess: it sounds like you want to remove the decimal part of the number and increment by your defined step amount (2). You can do this through the floor() function. See here for other Objective-C math functions
double floor ( double ) - removes the decimal part of the argument
NSLog(#"res: %.f", floor(3.000000000001));
//result 3
NSLog(#"res:%.f", floor(3.9999999));
//result 3
If I understand what you want, this code will give you the next even number (up or down depending on which arrow you click), but still allow you to enter non-integer numbers in the text filed. tf and stepper are IBOutlets and num is a property (a float) that keeps track of the value of the stepper before you click an arrow so you can compare with the new number to see if the up or down arrow was clicked.
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
self.num = 0;
self.tf.intValue = 0; //the stepper is set to 0 in IB
}
-(IBAction)textFieldDidChange:(id)sender {
self.num = self.stepper.floatValue = [sender floatValue];
}
-(IBAction)stepperDidChange:(id)sender {
if (self.num < self.stepper.floatValue) { //determines whether the up or down arrow was clicked
self.num = self.stepper.intValue = self.tf.intValue = [self nextLargerEven:self.num];
}else{
self.num = self.stepper.intValue = self.tf.intValue =[self nextSmallerEven:self.num];
}
}
-(int)nextLargerEven:(float) previousValue {
if ((int)previousValue % 2 == 0) {
return (int)previousValue + 2;
}else
return (int)previousValue + 1;
}
-(int)nextSmallerEven:(float) previousValue {
if ((int)previousValue % 2 == 0) {
if ((int)previousValue == previousValue) {
return (int)previousValue - 2;
}else{
return (int)previousValue;
}
}else
return (int)previousValue - 1;
}
int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
scanf(" %i", &side1test);
Returns "0" when the user enters a "y." However,
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}
Then does not catch.
The program drops into my else clause, and outputs all the NSLogs, skipping the scanf() commands, taking each of them as "0." What is wrong here?
I'm not a c++ dev but from googling that function returns the number of valid matches. If it returns 0 you should assume invalid input. side1test has not been set which is why it's 0.
Your code should probably be:--
int side1test;
NSLog(#"Is your triangle setup as in an Angle-Side-Angle? (Use 1 for Yes and 0 for No.)");
int result = 0;
while (result==0)
{
result =scanf(" %i", &side1test);
}
if (side1test != 1 && side1test != 0){
NSLog(#"Please use a '1' for YES and '0' for NO.");
}