Converting decimal to binary in Java

前端 未结 9 2290
时光取名叫无心
时光取名叫无心 2020-12-03 15:18

I\'m trying to write a code that converts a number to binary, and this is what I wrote. It gives me couple of errors in Eclipse, which I don\'t understand. What\'s wrong wit

9条回答
  •  醉梦人生
    2020-12-03 16:12

    Integer.toBinaryString(int) should do the trick !

    And by the way, correct your syntax, if you're using Eclipse I'm sure he's complaining about a lot of error.

    Working code :

    public class NumberConverter {
       public static void main(String[] args) {
           int i = Integer.parseInt(args[0]);
           toBinary(i);
       }
    
       public static void toBinary(int int1){
           System.out.println(int1 + " in binary is");
           System.out.println(Integer.toBinaryString(int1));
       }
    }
    

提交回复
热议问题