如何在java中的while循环中使用if语句?

时间:2014-07-12 11:24:36

标签: java loops if-statement while-loop binary

我有一个二进制字符串(010101011101010001),我希望有一个if语句检查第一个字符并执行一个命令,如打印“this is a 1”是字符是一个。

到目前为止我使用的代码就是这个。

    b = "010101011101010001";
    i = 0;

    System.out.println("length of b is " + b.length());

    do{

        s = b.charAt(i);
        System.out.println("s is " + s);
        i++;
        System.out.println("i is " + i);

        if (Character.toString(s) == "0"){

            System.out.println("if 1");

        }else if (Character.toString(s) == "1"){

            System.out.println("if 2");

        }
    }while(i != b.length());

我离开了。

length of b is 18
s is 0
i is 1
s is 1
i is 2
s is 0
i is 3
s is 1
i is 4
s is 0
i is 5
s is 1
i is 6
s is 0
i is 7
s is 1
i is 8
s is 1
i is 9
s is 1
i is 10
s is 0
i is 11
s is 1
i is 12
s is 0
i is 13
s is 1
i is 14
s is 0
i is 15
s is 0
i is 16
s is 0
i is 17
s is 1
i is 18

0 个答案:

没有答案