Converting decimal to binary in Java

前端 未结 9 2263
时光取名叫无心
时光取名叫无心 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 15:55

    Maybe you don't want to use toBinaryString(). You said that you are learning at the moment, so you can do it yourself like this:

    /*
      F:\>java A 123
      123
        1  1  0  1  1  1  1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
      0  0  0  0  0  0
    */
    
    public class A {
        public static void main(String[] args) {
    
            int a = Integer.parseInt(args[0]);
            System.out.println(a);
    
            int bit=1;
            for(int i=0; i<32; i++) {
                System.out.print("  "+(((a&bit)==0)?0:1));
                bit*=2;
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-03 16:05
    package gg;
    
    import java.util.*;
    
    public class Gg {
    
        public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
            boolean flag = true;
            while (flag) {
                menu();
                int n = in.nextInt();
                switch (n) {
                case 1:
                    System.out.println("enter an integer decimal number : ");
                    int d = in.nextInt();
                    System.out.print("the answer is ");
                    DTB(d);
                    System.out.println();
                    break;
                case 2:
                    System.out.println("enter a binary number : ");
                    int b = in.nextInt();
                    System.out.print("the answer is " + BTD(b));
                    System.out.println();
                    break;
                case 3:
                    flag = false;
                    break;
                }
            }
        }
    
        public static void menu() {
            System.out.println("1.convert decimal to binary : ");
            System.out.println("2.convert binary to decimal : ");
            System.out.println("3.exit");
        }
    
        public static void DTB(int x) {
            int n = 0;
            int y = x;
            while (y > 0) {
                y /= 2;
                n++;
            }
            int s[] = new int[n];
            int i = 0;
            while (x > 0) {
                s[i] = x % 2;
                x /= 2;
                i++;
            }
            for (int j = s.length - 1; j >= 0; j--) {
                System.out.print(s[j]);
            }
        }
    
        public static int BTD(int x) {
            int y = 2;
            int sum = 0;
            double k = 1;
            int c = 0;
            while (x > 0) {
                double z = x % 10;
                x /= 10;
                k = Math.pow(y, c);
                c++;
                k *= z;
                sum += k;
            }
            return sum;
        }
    
    }
    
    0 讨论(0)
  • 2020-12-03 16:08

    I suggest you get your program to compile first in your IDE. If you are not using an IDE I suggest you get a free one. This will show you where your errors are and I suggest you correct the errors until it compiles before worring about how to improve it.

    0 讨论(0)
  • Well, first I assume you know about error messages. Second, your code is bleak (syntax and indentation is not correct). I would suggest the code below,

    import java.util.Scanner;
    
    public class IntToBinary
    {
       public static void main(String[] args)
       {
          int number = 0;
          Scanner sc = new Scanner(System.in);
          System.out.println("Please enter an integer : ");
          number = sc.nextInt(); 
          convertToBinary(number);
          sc.close();
       }
    
       public static void convertToBinary(int num)
       {
          String str = "";
          for(int a = 0; a < 8; a++)
          {
             if(num % 2 == 1)
             {
                str = "1" + str;
             }
             if(num % 2 == 0)
             {
                str = "0" + str;
             }
             num = num / 2;
          }
          System.out.println("The binary conversion is : " + str);
       }
    }
    

    Hope it helps!!

    0 讨论(0)
  • 2020-12-03 16:10

    For starters you've declared a method inside a method. The main method is the method that runs first when you run your class. ParseInt takes a string, whereas args is an Array of strings, so we need to take the first (0-based) index of the array.

    mod is not a valid operator, the syntax you wanted was %

    You can use System.out.print to print on the same line rather than println

    Try these corrections and let me know how you get on:

     public class NumberConverter {
      public static void main(String[] args) {
      int i = Integer.parseInt(args[0]);
      Binary(i);
     } 
    
     public static void Binary(int int1){
        System.out.println(int1 + " in binary is ");
        do {
            System.out.print(int1 % 2);
            int1 /= 2;
        } while (int1 > 0);
    
    
     }
    }
    
    0 讨论(0)
  • 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));
       }
    }
    
    0 讨论(0)
提交回复
热议问题