简体   繁体   中英

Can't Assign arrays values with a loop in java

I am trying to recreate the .toBinaryString() function in java, because I have a lot of time on my hands. This is what I have so far:

   class Bitwise
   {
           public static void main(String args[])
           {
                   int a = 5;
                   int c = 0;
                   int d = 2;
                   String[] aray = new String[8];
                   int a2 = 7;

                  for(int ef = 1; ef > 128; ef = pwrsd(2, c))
                  {
                          String a1 = (((a & ef)> 0 ? "1" : "0"));
                          aray[a2] = a1;
                          a2 = a2 - 1;

                          c = c + 1;
                  }
                  for(int as=0; as < 8; as ++)
                  {
                          System.out.print(aray[as]);
                  }
          }

          static int pwrsd(int numto, int pwrsds)
          {
                  int ca = numto;
                  for(int cd = 1; cd < (pwrsds); cd ++)
                  {
                          ca = ca * numto;
                  }

                  return ca;
          }
}

I made the array because if I just did the normal power of two's it prints the number backwards. And now, when I run it, it prints Null 8 times, like i didn't assign each array variable, a number, which I did, in the first for loop. Did I do something wrong with the array? sorry for the weird function and variable names and please don't tell me that i'm wasting my time because there already is a .toBinaryString() function.

for(int ef = 1; ef > 128

Since ef is initialized to 1, it's not > 128 , so the loop is thus never executed.

You should learn to use your debugger and execute the code step by step. You would have found that in 2 seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM