mardi 1 septembre 2015

Combining if-then statements

I'm trying to convert individual integers to from their ASCII values to 0s and 1s. The relevant code looks something like this:

    int num1 = bin.charAt(0);
    int num2 = bin.charAt(1);
    int num3 = bin.charAt(2);
    int num4 = bin.charAt(3);

    if (num1 == 49) 
    {
        num1 = 1;
    }
    else
    {
        num1 = 0;
    }

    if (num2 == 49) 
    {num2 = 1;}
    else
    {
        num2 = 0;
    }

Is there a way to combine if/else statements so that I don't have 30 lines of code of the same principle?

I tried if (num1 == 49 || num2 ==49) but I'm not sure how to only assign the value that is equal to 49 to 0.

Aucun commentaire:

Enregistrer un commentaire