简体   繁体   English

十进制转换为二进制Java程序

[英]decimal to binary java program

I'm trying to make a java program where the user inputs a decimal number and it it then converted to binary. 我正在尝试制作一个Java程序,其中用户输入一个十进制数字,然后将其转换为二进制。 this is what i have so far but the while loop only loops once and only gives me 0 when i input 8. I know 8 in binary is 1000 so i don't understand what I'm doing wrong. 这是我到目前为止的内容,但是while循环仅循环一次,并且在我输入8时仅给我0。我知道二进制数8是1000,所以我不明白我在做什么错。 I need the coding to be simple. 我需要编码很简单。 please help thanks 请帮助谢谢

import java.util.Scanner;

 public class binary
 {
public static void main (String [] args)
{
    Scanner scan = new Scanner(System.in);

    int userNum = 0;
    int binary = 0;
    double newNum = 0;
    int count = 0;

    System.out.print("Enter a positive base 10 number: ");
    userNum = scan.nextInt();
    System.out.println();

    for(; (Math.pow(2,count) <= userNum); count++)
    {

    }

    while(!(count == 0))
    {
        if(userNum/Math.pow(2,count) != 0)
        {
            binary = userNum/(int)Math.pow(2,count);
            System.out.print(binary);
        }

        userNum %= Math.pow(2,count);
        count--;
        userNum %= (int)Math.pow(2,count));
    }
}
 }

You shouldn't re-invent the wheel, use Integer.toBinaryString(int) and something like, 您不应该重新发明轮子,使用Integer.toBinaryString(int)东西,

Scanner scan = new Scanner(System.in);

System.out.print("Enter a positive base 10 number: ");
if (scan.hasNextInt()) {
    int userNum = scan.nextInt();
    System.out.printf("%d decimal is %s binary%n", userNum,
            Integer.toBinaryString(userNum));
}

Also, Java classes should start with a capital letter. 另外,Java类应以大写字母开头。 So your binary should probably be Binary . 因此,您的binary可能应该是Binary Finally, if you need to convert from binary to decimal, you can use Integer.parseInt(String, 2) . 最后,如果需要从二进制转换为十进制,则可以使用Integer.parseInt(String, 2)

You can use the logic like below: 您可以使用如下逻辑:

package com.java2novice.algos;

public class DecimalToBinary {

public void printBinaryFormat(int number){
    int binary[] = new int[25];
    int index = 0;
    while(number > 0){
        binary[index++] = number%2;
        number = number/2;
    }
    for(int i = index-1;i >= 0;i--){
        System.out.print(binary[i]);
    }
}

public static void main(String a[]){
    DecimalToBinary dtb = new DecimalToBinary();
    dtb.printBinaryFormat(25);
}
}

You just got : 您刚得到:

for(userNum = userNum; (Math.pow(2,count) <= userNum); count++)
{

} 

Instead of: 代替:

for(userNum = userNum; (Math.pow(2,count) <= userNum); count++){
while(!(count == 0))
{
    if(userNum/Math.pow(2,count) != 0)
    {
        binary = userNum/(int)Math.pow(2,count);
        System.out.print(binary);
    }

    newNum = userNum%Math.pow(2,count);
    count--;
    userNum = (int)(newNum%(int)Math.pow(2,count));
}}
}
}

Basically you got empty if. 基本上,如果您是空的。

A couple issues: 几个问题:

Your count is going to always start one too high, since the last part of the for loop is going to be to increment it again. 因为for循环的最后一部分将再次增加它,所以您的count总是总是从一个开始太高。

You need the while loop to run when count is 0 to check the last bit. 您需要在count为0时运行while循环来检查最后一位。

The rest looks like it should be good, but I don't understand what the statements mean with newNum . 其余的看起来应该不错,但是我不明白newNum

An easier version of your algorithm is to use bitwise operators -- instead of having to divide by powers of two explicitly with Math.pow , you can use bitwise operators to test if bits are on or off, and then print out 1's and 0's accordingly. 该算法的一个更简单的版本是使用按位运算符-不必使用Math.pow明确除以2的幂,您可以使用按位运算符来测试位是打开还是关闭,然后相应地打印出1和0。 。 For example: 例如:

public static void toBinary(int n) {
   int mask = 1, nn = n;
   while(nn != 0) {
      mask << 1;
      nn /= 2;
   }
   mask >>= 1;

   while(mask != 0) {
      System.out.print( (mask & n) ? "1" : "0" );
      mask >>= 1;
   }
}

EDIT: Also, your userNum = userNum is redundant in the for loop. 编辑:另外,您的userNum = userNumfor循环中是多余的。 Just use a null statement ( ; ) in the assignment part of the loop instead. 只需在循环的赋值部分使用null语句( ; )。 So: 所以:

for(; (Math.pow(2, count) <= userNum); count++) {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM