输入一个十进制整数并将其转化为二进制整数输出
程序员文章站
2022-07-15 10:26:15
...
package com.rya.three;
import java.util.Scanner;
public class TenToTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入一个数:");
int n = in.nextInt();
int t = 0;//记录位数
int r = 0;//存储余数
int bin = 0;//存储最后二进制数
while (n != 0) {
r = n % 2;
n = n / 2;
bin += (int) (r * Math.pow(10, t));
t++;
}
System.out.println("对应的二进制为:" + bin);
}
}