mercredi 6 juillet 2016

Entering an if statement

In this segment of code, from everything I'm seeing, it should be entering the for loop and then the if statement as long as you enter 1's and 0's, which is what I'm doing. It's not entering it, as I've seen from my print statements.

I don't see a reason why.

If it did enter the if statement, I also am unsure what to do because my suspicion is that it will only set true if the last bit is not a 1 or 0: my intention being for zeroesAndOnes to be false if anything except 1's and 0's are entered. However, as it stands, it's false all the time.

System.out.println("Please enter a 32 bit number consisting of "
                    + "1's and 0's.");
String number = kb.nextLine();

boolean zeroesAndOnes = false;
for(int i = 0; i < number.length(); i++) {
    if(number.charAt(i) == 0 || number.charAt(i) == 1) {
            zeroesAndOnes = true;
            System.out.println("If boolean " + zeroesAndOnes);
    }
}
System.out.println("If boolean end " + zeroesAndOnes);

if(number.length() == 32 && zeroesAndOnes == true) {
    if(number.charAt(0) + number.charAt(1) % 2 == 1) {
        symmDiff = 1;
    }
    else{
        symmDiff = 0;
    }
}

Aucun commentaire:

Enregistrer un commentaire