进制间的转换,不使用Interger.toBinaryString(int) 等方法
作者:互联网
十进制转二进制
import java.util.Scanner;
class Main {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int numberNotChange = 0;
do{
//获得输入de整数
System.out.print("\nEnter a int number: ");
numberNotChange = input.nextInt();
int number = numberNotChange;
String numbertoBinaryString = "";
//如果为负数变成正数
number = (number >=0 ? number : (0 - number));
//如果为0输出值并结束程序
if(number == 0)
System.out.print(number + " 的二进制是: 0");
//计算不为0的二进制值
while(number != 0){
numbertoBinaryString = (number % 2) + numbertoBinaryString;
number = number / 2;
}
//输出二进制
if(numberNotChange!=0)
System.out.print((numberNotChange > 0) ? numberNotChange + " 的二进制是: "
+ numbertoBinaryString : numberNotChange + " 的二进制是: -"
+ numbertoBinaryString);
}while(numberNotChange != 0);
input.close();
}
}
十进制转十六进制
标签:int,numberNotChange,Interger,number,System,二进制,toBinaryString,numbertoBinaryStri 来源: https://www.cnblogs.com/doudou-20123/p/16317813.html