在Java中将小数转换为二进制 [英] Converting decimal to binary in Java

查看:307
本文介绍了在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);
    }
  }
}

错误消息:


  1. 方法 parseInt(String)在类型 Integer 不适用于参数( String []

  2. 此行有多个标记

    • 令牌上的语法错误

    • ,;预期
    • void 是变量的无效类型 Binary

  1. The method parseInt(String) in the type Integer is not applicable for the arguments (String[])
  2. Multiple markers at this line
    • Syntax error on token "(", ; expected
    • Syntax error on token ")", ; expected
    • void is an invalid type for the variable Binary

  • 令牌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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆