从键盘输入一个十进制整数数,转换成对应的二进制整数并输出。

import java.util.Scanner;
/*
 *@author Julain 
 *reminder 余数
 * quotient 商
 */
public class Convertion {
	public static void main(String[] args) {
		System.out.println("请输入一个十进制数");
		Scanner sc = new Scanner(System.in);
		int input = sc.nextInt();
		int quotient = input / 2;
		int reminder = input - quotient * 2;
		String result = "";
		result = reminder + result;
		while (quotient >= 2) {
			int tmp = quotient / 2;
			reminder = quotient - tmp * 2;
			result = reminder + result;
			quotient = tmp;
		}
		result = quotient + result;
		System.out.println("转换成二进制结果为:");
		System.out.println(result);
		sc.close();
	}
}

**编写环境JDK、JRE、Eclipse、

你可能感兴趣的:(JAVA)