无法在 java 中使用循环分配数组值
Can't Assign arrays values with a loop in java
我正在尝试在 java 中重新创建 .toBinaryString() 函数,因为我手上有很多时间。这是我目前所拥有的:
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;
}
}
我制作数组是因为如果我只是做二的正常幂,它会向后打印数字。现在,当我 运行 它时,它打印 Null 8 次,就像我没有在第一个 for 循环中为每个数组变量分配一个数字,我确实这样做了。我对数组做错了吗?抱歉奇怪的函数和变量名,请不要告诉我我在浪费时间,因为已经有一个 .toBinaryString() 函数。
for(int ef = 1; ef > 128
由于ef
初始化为1,不是> 128
,所以循环不会执行。
您应该学会使用调试器并逐步执行代码。您会在 2 秒内找到它。
我正在尝试在 java 中重新创建 .toBinaryString() 函数,因为我手上有很多时间。这是我目前所拥有的:
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;
}
}
我制作数组是因为如果我只是做二的正常幂,它会向后打印数字。现在,当我 运行 它时,它打印 Null 8 次,就像我没有在第一个 for 循环中为每个数组变量分配一个数字,我确实这样做了。我对数组做错了吗?抱歉奇怪的函数和变量名,请不要告诉我我在浪费时间,因为已经有一个 .toBinaryString() 函数。
for(int ef = 1; ef > 128
由于ef
初始化为1,不是> 128
,所以循环不会执行。
您应该学会使用调试器并逐步执行代码。您会在 2 秒内找到它。