samedi 28 novembre 2015

Print Line if output is greater than a certain number

I want to print a line of text into a label if the output is past a certain number (in this case 6). I was thinking of using a if, else function but I cant quite figure it out.

Any tips?

Switch Statement and If Statement

Why switch case does not take multiple lines where if do work perfectly

if(indexPath.row == 1){
            UIViewController *viewController = [UIViewController new];
            viewController.view.backgroundColor = [UIColor whiteColor];
            viewController.title = _titlesArray[indexPath.row];
            [self.navigationController pushViewController:viewControlleranimated:YES];
}

same code unable to use in switch case statement

            switch (indexPath.row) {
                case 0:
                    UIViewController *viewController = [UIViewController new];
                    viewController.view.backgroundColor = [UIColor whiteColor];
                    viewController.title = _titlesArray[indexPath.row];
                     [self.navigationController pushViewController:viewControlleranimated:YES];
    break;
                    case 1:
                    break;

                    default
                     break:
}

In switch statement its gives an errors.... I forcibly using if else statement instated of switch case. Can any one advice me how use switch case statment.

foreach and if statement to display custom taxonomies

I am trying to display custom taxonomies but if there are none to display a message.

Here is as far as I have gotten:

<?php
    $terms = get_the_terms( $post->ID , 'machine-features' );
    foreach ( $terms as $term ) {
        if ($term) {
            echo '<li>' . $term->name . '</li>';
        } else {
            echo "Contact us for details";
        }
    }
?>  

It works perfectly when there are features added but when empty I get the following error: Warning: Invalid argument supplied for foreach()

I'm new to php so apologies if this is a crazy basic question - I searched for an answer and tried various permutations but nothing worked!

Thanks a million for any help! O

What is called if 2 bools are true in if/else statements

In a normal if/else statement if one bool, for example, is true it calls that statement e.g:

bool1 = true;
bool2 = false;
bool3 = false;

if(bool1){
    DoSomething();
}else if(bool2){
    DoSomethingElse();
}else{
    DoSomethingHelpful();
}

Of course, in this example DoSomething() will be called.

But what if 2 or 3 of the bools were equal to true, e.g:

bool1 = true;
bool2 = true;
bool3 = false;

if(bool1){
    DoSomething();
}else if(bool2){
    DoSomethingElse();
}else{
    DoSomethingHelpful();
}

What statement will be called? Would it be DoSomething() because its the first statement read by the compiler?Or would it just return with an error

vendredi 27 novembre 2015

Problems with deal or no deal java game

The program is supposed to display a message on whether you have made a good deal or not. It doesn't display anything and I know the problem is something with my "message2" but I can't figure out what it is. I don't know how to get the message to display depending on the deal that the player has made. I have included the relative part of my code.

 public void deal()
{
    dealButton.setEnabled(false);
    noDealButton.setEnabled(false);

    String message = "You have chosen to accept the banker's offer of " + currency.format(bankerOffer) + " \nBut was it a good deal?\nIf we open your case we reveal";
    String message2 = "";


    for(int i = 0; i < unshuffledAmounts.size(); i++)
    {
        if(unshuffledAmounts.get(i).equals(yourCaseAmount))
        {
            yourCaseNumber = i;
        }
    }

    if(bankerOffer > yourCaseAmount)
    {
         message2 = "You made a good deal";
    }
    else
    {
        message2 = "Too bad, you made a bad deal";
    }

How to setup if-statement with multiple conditions, which uses the valid condition's variable in the if-statement?

Okay, that title will sound a bit crazy. I have an object, which I build from a bunch of inputs (from the user). I set them according to their value received, but sometimes they are not set at all, which makes them null. What I really want to do, it make an item generator for WoW. The items can have multiple attributes, which all look the same to the user. Here is my example:

+3 Agility
+5 Stamina
+10 Dodge

In theory, that should just grab my object's property name and key value, then output it in the same fashion. However, how do I setup that if-statement?

Here is what my current if-statement MADNESS looks like:

if(property == "agility") {
    text = "+" + text + " Agility";
}
if(property == "stamina") {
    text = "+" + text + " Stamina";
}
if(property == "dodge") {
    text = "+" + text + " Dodge";
}

You get that point right? In WoW there are A TON of attributes, so it would suck that I would have to create an if-statement for each, because there are simply too many. It's basically repeating itself, but still using the property name all the way. Here is what my JSFiddle looks like: http://ift.tt/1TclvHz so you can play with it yourself. Thanks!

EDIT: Oh by the way, what I want to do is something like this:

if(property == "agility" || property == "stamina" || ....) {
    text = "+" + text + " " + THE_ABOVE_VARIABLE_WHICH_IS_TRUE;
}

Which is hacky as well. I definitely don't want that.

if statement in python won't run even though conditions are true

made a program to simulate how life began on Earth in it's simplest form; the bigger number in the array (species[]) eats a smaller number beside it in the array and makes that number smaller. I have made an if statement which says if num > 0 & length > 1, then do whatever where num is the number of cycles the program runs and length is the length of the array, species[]. I have tested both variables and they are both more than 1. Even still, the if statement isn't running, the else is making it break out of the while loop. Any ideas on how could fix it? please explain in a simple way, I just started coding recently. Heres my code:

# Import Packages
import random

# Author
__author__ = 'VectorImage'

# Defaults
print('DEFAULTS')
print('Starting Size = 10')
print('Chance Of New Species = 1')
print('New Species Size = 5')
print('Number of Cycles = 100')
print('\n\n');


# Variables
print('SET VARIABLES')
choice = input('CUSTOM or DEFAULT: ')
p3 = 11

while p3 > 10:
    if choice == 'CUSTOM':
        p1 = int(input('Starting Size: '))
        p2 = int(input('Chance Of New Species (lower number means higher chance): '))-1
        p3 = int(input('New Species Size: '))-1
        p4 = int(input('Number of Cycles: '))
    elif choice != 'CUSTOM':
        p1 = 10
        p2 = 0
        p3 = 5
        p4 = 100
    else:
        print('species size cannot be more than x10')
species = [p1, p1, p1]
length = None
l = None
new_species = None
chance = None
num_range = None
temp_num = None
num = None
print('\n\n')


# Program
def main():
    print('PROGRAM')
    length = len(species)
    if length > 2:
        l = 0
        num = p4
        while 1 < 2:
            print(species)
            if num > 0 & length > 1:
                length = len(species)
                num_range = int(round(random.random()*(p3+1)))
                new_species = int(round(random.random()*p2))
                chance = int(round(random.random()))
                if new_species == 0:
                    if chance == 0:
                        species.insert(len(species) + num_range, length)
                    else:
                        species.insert(len(species) - num_range, length)
                l += 1
                num -= 1
                print('Cycle #', p4-num)
                print(length, ' species')
            else:
                break
            if species[length-1] > species[length-2]:
                temp_num = species[length-1] - num_range * (1 + p3)
                species[length-2] -= temp_num
                species[length-1] += temp_num
            else:
                temp_num = species[length-1] - (num_range * (1 + p3))
                species[length-1] += temp_num
                species[length-2] -= temp_num
            if species[length-1] <= 0:
                del species[length-1]
            elif species[length-2] <= 0:
                del species[length-2]

# RUN
main()