我正嘗試在Java中重新創建.toBinaryString()函數,因爲我手上有很多時間。這是我到目前爲止:無法在java中使用循環分配數組值
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;
}
}
我做了陣列,因爲如果我只是做了兩個正常的力量它向後打印數字。而現在,當我運行它時,它會在第一個for循環中打印Null 8次,就像我沒有爲每個數組變量分配一個數字一樣。我有沒有做錯陣列?對不起,奇怪的函數和變量名稱,請不要告訴我,我正在浪費我的時間,因爲已經有一個.toBinaryString()函數。
「選擇有意義的名稱來描述該變量的用途,避免使用通用名稱,如數字或目的不明確的臨時數據」。另請參見[Java編程風格指南,變量命名約定](https://www.cwu .edu /〜gellenbe/javastyle/variable.html) –
在我的情況下,for循環中的大多數變量都表示比迭代次數多的變量。 – Vityou