Java中十进制到二进制的转换

Java中十进制到二进制的转换,java,syntax,compiler-errors,Java,Syntax,Compiler Errors,我试图写一个代码,将一个数字转换成二进制,这就是我写的。它在Eclipse中给了我几个错误,我不理解。 怎么了?还有其他建议吗?我想了解并听取关于修复它的任何意见。多谢各位 public class NumberConverte { public static void main(String[] args) { int i = Integer.parseInt(args); public static void Binary(int int1){ System.

我试图写一个代码,将一个数字转换成二进制,这就是我写的。它在Eclipse中给了我几个错误,我不理解。 怎么了?还有其他建议吗?我想了解并听取关于修复它的任何意见。多谢各位

public class NumberConverte {
  public static void main(String[] args) {
    int i = Integer.parseInt(args);
    public static void Binary(int int1){
      System.out.println(int1 + "in binary is");
      do {
        System.out.println(i mod 2);
      } while (int1>0);
    }
  }
}
错误消息:

  • 类型
    Integer
    中的方法
    parseInt(String)
    不适用于参数(
    String[]
  • 这条线上有多个标记
    • 令牌“
      ”上出现语法错误;应为
    • 令牌“
      ”上的语法错误;期望
    • void
      对于变量
      Binary
  • 这条线上有多个标记
    • 令牌“mod”上的语法错误,AssignmentOperator无效
    • 令牌“mod”上的语法错误,AssignmentOperator无效

  • 我建议您首先在IDE中编译程序。如果你没有使用IDE,我建议你买一个免费的。这将向您显示错误所在,我建议您在编译错误之前更正错误,然后再考虑如何改进它。

    应该可以做到这一点

    顺便说一句,纠正你的语法,如果你使用的是Eclipse,我肯定他会抱怨很多错误

    工作代码:

    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));
       }
    }
    

    首先,您已经在一个方法中声明了一个方法。main方法是运行类时首先运行的方法。ParseInt接受一个字符串,而args是一组字符串,因此我们需要获取数组的第一个(基于0的)索引

    mod
    不是有效的运算符,您需要的语法是
    %

    您可以使用
    System.out.print
    在同一行上打印,而不是
    println

    尝试以下更正,并让我知道您的进展情况:

     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);
    
    
     }
    }
    

    也许您不想使用
    tobinarysting()
    。你说你现在正在学习,所以你可以自己这样做:

    /*
      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;
            }
        }
    }
    
    /*
    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
    */
    公共A类{
    公共静态void main(字符串[]args){
    inta=Integer.parseInt(args[0]);
    系统输出打印项次(a);
    int位=1;
    
    对于(int i=0;i,您需要解决两个主要问题:

    • 不要在另一个方法中声明一个方法。
    • 你的循环永远不会结束。
    首先,人们已经指出了如何编写该方法

    第二,您永远不会更改
    int1
    的值,因此最终会在一个紧密的循环中打印输入的LSB。请尝试以下操作:

    do {
      System.out.println(int1 & 1);
      int1 = int1 >> 1;
    } while (int1 > 0);
    
    说明:

    • int1&1:这是一个二进制和。它“选择”最小的位(LSB),即(a&1)对于奇数是一,对于偶数是零。
    • int1>>1:这是右移。它将所有位向下移动一个插槽(>>3将向下移动3个插槽)。LSB(位0)被丢弃,位1变为LSB,位2变为位1,等等。(a>>0)什么都不做,保留一个完整的。
    然后,您会注意到您正在以“错误的顺序”打印数字-将它们打印到MSB到LSB更自然。您正在以相反的方式输出。要解决这一问题,您最好使用
    for
    循环,检查从MSB到LSB的每一位

    for循环的思想是查看int中的每个32位,从MSB开始,以便从左到右打印

    for (i=31; i>=0; i--) {
      if (int1 & (1<<i)) {
        // i-th bit is set
        System.out.print("1");
      } else {
        // i-th bit is clear
        System.out.print("0");
      }
    }
    
    (i=31;i>=0;i--)的
    {
    
    if(int1&(1这里是我为Android编写的一段痛苦的代码

    int myres=最苦的(7128)

    public int bitest(int位,int值)
    {
    int res=0;
    int i=0;
    而(i>1;
    i++;
    }
    返回res;
    }
    
    致意
    Mikael Andersson

    首先,我假设您知道错误消息。其次,您的代码很糟糕(语法和缩进不正确)。我建议使用下面的代码

    StringBuffer sb = new StringBuffer("");
    void breakNumber(int num){
        if(num == 0 || num == 1){
            System.out.println(num);
        }else{
            int modr = num % 2;
            sb.append(modr);
            int divr = num / 2;
            if(divr > 1){
                  breakNumber(divr);    
            }else{
                 sb.append(modr);
                 StringBuffer sbr =sb.reverse();
                 System.out.println(sbr.toString());    
            }
        }
     }
    
    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);
       }
    }
    
    import java.util.Scanner;
    公共类国际旅行社
    {
    公共静态void main(字符串[]args)
    {
    整数=0;
    扫描仪sc=新的扫描仪(System.in);
    System.out.println(“请输入一个整数:”);
    编号=sc.nextInt();
    转换二进制(数字);
    sc.close();
    }
    公共静态void convertToBinary(int num)
    {
    字符串str=“”;
    对于(int a=0;a<8;a++)
    {
    如果(数量%2==1)
    {
    str=“1”+str;
    }
    如果(数值%2==0)
    {
    str=“0”+str;
    }
    num=num/2;
    }
    System.out.println(“二进制转换为:“+str”);
    }
    }
    

    希望有帮助!!

    你能写下你的输入和错误是什么吗?谢谢你的评论。当然,1.整型中的parseInt(String)方法不适用于参数(String[])2.此行有多个标记-令牌上的语法错误(“,;预期-令牌上的语法错误”)”,;应为-void是变量Binary 3的无效类型。此行有多个标记-标记“mod”上的语法错误,无效的AssignmentOperator-标记“mod”上的语法错误,无效的AssignmentOperator。下一次,将错误消息直接添加到问题中(那里有一个编辑链接).我现在为你做了这件事。这里没有小数。输入已经是二进制的。对不起,我是“修复”,不是“改进”。虽然这对学习Java编程没有多大帮助!另一个问题。我如何从用户那里获得输入?我可能应该在主函数中添加一些内容,是吗?谢谢chimoo的评论!1.为什么我必须将[0]放在parseInt函数中?2.我可以使用“mod”吗现在,在没有语法错误之后,它仍然在线程“main”java.lang.ArrayIndexOutOfBoundsException中显示Exception,而不是&?3
    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;
        }
    
    }
    
    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);
       }
    }