在Java中将十进制转换为二进制

我正在尝试编写一个将数字转换为二进制的代码,这就是我写的内容。 它给了我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); } } } 

错误消息:

  1. 参数类型中的方法parseInt(String)不适用于参数( String[]
  2. 此行有多个标记
    • 令牌上的语法错误“ ( ”,;预期
    • 令牌上的语法错误“ ) ”,; 预期
    • void是变量Binary的无效类型
  3. 此行有多个标记
    • 令牌“mod”上的语法错误,AssignmentOperator无效
    • 令牌“mod”上的语法错误,AssignmentOperator无效。

Integer.toBinaryString(int)应该可以做到!

顺便说一句,纠正你的语法,如果你正在使用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)); } } 

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

 /* 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; } } } 

我建议你先在IDE中编译你的程序。 如果您没有使用IDE,我建议您免费使用IDE。 这将显示您的错误在哪里,我建议您更正错误,直到它编译之后再担心如何改进它。

您需要解决两个主要问题:

  • 不要在另一个方法中声明一个方法。
  • 你的循环永远不会结束。

首先,人们已经指出了如何编写该方法。 请注意,java中的常规方法名称通常拼写为首字母小写。

对于第二个,你永远不会改变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)什么都不做,保持完整。

然后你会注意到你正在以“错误的顺序”打印数字 – 将它们打印到LSB更自然。 你正在反向输出。 要解决这个问题,使用for循环可能会更好,检查从MSB到LSB的每个位。

for循环的想法是查看int中的32位中的每一位,从MSB开始,以便从左到右打印它们。 像这样的东西

 for (i=31; i>=0; i--) { if (int1 & (1< 

1<是左移。 类似于右移,但在另一个方向。 (我根本没有测试过这个。)

一旦你开始工作,我建议你做一个进一步的练习,你尝试做同样的事情,但不要打印出前导的零。

对于初学者,你已经在方法中声明了一个方法。 主要方法是在运行类时首先运行的方法。 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); } } 

这是我为Android制作的一个小测试代码。

int myres = bitTest(7,128);

 public int bitTest(int bit,int value) { int res = 0; int i = 0; while (i <= bit) { res = (value & 1); value = value >> 1; i++; } return 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); } } 

希望能帮助到你!!

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