注册 登录
编程论坛 新人交流区

关于十进制转二进制的小程序~

iq717 发布于 2007-10-24 08:28, 857 次点击

eclipse总报错,我感觉是数组的问题,因为有一次我遇到一摸一样的问题,没有语法错误。请各位高手帮忙看一下。

public class Scale_Of_Two {

void bin(int D) {
int i = 0;
int[] a = new int[100];

while (a[i] != 1) {

a[i] = D % 2;
D = (int) (D / 2);
i++;
}
for (int b = i; b < 1; b--)
System.out.println(a[b]);

}

/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
Scale_Of_Two count = new Scale_Of_Two();
count.bin(10);
}

}

6 回复
#2
iq7172007-10-24 08:29
这些的eclipse的报错:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 100
at Utility.Scale_Of_Two.bin(Scale_Of_Two.java:9)
at Utility.Scale_Of_Two.main(Scale_Of_Two.java:27)
#3
neufcl2007-10-24 10:35

// 10to2.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int D=10;
int i=0;
int b=0;
int a[100];
while(D>0)
{
a[i]=D%2;
D=D/2;
i++;
};
for(;i>0;i--)
cout<<a[i-1];
return 0;
}

你有两处出现了逻辑错
while

for两个循环条件不正确

#4
hczsea2007-10-24 11:18

楼上说的对。
楼主主要是:1判断条件错误
2输出下标错误

#5
iq7172007-10-24 18:56

找到了,谢谢各位啊。非常感谢

#6
大旺旺2007-10-24 20:14
唉,上晚了~~~要不然,咱也可以插句话了~~~……
#7
e680e6802007-10-24 22:01
不懂.还是要看看
1