So I have a bunch of input textfields to calculate some stuff. These are doubles. I want that every time its not all filled in, it will display "Please enter all values" Below mentioned code does this for all fields, except the last one, here nitrogen. If i change them around to make another variable last this does not work. Instead it just makes the app give the result when the last field is entered.
So for example, no fields got a value, but when I type something into nitrogen it gives me the result. If I type something else into any of the other fields (Nitrogen blank) i get the correct message "Please enter all values."
How do I make it account for all fields, not just all except the last one?
if (temprature,methane,ethane,propane,nbutane,ibutane,oxygen,npetane,ipetane,nhexane,nitrogen == 0)
{
outputText.text = #"Please enter all values";
} else
{
outputText.text = resultString;
}
Try this, this should work if any of the fields are empty.
if (temprature == 0 || methane == 0 || ethane == 0 || propane == 0 || nbutane == 0 || ibutane == 0 || oxygen == 0 || npetane == 0 || ipetane == 0 || nhexane == 0 || nitrogen == 0)
{
outputText.text = #"Please enter all values";
} else
{
outputText.text = resultString;
}
Related
Im trying to make a while loop that checks to see if a user inputted character is correct. If the character is incorrect, the loop is supposed to run until it is. However, when i enter this loop I cannot exit, even if i input the correct value. Any help would be appreciated!
if (userInputCheck == 'U' || userInputCheck == 'D' || userInputCheck == 'R' || userInputCheck == 'L'){
return userInputCheck;
}
else
while(userInputCheck != 'U' || userInputCheck != 'D' || userInputCheck != 'R' || userInputCheck != 'L'){
System.out.println("ERROR. Please enter U for up, D for down, R for right, or L for left");
String temp = keyboard.next();
userInputCheck = temp.charAt(0);
}
return userInputCheck;
A test like x != 'U' || x != 'D' will ALWAYS be true -- think about it. If the chracter entered is U the first test will be false, but the second will be true, so the whole thing will be true. You want use && instead of ||:
while(userInputCheck != 'U' && userInputCheck != 'D' && ...
Once you do this, there's also no need for the first if...
How can I (more) efficiently write out an if / else conditional that uses the same variable and a huge amount of OR operators?
I've been banging my head on the desk trying to think of a way around writing out all of these OR operators with the same comparison. For loops won't do the trick, and I'm pretty certain that a do / while loop wouldn't do it either. A switch / case might solve this, but I don't think it could condense the code or make it any less tedious to write out.
The example below illustrates my dilemma. The uicollectionview API has a protocol method which is called for each section in the collection and needs a return value for the number of items in that section. Essentially, the protocol method below is a fancy for loop.
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
if (section == 0 || section == 31) return 7;
else if (section == 1 || section == 26 || section == 27 || section == 28 || section == 29 || section == 30) return 6;
else if (section == 2 || section == 3 || section == 4 || section == 5 || section == 6 || section == 7 || section == 8 || section == 9 || section == 10 || section == 11 || section == 12 || section == 13 || section == 14 || section == 15) return 2;
else if (section == 16 || section == 17 || section == 18 || section == 19 || section == 20 || section == 21 || section == 22 || section == 23 || section == 24 || section == 25) return 4;
else return 7;
}
As you can see in the extremely tedious if / else conditional I've written, it checks for every single section. I feel like I'm missing something - that Objective-C provides a nifty way around this kind of tedious and inefficient conditional writing / checking?
A clean way is to use a switch statement. These are usually implemented with a branching array, so are quite efficient.
switch (section)
{
case 0:
case 31:
return 7;
...
}
And the most efficient of all (branchless) is a lookup-table:
const int nums[]= { 7, 6, 2, 2, 2... };
return nums[section];
switch is probably the right answer, but you can also use shift/mask, something like:
int sectionBit = 0x00000001 << section;
if (sectionBit & 0x80000001) return 7;
if (sectionBit & 0x7c000002) return 6;
etc
The disadvantage of this approach is that it's tedious and error-prone to define the bit constants, but one can sometimes develop the constants by using enums, etc (at the expense of long declaration strings).
A generic approach to this is to use NSArray / NSDictionary to build indexes, something like:
NSArray *sections = #[#3, #5, #27, ...];
if ([sections containsObject:#3]) ...
or
NSDictionary *sections = #{ #5: #27, #7: #23, ... };
int value = [sections[#(section)] intValue]
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.
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.
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.");
}