Java中十进制转二进制 [英] Converting decimal to binary in Java
本文介绍了Java中十进制转二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试编写一个将数字转换为二进制的代码,这就是我所写的.它在 Eclipse 中给了我几个错误,我不明白.那有什么问题?还有其他建议吗?我想学习并听取有关修复它的任何评论.谢谢.
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 with that? Any other suggestions? I'd like to learn and hear for any comments for fixing it. Thank you.
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
的无效类型
- 标记
- The method
parseInt(String)
in the typeInteger
is not applicable for the arguments (String[]
) - Multiple markers at this line
- Syntax error on token "
(
", ; expected - Syntax error on token "
)
", ; expected void
is an invalid type for the variableBinary
- Syntax error on token "
- 标记mod"的语法错误,无效的 AssignmentOperator
- 标记mod"的语法错误,无效的 AssignmentOperator.
推荐答案
Integer.toBinaryString(int) 应该可以解决问题!
顺便说一下,如果您使用的是 Eclipse,请更正您的语法,我敢肯定他会抱怨很多错误.
And by the way, correct your syntax, if you're using Eclipse I'm sure he's complaining about a lot of error.
工作代码:
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));
}
}
这篇关于Java中十进制转二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文