我想在java中創建一段代碼,它將十進制值轉換爲二進制而不使用內置二進制轉換器命令。十進制到二進制轉換器
但它不工作...
public class MainFrame {
public static void binary(int number) {
String result = new String();
int binaryValues[] = {1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1};
if (number == 0) {
result = result + "0";
} else if (number == 1) {
result = result + "1";
} else {
for (int i = 0; i < 11; i++) {
while(number >= binaryValues[i]) {
if (number % binaryValues[i] >= 0) {
result = result + "1";
number -= binaryValues[i];
} else {
result = result + "0";
// number -= binaryValues[i];
}
}
}
}
System.out.println(result);
}
public static void main(String[] args) {
binary(5);
}
}
是這個家庭作業? – Bozho 2011-01-31 09:27:17
看起來像功課,你有一個代碼。請解釋你到目前爲止所瞭解的內容。 – 2011-01-31 09:29:22
它不是功課,我只是對如何做它感興趣 – java 2011-01-31 09:36:24