Lesson_3 作业_2 十进制转二进制

一、问题描述

  输入一个十进制数,将其转换成二进制并输出

二、代码

 1 /********************************************
 2 //        十进制和二进制转换
 3 //            2013-01-14
 4 ********************************************/
 5 
 6 
 7 import java.util.Scanner;
 8 
 9 
10 public class 进制转换{
11     public static void main(String []args){
12         System.out.println("请输入要转换的数:");
13         Scanner sc = new Scanner(System.in);
14         int in = sc.nextInt();
15         int newIn = in;
16 
17         //C语言写法int out[];
18         int []out = new int[20];//定义数组的方法
19         int count = 0;
20         for(int i = 0; newIn > 0; i++ ){
21             out[i] = newIn % 2;
22             newIn /= 2;
23             count++;
24         }
25 
26         for(int i = count-1; i >= 0; i--){
27             System.out.print(out[i]);
28         }
29         System.out.println();
30     }
31 }


三、运行结果

原文地址:https://www.cnblogs.com/CocoonFan/p/2861704.html